AddressService::discoveryState()   B
last analyzed

Complexity

Conditions 8
Paths 17

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 20
c 1
b 0
f 0
nc 17
nop 4
dl 0
loc 28
rs 8.4444
1
<?php
2
3
namespace ControleOnline\Service;
4
5
use ControleOnline\Entity\Address;
6
use ControleOnline\Entity\Cep;
7
use ControleOnline\Entity\City;
8
use ControleOnline\Entity\Country;
9
use ControleOnline\Entity\District;
10
use ControleOnline\Entity\People;
0 ignored issues
show
Bug introduced by
The type ControleOnline\Entity\People was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use ControleOnline\Entity\State;
12
use ControleOnline\Entity\Street;
13
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use DateTime;
15
use Exception;
16
17
class AddressService
18
{
19
20
21
  public function __construct(
22
    private  EntityManagerInterface $manager
23
  ) {}
24
25
  public function discoveryAddress(
26
    ?People $people = null,
27
    int $postalCode,
28
    int $streetNumber,
29
    string $streetName,
30
    string $district,
31
    string $city,
32
    string $uf,
33
    string $countryCode,
34
    ?string $complement = null,
35
    ?int $latitude = 0,
36
    ?int $longitude = 0,
37
    ?string $nickName = 'Default',
38
  ): Address {
39
40
    $cep = ($postalCode) ? $this->discoveryCep($postalCode) : null;
41
    $country = ($countryCode) ? $this->getCountry($countryCode) : null;
42
    $state = ($uf && $country) ? $this->discoveryState($country, $uf) : null;
43
    $city = ($city && $state) ? $this->discoveryCity($state, $city) : null;
44
    $district = ($district && $city) ? $this->discoveryDistrict($city, $district) : null;
45
    $street = ($streetName && $cep && $district) ? $this->discoveryStreet($cep, $district, $streetName) : null;
46
47
    $address =  $this->manager->getRepository(Address::class)->findOneBy([
48
      'people' => $people,
49
      'street' => $street,
50
      'number' => $streetNumber,
51
      'complement' => $complement
52
    ]);
53
54
    if (!$address) {
55
      $address = new Address();
56
      $address->setNumber($streetNumber);
57
      $address->setNickname($nickName);
58
      $address->setComplement($complement);
59
      $address->setStreet($street);
60
      $address->setPeople($people);
61
    }
62
    if ($latitude > 0) $address->setLatitude($latitude);
63
    if ($longitude > 0) $address->setLongitude($longitude);
64
65
66
    $this->manager->persist($address);
67
    $this->manager->flush();
68
69
    return  $address;
70
  }
71
72
73
74
  public function discoveryCep(string $postalCode): Cep
75
  {
76
    $cep = $this->manager->getRepository(Cep::class)->findOneBy(['cep' => $postalCode]);
77
78
    if (!$cep) {
79
      $cep = new Cep();
80
      $cep->setCep($postalCode);
0 ignored issues
show
Bug introduced by
$postalCode of type string is incompatible with the type integer expected by parameter $cep of ControleOnline\Entity\Cep::setCep(). ( Ignorable by Annotation )

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

80
      $cep->setCep(/** @scrutinizer ignore-type */ $postalCode);
Loading history...
81
      $this->manager->persist($cep);
82
      $this->manager->flush();
83
    }
84
85
    return $cep;
86
  }
87
  public function discoveryStreet(Cep $cep, District $district, string $streetName): Street
88
  {
89
    $search = [
90
      'cep' => $cep,
91
      'district' => $district,
92
      'street' => $streetName
93
    ];
94
    $street =  $this->manager->getRepository(Street::class)->findOneBy($search);
95
96
    if (!$street) {
97
      $street = new Street();
98
      $street->setCep($cep);
99
      $street->setDistrict($district);
100
      $street->setStreet($streetName);
101
      $this->manager->persist($street);
102
      $this->manager->flush();
103
    }
104
    return  $street;
105
  }
106
107
  public function discoveryDistrict(City $city, string $districtName): District
108
  {
109
    $search = [
110
      'city' => $city
111
    ];
112
113
    $search['district'] = $districtName;
114
115
    $district =  $this->manager->getRepository(District::class)->findOneBy($search);
116
117
    if (!$district) {
118
      $district = new District();
119
      $district->setCity($city);
120
      $district->setDistrict($districtName);
121
      $this->manager->persist($district);
122
      $this->manager->flush();
123
    }
124
    return  $district;
125
  }
126
  public function discoveryCity(State $state, ?string $cityName = null, ?string $cod_ibge = null): City
127
  {
128
    if (!$cityName && !$cod_ibge)
129
      throw new Exception("Need a param to search city", 404);
130
131
    $search = [
132
      'state' => $state
133
    ];
134
    if ($state)
0 ignored issues
show
introduced by
$state is of type ControleOnline\Entity\State, thus it always evaluated to true.
Loading history...
135
      $search['city'] = $cityName;
136
    if ($cod_ibge)
137
      $search['cod_ibge'] = $cod_ibge;
138
139
    $city =  $this->manager->getRepository(City::class)->findOneBy($search);
140
141
    if (!$city) {
142
      $city = new City();
143
      $city->setCity($cityName);
144
      $city->setState($state);
145
      $city->setIbge($cod_ibge);
146
      $this->manager->persist($city);
147
      $this->manager->flush();
148
    }
149
    return  $city;
150
  }
151
  public function discoveryState(Country $country, ?string $uf = null, ?string $stateName = null, ?string $cod_ibge = null): State
152
  {
153
    if (!$uf && !$stateName && !$cod_ibge)
154
      throw new Exception("Need a param to search state", 404);
155
156
    $search = [
157
      'country' => $country
158
    ];
159
    if ($stateName)
160
      $search['state'] = $stateName;
161
    if ($cod_ibge)
162
      $search['cod_ibge'] = $cod_ibge;
163
    if ($uf)
164
      $search['uf'] = $uf;
165
166
    $state = $this->manager->getRepository(State::class)->findOneBy($search);
167
168
169
    if (!$state) {
170
      $state = new State();
171
      $state->setState($stateName);
172
      $state->setIbge($cod_ibge);
173
      $state->setUf($uf);
174
      $state->setCountry($country);
175
      $this->manager->persist($state);
176
      $this->manager->flush();
177
    }
178
    return  $state;
179
  }
180
181
  public function getCountry(string $countryCode): Country
182
  {
183
    return $this->manager->getRepository(Country::class)->findOneBy(['countrycode' => $countryCode]);
184
  }
185
}
186