Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 154
0.00% covered (danger)
0.00%
0 / 43
CRAP
0.00% covered (danger)
0.00%
0 / 1
LoyaltyCoupon
0.00% covered (danger)
0.00%
0 / 154
0.00% covered (danger)
0.00%
0 / 43
4290
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 createFromUUID
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
30
 read
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
 create
0.00% covered (danger)
0.00%
0 / 20
0.00% covered (danger)
0.00%
0 / 1
20
 update
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
20
 deleteCoupon
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 createUUID
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 generate_uuid
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 crypto_rand_secure
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
6
 getCouponID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setCouponID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUseType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setUseType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getUseId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setUseId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMaximum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setMaximum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getMinimum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setMinimum
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRandom
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setRandom
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getValueRange
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setValueRange
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getValueWeights
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setValueWeights
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOtherText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setOtherText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExpireDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setExpireDate
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDb
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getGroupID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setGroupID
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getStoresArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setStoresArray
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getExpireDays
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setExpireDays
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getShortName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setShortName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace BuyerKiosk\Core\Loyalty;
4
5class LoyaltyCoupon extends LoyaltyGroup {
6    public $couponID;
7    public $useType;
8    public $useId;
9    public $type;
10    public $value;
11    public $maximum;
12    public $minimum;
13    public $random;
14    public $valueRange;
15    public $valueWeights;
16    public $otherText;
17    public $expireDate;
18    public $expireDays;
19    public $shortName;
20    private $db;
21
22    public function __construct($groupID = null, $typeNum = null, $couponID = null) {
23        //Make sure we have a valid storeGroup
24        if(parent::__construct($groupID, $typeNum)) {
25            global $db_name;
26            $this->db = dbConnectByName($db_name);
27            //If we know the couponID we can pull it from the database
28        } else {
29            global $db_name;
30            $this->db = dbConnectByName($db_name);
31        }
32        if(isset($couponID)) {
33            $this->read($couponID);
34        }
35        return false;
36    }
37    public function createFromUUID($uuid) {
38        try {
39            $stmt = $this->db->prepare("SELECT couponID FROM loyaltyCoupon WHERE UUID = :uuid");
40            $stmt->bindValue(":uuid", $uuid);
41            if($stmt->execute()) {
42                if($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
43                    if(isset($row['couponID'])) {
44                        return $this->read("couponID");
45                    }
46                }
47            }
48        } catch (\PDOException $e) {
49        }
50        return false;
51    }
52    private function read($couponID) {
53        $stmt = $this->db->query("SELECT * FROM loyaltyCoupons WHERE id = ".$couponID);
54        $row = $stmt->fetch(\PDO::FETCH_ASSOC);
55        //Check to make sure couponID exists
56        if(isset($row['id'])) {
57            $this->couponID = $row['id'];
58            $this->useType = $row['useType'];
59            $this->useId = $row['useId'];
60            $this->type = $row['type'];
61            $this->value = $row['value'];
62            $this->maximum = $row['maximum'];
63            $this->minimum = $row['minimum'];
64            $this->random = $row['random'];
65            $this->valueRange = $row['valueRange'];
66            $this->valueWeights = $row['valueWeights'];
67            $this->otherText = $row['otherText'];
68            $this->shortName = $row['shortName'];
69            $this->getGroupByID($this->useId);
70            if(isset($row['expireDate'])) {
71                $this->expireDate = new \DateTime($row['expireDate'], new \DateTimeZone('utc'));
72            } else {
73                $this->expireDate = null;
74            }
75            $this->expireDays = $row['expireDays'];
76            return true;
77        }
78        return false;
79    }
80    public function create() {
81        try {
82            $stmt = $this->db->prepare("INSERT INTO loyaltyCoupons (useType, useId, type, value, maximum, minimum, otherText, expireDays, shortName)
83VALUES
84  (:useType, :useId, :type, :value, :maximum, :minimum, :otherText, :expireDays, :shortName)");
85            if(isset($this->expireDate)) {
86                $expireDate = $this->expireDate->format('Y-m-d');
87            } else {
88                $expireDate = null;
89            }
90            $stmt->bindParam(":useType", $this->useType);
91            $stmt->bindParam(":useId", $this->useId);
92            $stmt->bindParam(":type", $this->type);
93            $stmt->bindParam(":value", $this->value);
94            $stmt->bindParam(":maximum", $this->maximum);
95            $stmt->bindParam(":minimum", $this->minimum);
96            $stmt->bindParam(":otherText", $this->otherText);
97            $stmt->bindParam(":expireDays", $this->expireDays);
98            $stmt->bindParam(":shortName", $this->shortName);
99            if($stmt->execute()) {
100                $this->couponID = $this->db->lastInsertId();
101                return true;
102            }
103        } catch (\PDOException $e) {
104            return false;
105        }
106        return false;
107    }
108    public function update() {
109        if(isset($this->couponID)) {
110            try {
111                $stmt = $this->db->prepare("UPDATE loyaltyCoupons
112SET useType  = :useType, useId = :useId, type = :type, value = :value, maximum = :maximum, minimum = :minimum,
113  random     = :random, valueRange = :valueRange, valueWeights = :valueWeights, otherText = :otherText,
114  expireDate = :expireDate, shortName = :shortName
115WHERE id = :couponID");
116                $expireDate = $this->expireDate->format('Y-m-d');
117                $stmt->bindParam(":useType", $this->useType);
118                $stmt->bindParam(":useId", $this->useId);
119                $stmt->bindParam(":type", $this->type);
120                $stmt->bindParam(":value", $this->value);
121                $stmt->bindParam(":maximum", $this->maximum);
122                $stmt->bindParam(":minimum", $this->minimum);
123                $stmt->bindParam(":random", $this->random);
124                $stmt->bindParam(":valueWeights", $this->valueWeights);
125                $stmt->bindParam(":otherText", $this->otherText);
126                $stmt->bindParam(":expireDate", $expireDate);
127                $stmt->bindParam(":expireDays", $this->expireDays);
128                $stmt->bindParam(":shortName", $this->shortName);
129                if($stmt->execute()) {
130                    return true;
131                }
132            } catch (\PDOException $e) {
133                return false;
134            }
135        }
136        return false;
137    }
138    public function deleteCoupon() {
139        if(isset($this->couponID)) {
140            try {
141                $stmt = $this->db->prepare("DELETE FROM loyaltyCoupons WHERE id = :id");
142                $stmt->bindParam(":id", $this->couponID);
143                if ($stmt->execute()) {
144                    return true;
145                }
146            } catch (\PDOException $e) {
147                return false;
148            }
149        }
150        return false;
151    }
152    public function createUUID($loyaltyID, $storeGroup, $expireDate) {
153        try {
154            $uuid = $this->generate_uuid();
155            $this->db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
156            $stmt = $this->db->prepare("INSERT INTO loyaltyCouponUUID (UUID, loyaltyID, couponID, storeGroup, expireDate) VALUES (:uuid, :loyaltyID, :couponID, :storeGroup, :expireDate)");
157            $stmt->bindValue(":uuid", $uuid);
158            $stmt->bindValue(":couponID", $this->getCouponID());
159            $stmt->bindValue(":loyaltyID", $loyaltyID);
160            $stmt->bindValue(":storeGroup", $storeGroup);
161            $stmt->bindValue(":expireDate", $expireDate);
162            if($stmt->execute()) {
163                return $uuid;
164            } else {
165                //If we get an error code 1062, we know we generated an already used UUID, and we need to try again until we generate a unique one.
166                if((int)$stmt->errorCode() == 1062) {
167                    return $this->createUUID($loyaltyID, $storeGroup, $expireDate);
168                }
169            }
170        } catch (\PDOException $e) {
171            error_log($e->getMessage());
172        }
173
174        return null;
175    }
176    public function generate_uuid($len=8) {
177
178        $token = "";
179        $codeAlphabet = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
180        $codeAlphabet.= "abcdefghijklmnpqrstuvwxyz";
181        $codeAlphabet.= "123456789";
182        $max = strlen($codeAlphabet); // edited
183
184        for ($i=0; $i < $len; $i++) {
185            $token .= $codeAlphabet[$this->crypto_rand_secure(0, $max-1)];
186        }
187        return $token;
188    }
189    function crypto_rand_secure($min, $max)
190    {
191        $range = $max - $min;
192        if ($range < 1) return $min; // not so random...
193        $log = ceil(log($range, 2));
194        $bytes = (int) ($log / 8) + 1; // length in bytes
195        $bits = (int) $log + 1; // length in bits
196        $filter = (int) (1 << $bits) - 1; // set all lower bits to 1
197        do {
198            $rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
199            $rnd = $rnd & $filter; // discard irrelevant bits
200        } while ($rnd > $range);
201        return $min + $rnd;
202    }
203    public function getCouponID()
204    {
205        return $this->couponID;
206    }
207
208    /**
209     * @param mixed $id
210     */
211    public function setCouponID($id)
212    {
213        $this->couponID = $id;
214    }
215
216    /**
217     * @return mixed
218     */
219    public function getUseType()
220    {
221        return $this->useType;
222    }
223
224    /**
225     * @param mixed $useType
226     */
227    public function setUseType($useType)
228    {
229        $this->useType = $useType;
230    }
231
232    /**
233     * @return mixed
234     */
235    public function getUseId()
236    {
237        return $this->useId;
238    }
239
240    /**
241     * @param mixed $useId
242     */
243    public function setUseId($useId)
244    {
245        $this->useId = $useId;
246    }
247
248    /**
249     * @return mixed
250     */
251    public function getType()
252    {
253        return $this->type;
254    }
255
256    /**
257     * @param mixed $type
258     */
259    public function setType($type)
260    {
261        $this->type = $type;
262    }
263
264    /**
265     * @return mixed
266     */
267    public function getValue()
268    {
269        return $this->value;
270    }
271
272    /**
273     * @param mixed $value
274     */
275    public function setValue($value)
276    {
277        $this->value = $value;
278    }
279
280    /**
281     * @return mixed
282     */
283    public function getMaximum()
284    {
285        return $this->maximum;
286    }
287
288    /**
289     * @param mixed $maximum
290     */
291    public function setMaximum($maximum)
292    {
293        $this->maximum = $maximum;
294    }
295
296    /**
297     * @return mixed
298     */
299    public function getMinimum()
300    {
301        return $this->minimum;
302    }
303
304    /**
305     * @param mixed $minimum
306     */
307    public function setMinimum($minimum)
308    {
309        $this->minimum = $minimum;
310    }
311
312    /**
313     * @return mixed
314     */
315    public function getRandom()
316    {
317        return $this->random;
318    }
319
320    /**
321     * @param mixed $random
322     */
323    public function setRandom($random)
324    {
325        $this->random = $random;
326    }
327
328    /**
329     * @return mixed
330     */
331    public function getValueRange()
332    {
333        return $this->valueRange;
334    }
335
336    /**
337     * @param mixed $valueRange
338     */
339    public function setValueRange($valueRange)
340    {
341        $this->valueRange = $valueRange;
342    }
343
344    /**
345     * @return mixed
346     */
347    public function getValueWeights()
348    {
349        return $this->valueWeights;
350    }
351
352    /**
353     * @param mixed $valueWeights
354     */
355    public function setValueWeights($valueWeights)
356    {
357        $this->valueWeights = $valueWeights;
358    }
359
360    /**
361     * @return mixed
362     */
363    public function getOtherText()
364    {
365        return $this->otherText;
366    }
367
368    /**
369     * @param mixed $otherText
370     */
371    public function setOtherText($otherText)
372    {
373        $this->otherText = $otherText;
374    }
375
376    /**
377     * @return mixed
378     */
379    public function getExpireDate()
380    {
381        return $this->expireDate;
382    }
383
384    /**
385     * @param mixed $expireDate
386     */
387    public function setExpireDate($expireDate)
388    {
389        $this->expireDate = $expireDate;
390    }
391
392    /**
393     * @return null|\PDO
394     */
395    public function getDb()
396    {
397        return $this->db;
398    }
399
400    /**
401     * @param null|\PDO $db
402     */
403    public function setDb($db)
404    {
405        $this->db = $db;
406    }
407
408    /**
409     * @return mixed
410     */
411    public function getGroupID()
412    {
413        return $this->groupID;
414    }
415
416    /**
417     * @param mixed $groupID
418     */
419    public function setGroupID($groupID)
420    {
421        $this->groupID = $groupID;
422    }
423
424    /**
425     * @return mixed
426     */
427    public function getStoresArray()
428    {
429        return $this->storesArray;
430    }
431
432    /**
433     * @param mixed $storesArray
434     */
435    public function setStoresArray($storesArray)
436    {
437        $this->storesArray = $storesArray;
438    }
439
440    /**
441     * @return mixed
442     */
443    public function getExpireDays()
444    {
445        return $this->expireDays;
446    }
447
448    /**
449     * @param mixed $expireDays
450     */
451    public function setExpireDays($expireDays)
452    {
453        $this->expireDays = $expireDays;
454    }
455
456    /**
457     * @return mixed
458     */
459    public function getShortName()
460    {
461        return $this->shortName;
462    }
463
464    /**
465     * @param mixed $shortName
466     */
467    public function setShortName($shortName)
468    {
469        $this->shortName = $shortName;
470    }
471
472
473
474}