State::bootstrap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Source\Models\Address;
4
5
use Source\Core\Model;
6
7
/**
8
 * @package Source\Models
9
 */
10
class State extends Model
11
{
12
    /**
13
     * State constructor.
14
     */
15
    public function __construct()
16
    {
17
        parent::__construct("state", ["id"], ["name", "acronym"]);
18
    }
19
20
    /**
21
     * @param string $name
22
     * @param string $acronym
23
     * @return State
24
     */
25
    public function bootstrap(string $name, string $acronym): State
26
    {
27
        $this->name = $name;
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        $this->acronym = $acronym;
0 ignored issues
show
Bug Best Practice introduced by
The property acronym does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29
        return $this;
30
    }
31
32
    /**
33
     * @param string $name
34
     * @param string $columns
35
     * @return null|State
36
     */
37
    public function findByName(string $name, string $columns = "*"): ?State
38
    {
39
        $find = $this->find("name = :name", "name={$name}", $columns);
40
41
        return $find->fetch();
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function save(): bool
48
    {
49
        if (!$this->required()) {
50
            $this->error = "Faltam campos obrigatórios!";
51
            return false;
52
        }
53
54
        /** State Update */
55
        if (!empty($this->id)) {
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Source\Models\Address\State. Since you implemented __get, consider adding a @property annotation.
Loading history...
56
            $stateId = $this->id;
57
58
            if ($this->find("name = :e AND id != :i", "e={$this->name}&i={$stateId}", "id")->fetch()) {
59
                $this->error = "O estado informado já está cadastrado!";
60
                return false;
61
            }
62
63
            $this->update($this->safe(), "id = :id", "id={$stateId}");
64
            if ($this->fail()) {
65
                $this->error = "Erro ao atualizar, verifique os dados";
66
                return false;
67
            }
68
        }
69
70
        /** State Create */
71
        if (empty($this->id)) {
72
            if ($this->find("name = :e AND id != :i", "e={$this->name}&i={$this->id}", "id")->fetch()) {
73
                $this->error = "O estado informado já está cadastrado!";
74
                return false;
75
            }
76
77
            $stateId = $this->create($this->safe());
78
            if ($this->fail()) {
79
                $this->error = "Erro ao cadastrar, verifique os dados";
80
                return false;
81
            }
82
        }
83
84
        $this->data = ($this->findById($stateId))->data();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stateId does not seem to be defined for all execution paths leading up to this point.
Loading history...
85
        return true;
86
    }
87
}
88