Restaurants::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace WagnerMontanini\GoomerApi\Api;
4
5
use WagnerMontanini\GoomerApi\Support\Pager;
6
use WagnerMontanini\GoomerApi\Models\Restaurant;
7
8
/**
9
 * Class Restaurants
10
 * @package WagnerMontanini\GoomerApi
11
 */
12
class Restaurants extends GoomerApi
13
{
14
    /**
15
     * Restaurants constructor.
16
     * @throws \Exception
17
     */
18
    public function __construct()
19
    {
20
        parent::__construct();
21
    }
22
23
    /**
24
     * list all restaurants
25
     */
26
    public function index(): void
27
    {
28
        $values = $this->headers;
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->headers can also be of type boolean. However, the property $headers is declared as type array|false. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
29
        
30
        //get restaurants
31
        $restaurants = (new Restaurant())->find();
32
33
        if (!$restaurants->count()) {
34
            $this->call(
35
                404,
36
                "not_found",
37
                "Nada encontrado para sua busca."
38
            )->back(["results" => 0]);
39
            return;
40
        }
41
42
        $page = (!empty($values["page"]) ? $values["page"] : 1);
43
        $pager = new Pager(url("/restaurants"));
44
        $pager->pager($restaurants->count(), 10, $page);
45
46
        $response["results"] = $restaurants->count();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
47
        $response["page"] = $pager->page();
48
        $response["pages"] = $pager->pages();
49
50
        foreach ($restaurants->limit($pager->limit())->offset($pager->offset())->order("name ASC")->fetch(true) as $restaurant) {
51
            $response["restaurants"][] = $restaurant->data();
52
        }
53
54
        $this->back($response);
55
        return;
56
    }
57
58
    /**
59
     * @param array $data
60
     * @throws \Exception
61
     */
62
    public function create(array $data): void
63
    {
64
        $request = $this->requestLimit("restaurantsCreate", 5, 60);
65
        if (!$request) {
66
            return;
67
        }
68
        
69
        if ( empty($data["name"]) || empty($data["address"]) ) {
70
            $this->call(
71
                400,
72
                "empty_data",
73
                "Para criar informe o nome da empresa e o endereço"
74
            )->back();
75
            return ;
76
        }
77
78
        $restaurant = new Restaurant();
79
        $restaurant->name = filter_var($data["name"], FILTER_SANITIZE_STRIPPED);
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on WagnerMontanini\GoomerApi\Models\Restaurant. Since you implemented __set, consider adding a @property annotation.
Loading history...
80
        $restaurant->address = filter_var($data["address"], FILTER_SANITIZE_STRIPPED);
0 ignored issues
show
Bug Best Practice introduced by
The property address does not exist on WagnerMontanini\GoomerApi\Models\Restaurant. Since you implemented __set, consider adding a @property annotation.
Loading history...
81
        $restaurant->image = (!empty($data["image"])) ? filter_var($data["image"], FILTER_SANITIZE_STRIPPED) : null;
0 ignored issues
show
Bug Best Practice introduced by
The property image does not exist on WagnerMontanini\GoomerApi\Models\Restaurant. Since you implemented __set, consider adding a @property annotation.
Loading history...
82
        $restaurant->is_active = (!empty($data["is_active"])) ? filter_var($data["is_active"], FILTER_SANITIZE_STRIPPED) : 0;
0 ignored issues
show
Bug Best Practice introduced by
The property is_active does not exist on WagnerMontanini\GoomerApi\Models\Restaurant. Since you implemented __set, consider adding a @property annotation.
Loading history...
83
        $restaurant->is_accepted = (!empty($data["is_accepted"])) ? filter_var($data["is_accepted"], FILTER_SANITIZE_STRIPPED) : 0;
0 ignored issues
show
Bug Best Practice introduced by
The property is_accepted does not exist on WagnerMontanini\GoomerApi\Models\Restaurant. Since you implemented __set, consider adding a @property annotation.
Loading history...
84
        $restaurant->is_schedulable = (!empty($data["is_schedulable"])) ? filter_var($data["is_schedulable"], FILTER_SANITIZE_STRIPPED) : 0;
0 ignored issues
show
Bug Best Practice introduced by
The property is_schedulable does not exist on WagnerMontanini\GoomerApi\Models\Restaurant. Since you implemented __set, consider adding a @property annotation.
Loading history...
85
        $restaurant->schedule_data = (!empty($data["schedule_data"])) ? $data["schedule_data"] : null;
0 ignored issues
show
Bug Best Practice introduced by
The property schedule_data does not exist on WagnerMontanini\GoomerApi\Models\Restaurant. Since you implemented __set, consider adding a @property annotation.
Loading history...
86
        $restaurant->save();
87
88
        if($restaurant->fail()){
89
            $this->call(
90
                400,
91
                "empty_data",
92
                $restaurant->fail()->getMessage()
93
            )->back();
94
            return;
95
        }
96
97
        $this->back(["restaurant" => $restaurant->data()]);
98
        return;
99
    }
100
101
    /**
102
     * @param array $data
103
     */
104
    public function read(array $data): void
105
    {
106
        if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT)) {
107
            $this->call(
108
                400,
109
                "invalid_data",
110
                "É preciso informar o ID do restaurante que deseja consultar"
111
            )->back();
112
            return;
113
        }
