Conditions | 8 |
Paths | 11 |
Total Lines | 39 |
Code Lines | 22 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
48 | public function save(): bool |
||
49 | { |
||
50 | if (!$this->required()) { |
||
51 | $this->error = "O nome da categoria é obrigatória"; |
||
52 | return false; |
||
53 | } |
||
54 | |||
55 | /** Category Update */ |
||
56 | if (!empty($this->id)) { |
||
57 | $categoryId = $this->id; |
||
58 | |||
59 | if ($this->find("name = :e AND id != :i", "e={$this->name}&i={$categoryId}", "id")->fetch()) { |
||
60 | $this->error = "A categoria informada já está cadastrada"; |
||
61 | return false; |
||
62 | } |
||
63 | |||
64 | $this->update($this->safe(), "id = :id", "id={$categoryId}"); |
||
65 | if ($this->fail()) { |
||
66 | $this->error = "Erro ao atualizar, verifique os dados"; |
||
67 | return false; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /** Category Create */ |
||
72 | if (empty($this->id)) { |
||
73 | if ($this->findByName($this->name, 'id')) { |
||
74 | $this->error = "A categoria informada já está cadastrada"; |
||
75 | return false; |
||
76 | } |
||
77 | |||
78 | $categoryId = $this->create($this->safe()); |
||
79 | if ($this->fail()) { |
||
80 | $this->error = "Erro ao cadastrar, verifique os dados"; |
||
81 | return false; |
||
82 | } |
||
83 | } |
||
84 | |||
85 | $this->data = ($this->findById($categoryId))->data(); |
||
86 | return true; |
||
87 | } |
||
89 |