Address::bootstrap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 4
1
<?php
2
3
namespace Source\Models\Address;
4
5
use Source\Core\Model;
6
use Source\Core\Connect;
7
8
/**
9
 * @package Source\Models
10
 */
11
class Address extends Model
12
{
13
    /**
14
     * Address constructor.
15
     */
16
    public function __construct()
17
    {
18
        parent::__construct('address', ['id'], ['city_id', 'street', 'cep', 'number']);
19
    }
20
21
    /**
22
     * @param int $city_id
23
     * @param string $street
24
     * @param string $cep
25
     * @param int $number
26
     * @return Address
27
     */
28
    public function bootstrap(int $city_id, string $street, string $cep, int $number): Address
29
    {
30
        $this->city_id = $city_id;
0 ignored issues
show
Bug Best Practice introduced by
The property city_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
31
        $this->street = $street;
0 ignored issues
show
Bug Best Practice introduced by
The property street does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
        $this->cep = $cep;
0 ignored issues
show
Bug Best Practice introduced by
The property cep does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
33
        $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...
34
        return $this;
35
    }
36
37
    public static function bindAddress(int $userId, int $addressId)
38
    {
39
        try {
40
            $stmt = Connect::getInstance()->prepare("INSERT INTO redstore.user_address (user_id, address_id, created_by, created_at, updated_at) VALUES (:userId, :addressId, :userId2, NOW(), NOW());");
41
            $stmt->bindValue(":userId", $userId, \PDO::PARAM_INT);
42
            $stmt->bindValue(":addressId", $addressId, \PDO::PARAM_INT);
43
            $stmt->bindValue(":userId2", $userId, \PDO::PARAM_INT);
44
            $stmt->execute();
45
46
            return true;
47
        } catch (\PDOException $exception) {
48
            return null;
49
        }
50
    }
51
52
    public static function getAddressByUserId(int $userId)
53
    {
54
        try {
55
            $stmt = Connect::getInstance()->prepare("SELECT redstore.address.* FROM redstore.address INNER JOIN redstore.user_address ON redstore.address.id = redstore.user_address.address_id WHERE redstore.user_address.user_id = :userId;");
56
            $stmt->bindValue(":userId", $userId, \PDO::PARAM_INT);
57
            $stmt->execute();
58
59
            if (!$stmt->rowCount()) {
60
                return null;
61
            }
62
63
            return $stmt->fetchAll();
64
        } catch (\PDOException $exception) {
65
            return null;
66
        }
67
    }
68
69
    public static function getBindedAddress(int $userId, int $addressId)
70
    {
71
        try {
72
            $stmt = Connect::getInstance()->prepare("SELECT user_id, address_id FROM redstore.user_address WHERE user_id = :userId AND address_id = :addressId;");
73
            $stmt->bindValue(":userId", $userId, \PDO::PARAM_INT);
74
            $stmt->bindValue(":addressId", $addressId, \PDO::PARAM_INT);
75
            $stmt->execute();
76
77
            if (!$stmt->rowCount()) {
78
                return null;
79
            }
80
81
            return $stmt->fetchAll();
82
        } catch (\PDOException $exception) {
83
            return null;
84
        }
85
    }
86
87
    /**
88
     * @return bool
89
     */
90
    public function save(): bool
91
    {
92
        /** Address Update */
93
        if (!empty($this->id)) {
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Source\Models\Address\Address. Since you implemented __get, consider adding a @property annotation.
Loading history...
94
            $addressId = $this->id;
95
96
            $this->update($this->safe(), "id = :id", "id={$addressId}");
97
            if ($this->fail()) {
98
                $this->error = "Erro ao atualizar, verifique os dados";
99
                return false;
100
            }
101
        }
102
103
        if (!$this->required()) {
104
            $this->error = "Campos inválidos!";
105
            return false;
106
        }
107
108
        /** Address Create */
109
        if (empty($this->id)) {
110
            $addressId = $this->create($this->safe());
111
            if ($this->fail()) {
112
                $this->error = "Erro ao cadastrar, verifique os dados";
113
                return false;
114
            }
115
        }
116
117
        $this->data = ($this->findById($addressId))->data();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $addressId does not seem to be defined for all execution paths leading up to this point.
Loading history...
118
        return true;
119
    }
120
}
121