Phone   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 32
dl 0
loc 78
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A save() 0 29 6
A bootstrap() 0 6 1
A findByUserId() 0 15 3
1
<?php
2
3
namespace Source\Models;
4
5
use Source\Core\Model;
6
use Source\Core\Connect;
7
8
/**
9
 * @package Source\Models
10
 */
11
class Phone extends Model
12
{
13
    /**
14
     * Phone constructor.
15
     */
16
    public function __construct()
17
    {
18
        parent::__construct("phone", ["id"], ["user_id", "phone_type_id", "number"]);
19
    }
20
21
    /**
22
     * @param int $user_id
23
     * @param int $phone_type_id
24
     * @param int $phone_type_id
25
     * @return Phone
26
     */
27
    public function bootstrap(int $user_id, int $phone_type_id, int $number): Phone
28
    {
29
        $this->user_id = $user_id;
0 ignored issues
show
Bug Best Practice introduced by
The property user_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
30
        $this->phone_type_id = $phone_type_id;
0 ignored issues
show
Bug Best Practice introduced by
The property phone_type_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->number = $number;
0 ignored issues
show
Bug Best Practice introduced by
The property number does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        return $this;
33
    }
34
35
    /**
36
     * @param string $userId
37
     * @return null|Phone
38
     */
39
    public function findByUserId(string $userId)
40
    {
41
        try {
42
            $stmt = Connect::getInstance()->prepare("SELECT phone.id, phone.number, phone_type.name FROM redstore.phone INNER JOIN redstore.phone_type ON phone_type.id = phone.phone_type_id WHERE phone.user_id = :userId;");
43
            $stmt->bindValue(":userId", $userId, \PDO::PARAM_INT);
44
            $stmt->execute();
45
46
            if (!$stmt->rowCount()) {
47
                return null;
48
            }
49
50
            return $stmt->fetchAll();
51
        } catch (\PDOException $exception) {
52
            $this->fail = $exception;
53
            return null;
54
        }
55
    }
56
57
    /**
58
     * @return bool
59
     */
60
    public function save(): bool
61
    {
62
        if (!$this->required()) {
63
            $this->error = "Faltam campos obrigatórios!";
64
            return false;
65
        }
66
67
        /** Phone Update */
68
        if (!empty($this->id)) {
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Source\Models\Phone. Since you implemented __get, consider adding a @property annotation.
Loading history...
69
            $phoneId = $this->id;
70
71
            $this->update($this->safe(), "id = :id", "id={$phoneId}");
72
            if ($this->fail()) {
73
                $this->error = "Erro ao atualizar, verifique os dados";
74
                return false;
75
            }
76
        }
77
78
        /** Phone Create */
79
        if (empty($this->id)) {
80
            $phoneId = $this->create($this->safe());
81
            if ($this->fail()) {
82
                $this->error = "Erro ao cadastrar, verifique os dados";
83
                return false;
84
            }
85
        }
86
87
        $this->data = ($this->findById($phoneId))->data();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $phoneId does not seem to be defined for all execution paths leading up to this point.
Loading history...
88
        return true;
89
    }
90
}
91