Category   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 28
c 0
b 0
f 0
dl 0
loc 77
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A findByName() 0 5 1
A bootstrap() 0 4 1
B save() 0 39 8
1
<?php
2
3
namespace Source\Models;
4
5
use Source\Core\Model;
6
7
/**
8
 * @package Source\Models
9
 */
10
class Category extends Model
11
{
12
    /**
13
     * Category constructor.
14
     */
15
    public function __construct()
16
    {
17
        parent::__construct("product_type", ["id"], ["name"]);
18
    }
19
20
    /**
21
     * @param string $name
22
     * @param string $email
23
     * @param string $password
24
     * @param string|null $document
25
     * @return User
26
     */
27
    public function bootstrap(string $name): Category
28
    {
29
        $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...
30
        return $this;
31
    }
32
33
    /**
34
     * @param string $name
35
     * @param string $columns
36
     * @return null|Category
37
     */
38
    public function findByName(string $name, string $columns = "*"): ?Category
39
    {
40
        $find = $this->find("name = :name", "name={$name}", $columns);
41
42
        return $find->fetch();
43
    }
44
45
    /**
46
     * @return bool
47
     */
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)) {
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Source\Models\Category. Since you implemented __get, consider adding a @property annotation.
Loading history...
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();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $categoryId does not seem to be defined for all execution paths leading up to this point.
Loading history...
86
        return true;
87
    }
88
}
89