GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ProvinceSearchAction::getProvinceName()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusReportPlugin\Controller\Action;
6
7
use FOS\RestBundle\View\ConfigurableViewHandlerInterface;
8
use FOS\RestBundle\View\View;
9
use Odiseo\SyliusReportPlugin\Repository\AddressRepositoryInterface;
10
use Sylius\Component\Addressing\Model\CountryInterface;
11
use Sylius\Component\Addressing\Model\ProvinceInterface;
12
use Sylius\Component\Core\Model\AddressInterface;
13
use Sylius\Component\Resource\Repository\RepositoryInterface;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @author Odiseo Team <[email protected]>
19
 */
20
final class ProvinceSearchAction
21
{
22
    private AddressRepositoryInterface $addressRepository;
23
24
    private RepositoryInterface $provinceRepository;
25
26
    private RepositoryInterface $countryRepository;
27
28
    private ConfigurableViewHandlerInterface $viewHandler;
29
30
    public function __construct(
31
        AddressRepositoryInterface $addressRepository,
32
        RepositoryInterface $provinceRepository,
33
        RepositoryInterface $countryRepository,
34
        ConfigurableViewHandlerInterface $viewHandler
35
    ) {
36
        $this->addressRepository = $addressRepository;
37
        $this->provinceRepository = $provinceRepository;
38
        $this->countryRepository = $countryRepository;
39
        $this->viewHandler = $viewHandler;
40
    }
41
42
    public function __invoke(Request $request): Response
43
    {
44
        $addresses = $this->getAddresses($request->get('province', ''));
45
        $view = View::create($addresses);
46
47
        $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']);
48
        $view->getContext()->enableMaxDepth();
49
50
        return $this->viewHandler->handle($view);
51
    }
52
53
    private function getAddresses(string $query): array
54
    {
55
        $addresses = [];
56
        $searchAddresses = $this->addressRepository->findByProvinceName($query);
57
58
        /** @var AddressInterface $address */
59
        foreach ($searchAddresses as $address) {
60
            /** @var CountryInterface|null $country */
61
            $country = $this->countryRepository->findOneBy([
62
                'code' => $address->getCountryCode()
63
            ]);
64
65
            $countryName = $country !== null ? $country->getName() : $address->getCountryCode();
66
67
            $provinceName = $this->getProvinceName($address);
68
            $provinceLabel = ucfirst(strtolower($provinceName ?? '')).', '.$countryName;
69
            $isNew = count(array_filter($addresses, function ($address) use ($provinceLabel): bool {
70
                return $address['province'] === $provinceLabel;
71
            })) === 0;
72
73
            if ($isNew) {
74
                $addresses[] = [
75
                    'province' => $provinceLabel,
76
                    'id' => $address->getId(),
77
                ];
78
            }
79
        }
80
81
        return $addresses;
82
    }
83
84
    private function getProvinceName(AddressInterface $address): ?string
85
    {
86
        $provinceName = $address->getProvinceName();
87
88
        if (!$provinceName) {
89
            /** @var ProvinceInterface $province */
90
            $province = $this->provinceRepository->findOneBy([
91
                'code' => $address->getProvinceCode(),
92
            ]);
93
            $provinceName = $province->getName();
94
        }
95
96
        return $provinceName;
97
    }
98
}
99