114
115
        $restaurant = (new Restaurant())->findById($restaurant_id);
116
117
        if (!$restaurant) {
118
            $this->call(
119
                404,
120
                "not_found",
121
                "Você tentou acessar um restaurante que não existe"
122
            )->back();
123
            return;
124
        }
125
126
        $response["restaurant"] = $restaurant->data();
0 ignored issues
show
Comprehensibility Best Practice introduced by
$response was never initialized. Although not strictly required by PHP, it is generally a good practice to add $response = array(); before regardless.
Loading history...
127
        
128
        $this->back($response);
129
    }
130
131
    /**
132
     * @param array $data
133
     */
134
    public function update(array $data): void
135
    {
136
        if ( empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT)) {
137
            $this->call(
138
                400,
139
                "invalid_data",
140
                "Informe o ID do restaurante que deseja atualizar"
141
            )->back();
142
            return;
143
        }
144
145
        $restaurant = (new Restaurant())->findById($restaurant_id);
146
147
        if (!$restaurant) {
148
            $this->call(
149
                404,
150
                "not_found",
151
                "Você tentou atualizar um restaurante que não existe"
152
            )->back();
153
            return;
154
        }
155
        
156
        $restaurant->name = (!empty($data["name"])) ? filter_var($data["name"], FILTER_SANITIZE_STRIPPED) : $restaurant->name;
157
        $restaurant->address = (!empty($data["address"])) ? filter_var($data["address"], FILTER_SANITIZE_STRIPPED) : $restaurant->address;
158
        $restaurant->image = (!empty($data["image"])) ? filter_var($data["image"], FILTER_SANITIZE_STRIPPED) : $restaurant->image;
159
        $restaurant->is_active = (!empty($data["is_active"])) ? filter_var($data["is_active"], FILTER_SANITIZE_STRIPPED) : $restaurant->is_active;
160
        $restaurant->is_accepted = (!empty($data["is_accepted"])) ? filter_var($data["is_accepted"], FILTER_SANITIZE_STRIPPED) : $restaurant->is_accepted;
161
        $restaurant->is_schedulable = (!empty($data["is_schedulable"])) ? filter_var($data["is_schedulable"], FILTER_SANITIZE_STRIPPED) : $restaurant->is_schedulable;
162
        $restaurant->schedule_data = (!empty($data["schedule_data"])) ? $data["schedule_data"] : $restaurant->schedule_data;
163
        $restaurant->save();
164
        
165
        if($restaurant->fail()){
166
            $this->call(
167
                400,
168
                "empty_data",
169
                $restaurant->fail()->getMessage()
170
            )->back();
171
            return;
172
        }
173
174
        $this->back(["restaurant" => $restaurant->data()]);
175
        return;
176
    }
177
178
    /**
179
     * @param array $data
180
     */
181
    public function delete(array $data): void
182
    {
183
        if (empty($data["restaurant_id"]) || !$restaurant_id = filter_var($data["restaurant_id"], FILTER_VALIDATE_INT)) {
184
            $this->call(
185
                400,
186
                "invalid_data",
187
                "Informe o ID do restaurante que deseja deletar"
188
            )->back();
189
            return;
190
        }
191
192
        $restaurant = (new Restaurant())->findById($restaurant_id);
193
194
        if (!$restaurant) {
195
            $this->call(
196
                404,
197
                "not_found",
198
                "Você tentou excluir um restaurante que não existe"
199
            )->back();
200
            return;
201
        }
202
203
        $restaurant->destroy();
204
        $this->call(
205
            200,
206
            "success",
207
            "O restaurante foi excluído com sucesso",
208
            "accepted"
209
        )->back();
210
    }
211
}