Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
Contact
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 7
420
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createByPhone
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
42
 get
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
30
 update
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
20
 generateCode
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 sendCode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 sanitizePhone
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace BuyerKiosk\ResalePerks;
4
5class Contact extends Base
6{
7
8    public $id;
9    public $phone;
10    public $generatedCode;
11    public $validated;
12    public $deviceID;
13    public $dateAdded;
14    public $dateValidated;
15    public $phoneType;
16    public $osVersion;
17    public $lastSeen;
18    public $homeStore;
19    public $oneSignalID;
20    public $phoneID;
21    public $dev;
22
23
24    public function __construct()
25    {
26        parent::__construct();
27    }
28
29    public function createByPhone($phone)
30    {
31        $phone = $this->sanitizePhone($phone);
32        if($phone !== null) {
33            $this->phone = $phone;
34        } else {
35            return false;
36        }
37        try {
38            $stmt = $this->db->prepare("SELECT * FROM rpContacts WHERE phone = :phone");
39            $stmt->bindValue(":phone", $this->phone);
40            if ($stmt->execute()) {
41                if ($stmt->rowCount() > 0) {
42                    //Record does exist
43                } else {
44                    //Record does not exist
45
46                    $code = $this->generateCode();
47                    $stmt = $this->db->prepare("INSERT INTO rpContacts (phone, generatedCode, validated, dateAdded) VALUES (:phone, :generatedCode, 0 , CURRENT_TIMESTAMP)");
48                    $stmt->bindValue(":phone", $this->phone);
49                    $stmt->bindValue(":generatedCode", $code);
50                    if($stmt->execute()) {
51                        $this->id = $this->db->lastInsertId();
52                        $this->generatedCode = $code;
53                        return true;
54                    }
55                }
56            }
57        } catch (\PDOException $e) {
58            return false;
59        }
60        return false;
61    }
62    public function get($phone) {
63        $phone = $this->sanitizePhone($phone);
64        if($phone !== null) {
65            $this->phone = $phone;
66        } else {
67            return false;
68        }
69        try {
70            $stmt = $this->db->prepare("SELECT * FROM rpContacts WHERE phone = :phone LIMIT 1");
71            $stmt->bindValue(":phone", $this->phone);
72            if($stmt->execute()) {
73                $row = $stmt->fetch(\PDO::FETCH_ASSOC);
74                if(isset($row['id'])) {
75                    $this->id = $row['id'];
76                    $this->generatedCode = $row['generatedCode'];
77                    $this->validated = $row['validated'];
78                    $this->deviceID = $row['deviceID'];
79                    $this->dateAdded = $row['dateAdded'];
80                    $this->dateValidated = $row['dateValidated'];
81                    $this->phoneType = $row['phoneType'];
82                    $this->osVersion = $row['osVersion'];
83                    $this->lastSeen = $row['lastSeen'];
84                    $this->homeStore = $row['homeStore'];
85                    $this->oneSignalID = $row['oneSignalID'];
86                    $this->phoneID = $row['phoneID'];
87                    $this->dev = $row['dev'];
88                    return true;
89                }
90            }
91
92        } catch (\PDOException $e) {
93            return false;
94        }
95        return false;
96    }
97    public function update() {
98        if($this->id > 0) {
99            try {
100                $stmt = $this->db->prepare("UPDATE rpContacts SET dateValidated = :dateValidated, phoneType = :phoneType, osVersion = :osVersion, lastSeen = :lastSeen, homeStore = :homeStore, oneSignalID = :oneSignalID, phoneID = :phoneID WHERE id = :id");
101                $stmt->bindValue(":id", $this->id);
102                $stmt->bindValue(":dateValidated", $this->dateValidated);
103                $stmt->bindValue(":phoneType", $this->phoneType);
104                $stmt->bindValue(":osVersion", $this->osVersion);
105                $stmt->bindValue(":lastSeen", $this->lastSeen);
106                $stmt->bindValue(":homeStore", $this->homeStore);
107                $stmt->bindValue(":oneSignalID", $this->oneSignalID);
108                $stmt->bindValue(":phoneID", $this->phoneID);
109                if($stmt->execute()) {
110                    return true;
111                }
112
113            } catch (\PDOException $e) {
114
115            }
116        }
117        return false;
118    }
119
120    public function generateCode()
121    {
122        $randomCode = mt_rand(100000, 999999);
123        return $randomCode;
124    }
125    public function sendCode() {
126
127    }
128    public function sanitizePhone($phone) {
129        //Remove all but digits from phone #
130        $phone = preg_replace('/[^0-9]/', '', $phone);
131        if (strlen($phone) !== 10) {
132            //After removal of non digits we should only have 10 digits for a phone number
133            return null;
134        }
135        return $phone;
136    }
137}
138