Gender   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A bootstrap() 0 4 1
A findByName() 0 5 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 Gender extends Model
11
{
12
    /**
13
     * Gender constructor.
14
     */
15
    public function __construct()
16
    {
17
        parent::__construct("gender", ["id"], ["name"]);
18
    }
19
20
    /**
21
     * @param string $name
22
     * @return Gender
23
     */
24
    public function bootstrap(string $name): Gender
25
    {
26
        $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...
27
        return $this;
28
    }
29
30
    /**
31
     * @param string $name
32
     * @param string $columns
33
     * @return null|Gender
34
     */
35
    public function findByName(string $name, string $columns = "*"): ?Gender
36
    {
37
        $find = $this->find("name = :name", "name={$name}", $columns);
38
39
        return $find->fetch();
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    public function save(): bool
46
    {
47
        if (!$this->required()) {
48
            $this->error = "Faltam campos obrigatórios!";
49
            return false;
50
        }
51
52
        /** Gender Update */
53
        if (!empty($this->id)) {
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist on Source\Models\Gender. Since you implemented __get, consider adding a @property annotation.
Loading history...
54
            $genderId = $this->id;
55
56
            if ($this->find("name = :e AND id != :i", "e={$this->name}&i={$genderId}", "id")->fetch()) {
57
                $this->error = "O sexo informado já está cadastrado";
58
                return false;
59
            }
60
61
            $this->update($this->safe(), "id = :id", "id={$genderId}");
62
            if ($this->fail()) {
63
                $this->error = "Erro ao atualizar, verifique os dados";
64
                return false;
65
            }
66
        }
67
68
        /** Gender Create */
69
        if (empty($this->id)) {
70
            if ($this->findByName($this->name, 'id')) {
71
                $this->error = "O sexo informado já está cadastrado";
72
                return false;
73
            }
74
75
            $genderId = $this->create($this->safe());
76
            if ($this->fail()) {
77
                $this->error = "Erro ao cadastrar, verifique os dados";
78
                return false;
79
            }
80
        }
81
82
        $this->data = ($this->findById($genderId))->data();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $genderId does not seem to be defined for all execution paths leading up to this point.
Loading history...
83
        return true;
84
    }
85
}
86