AddressController::addAddress()   B
last analyzed

Complexity

Conditions 10
Paths 32

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 50
rs 7.6666
cc 10
nc 32
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Source\Controllers;
4
5
use stdClass;
6
use Source\Core\Request;
7
use Source\Core\Response;
8
use Source\Models\User;
9
use Source\Models\Phone;
10
use Source\Models\Gender;
11
use Source\Models\AccessLevel;
12
use Source\Models\Address\Address;
13
use Source\Models\Address\City;
14
use Source\Controllers\Auth;
15
16
final class AddressController
17
{
18
    private $Message;
19
20
    private $Request;
21
22
    private $token;
23
24
    public function __construct()
25
    {
26
        $this->Message = new stdClass();
27
        $this->Request = new Request();
28
29
        $this->token = (new Auth())->validateLogin();
30
    }
31
32
    public function addAddress($data)
33
    {
34
        $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
35
36
        if (empty($data)) {
37
            $this->Message->message = 'Campos inválidos!';
38
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
39
        }
40
41
        $User = new User();
42
        /** @var Source\Models\User $User */
43
        $User = $User->findById($this->token['id'], 'id');
44
45
        if (!$User || $User == null) {
46
            $this->Message->message = 'Usuário não encontrado!';
47
            (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message);
48
        }
49
50
        $city_id = filter_var($data['city_id'], FILTER_DEFAULT);
51
        $street = filter_var($data['street'], FILTER_DEFAULT);
52
        $complement = filter_var($data['complement'], FILTER_DEFAULT);
53
        $cep = filter_var($data['cep'], FILTER_DEFAULT);
54
        $number = filter_var($data['number'], FILTER_DEFAULT);
55
56
        if (!$city_id || !$street || !$cep || !$number) {
57
            $this->Message->message = 'Endereço inválido!';
58
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
59
        }
60
61
        /** @var Source\Models\Address $Address */
62
        $Address = new Address();
63
64
        $Address->city_id = $city_id;
65
        $Address->street = $street;
66
        $Address->complement = $complement;
67
        $Address->cep = $cep;
68
        $Address->number = $number;
69
70
        if (!$Address->save()) {
71
            $this->Message->message = $Address->message();
72
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
73
        }
74
75
        if (!Address::bindAddress($User->id, $Address->id)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression Source\Models\Address\Ad...User->id, $Address->id) of type null|true is loosely compared to false; this is ambiguous if the boolean can be false. You might want to explicitly use !== null instead.

If an expression can have both false, and null as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.

$a = canBeFalseAndNull();

// Instead of
if ( ! $a) { }

// Better use one of the explicit versions:
if ($a !== null) { }
if ($a !== false) { }
if ($a !== null && $a !== false) { }
Loading history...
76
            $this->Message->message = 'Ocorreu algum erro ao cadastrar o endereço!';
77
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
78
        }
79
80
        $this->Message->message = 'Endereço salvo com sucesso!';
81
        (new Response())->setStatusCode(HTTP_OK)->send($this->Message);
82
    }
83
84
    public function updateAddress($data)
85
    {
86
        $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
87
88
        if (empty($data) || !isset($data['id']) || !filter_var($data['id'], FILTER_VALIDATE_INT)) {
89
            $this->Message->message = 'Campos inválidos!';
90
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
91
        }
92
93
        $User = new User();
94
        $User = $User->findById($this->token['id'], 'id');
95
96
        if (!$User || $User == null) {
97
            $this->Message->message = 'Usuário não encontrado!';
98
            (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message);
99
        }
100
101
        if (!Address::getBindedAddress($User->id, $data['id'])) {
0 ignored issues
show
Bug introduced by
$User->id of type null is incompatible with the type integer expected by parameter $userId of Source\Models\Address\Address::getBindedAddress(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        if (!Address::getBindedAddress(/** @scrutinizer ignore-type */ $User->id, $data['id'])) {
Loading history...
Bug Best Practice introduced by
The property id does not exist on Source\Core\Model. Since you implemented __get, consider adding a @property annotation.
Loading history...
102
            $this->Message->message = 'Você não possui permissão para editar este endereço!';
103
            (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message);
104
        }
105
106
        $this->verifyAddressData();
0 ignored issues
show
Bug introduced by
The call to Source\Controllers\Addre...er::verifyAddressData() has too few arguments starting with data. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
        $this->/** @scrutinizer ignore-call */ 
107
               verifyAddressData();

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
107
108
        /** @var Source\Models\Address $Address */
109
        $Address = new Address();
110
        $Address->city_id = $data['city_id'];
111
        $Address->street = $data['street'];
112
        $Address->cep = $data['cep'];
113
        $Address->number = $data['number'];
114
115
        if (!$Address->save()) {
116
            $this->Message->message = $Address->message();
117
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
118
        }
119
120
        $this->Message->message = 'Endereço salvo com sucesso!';
121
        (new Response())->setStatusCode(HTTP_OK)->send($this->Message);
122
    }
123
124
    private function verifyAddressData($data)
125
    {
126
        if (empty($data['city_id'])) {
127
            $this->Message->message = 'Cidade inválida!';
128
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
129
        }
130
131
        $City = new City();
132
133
        if (!$City->findById($data['city_id'])) {
134
            $this->Message->message = 'Cidade inválida!';
135
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
136
        }
137
138
        if (empty($data['street'])) {
139
            $this->Message->message = 'Nome da rua inválida!';
140
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
141
        }
142
143
        if (empty($data['cep'])) {
144
            $this->Message->message = 'CEP inválido!';
145
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
146
        }
147
148
        if (empty($data['number'])) {
149
            $this->Message->message = 'Número da casa inválido!';
150
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
151
        }
152
    }
153
154
    public function address($data)
155
    {
156
        $data = filter_var_array($data, FILTER_SANITIZE_STRIPPED);
157
158
        if (empty($data) || !isset($data['id']) || !filter_var($data['id'], FILTER_VALIDATE_INT)) {
159
            $this->Message->message = 'Campos inválidos!';
160
            (new Response())->setStatusCode(HTTP_PARTIAL_CONTENT)->send($this->Message);
161
        }
162
163
        /** @var Source\Models\Address $Address */
164
        $Address = new Address();
165
        $result = $Address->findById($data['id'], "*");
166
167
        if (!$result || $result == null) {
168
            $this->Message->message = 'Endereço não encontrado!';
169
            (new Response())->setStatusCode(HTTP_NOT_FOUND)->send($this->Message);
170
        }
171
172
        $result = $result->data();
173
        $this->Message->message = $result;
174
175
        (new Response())->setStatusCode(HTTP_OK)->send($this->Message);
176
    }
177
}
178