Product::findByName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
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;
4
5
use Source\Core\Model;
6
use Source\Models\Category;
7
use Source\Models\ProductImage;
8
9
/**
10
 * @package Source\Models
11
 */
12
class Product extends Model
13
{
14
    /**
15
     * User constructor.
16
     */
17
    public function __construct()
18
    {
19
        parent::__construct("product", ["id"], ["name", "value", "description", "available", "product_type_id"]);
20
    }
21
22
    /**
23
     * @param string $name
24
     * @param string $value
25
     * @param string $description
26
     * @param int $available
27
     * @param int $product_type_id
28
     * @return Product
29
     */
30
    public function bootstrap(
31
        string $name,
32
        string $value,
33
        string $description,
34
        int $available,
35
        int $product_type_id
36
    ): Product {
37
        $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...
38
        $this->value = $value;
0 ignored issues
show
Bug Best Practice introduced by
The property value does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
39
        $this->description = $description;
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40
        $this->available = $available;
0 ignored issues
show
Bug Best Practice introduced by
The property available does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
        $this->product_type_id = $product_type_id;
0 ignored issues
show
Bug Best Practice introduced by
The property product_type_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
42
        return $this;
43
    }
44
45
    /**
46
     * @param string $name
47
     * @param string $columns
48
     * @return null|Product
49
     */
50
    public function findByName(string $name, string $columns = "*"): ?Product
51
    {
52
        $find = $this->find("name = :name", "name={$name}", $columns);
53
54
        return $find->fetch();
55
    }
56
57
    public function findAll(int $limit = null, int $offset = null): ?array
58
    {
59
        $find = $this->find();
60
61
        if (!empty($limit)) {
62
            $find = $find->limit($limit);
63
        }
64
65
        if (!empty($offset)) {
66
            $find = $find->offset($offset);
67
        }
68
69
        $find = $find->fetch(true);
70
71
        if (empty($find)) {
72
            return null;
73
        }
74
75
        $return = [];
76
        foreach ($find as $key => $value) {
77
            $result = $value->data();
78
79
            $result->category = (new Category())->findById((int) $result->product_type_id)->data();
80
            $result->ProductImage = (new ProductImage())->findAllByProductId((int) $result->id);
81
82
            $return[] = $result;
83
        }
84
85
        return $return;
86
    }
87
88
    public function findByProductId(int $id, string $columns = '*'): ?Product
89
    {
90
        $find = $this->find("id = :id", "id={$id}", $columns);
91
92
        $value = $find->fetch();
93
94
        if (empty($value)) {
95
            return null;
96
        }
97
98
        $value->category = (new Category())->findById((int) $value->product_type_id)->data();
99
        $value->ProductImage = (new ProductImage())->findAllByProductId((int) $value->id);
100
101
        return $value;
102
    }
103
104
    public function coluntAllAvailableProducts(): int
105
    {
106
        $this->rawQuery('SELECT COUNT(id) AS count FROM product;');
107
108
        return (int) ($this->fetch())->data()->count;
109
    }
110
111
    /**
112
     * @return bool
113
     */
114
    public function save(): bool
115
    {
116
        if (!$this->required()) {
117
            $this->error = "Nome, valor, descrição, categoria e estoque são obrigatórios";
118
            return false;
119
        }
120
121
        /** User Update */
122
        if (!empty($this->id)) {
123
            if ($this->find("name = :e AND id != :i", "e={$this->name}&i={$this->id}", "id")->fetch()) {
124
                $this->error = "O produto informado já está cadastrado";
125
                return false;
126
            }
127
128
            $this->update($this->safe(), "id = :id", "id={$this->id}");
129
            if ($this->fail()) {
130
                $this->error = "Erro ao atualizar, verifique os dados";
131
                return false;
132
            }
133
        }
134
135
        /** User Create */
136
        if (empty($this->id)) {
137
            if ($this->find("name = :e", "e={$this->name}")->fetch()) {
138
                $this->error = "O produto informado já está cadastrado";
139
                return false;
140
            }
141
142
            $this->id = $this->create($this->safe());
0 ignored issues
show
Bug Best Practice introduced by
The property id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
143
144
            if ($this->fail()) {
145
                error_log($this->fail());
146
                $this->error = "Erro ao cadastrar, verifique os dados";
147
                return false;
148
            }
149
        }
150
151
        $this->data = ($this->findById($this->id))->data();
152
        return true;
153
    }
154
}
155