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.
Completed
Push — master ( 61d647...5c0fd8 )
by Odiseo
08:26
created

ProvinceSearchAction   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 70
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getProvinceName() 0 13 2
A __construct() 0 9 1
A getAddresses() 0 22 3
A __invoke() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Odiseo\SyliusReportPlugin\Controller\Action;
6
7
use FOS\RestBundle\View\View;
8
use FOS\RestBundle\View\ViewHandler;
9
use FOS\RestBundle\View\ViewHandlerInterface;
10
use Odiseo\SyliusReportPlugin\Repository\AddressRepositoryInterface;
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
final class ProvinceSearchAction
18
{
19
    /** @var AddressRepositoryInterface */
20
    private $addressRepository;
21
22
    /** @var RepositoryInterface */
23
    private $provinceRepository;
24
25
    /** @var ViewHandlerInterface */
26
    private $viewHandler;
27
28
    public function __construct(
29
        AddressRepositoryInterface $addressRepository,
30
        RepositoryInterface $provinceRepository,
31
        ViewHandler $viewHandler
32
    )
33
    {
34
        $this->addressRepository = $addressRepository;
35
        $this->provinceRepository = $provinceRepository;
36
        $this->viewHandler = $viewHandler;
37
    }
38
39
    public function __invoke(Request $request): Response
40
    {
41
        $addresses = $this->getAddresses($request->get('province', ''));
42
        $view = View::create($addresses);
43
44
        $this->viewHandler->setExclusionStrategyGroups(['Autocomplete']);
45
        $view->getContext()->enableMaxDepth();
46
47
        return $this->viewHandler->handle($view);
48
    }
49
50
    private function getAddresses($query): array
51
    {
52
        $addresses = [];
53
        $searchAddresses = $this->addressRepository->findByProvinceName($query);
54
55
        /** @var AddressInterface $address */
56
        foreach ($searchAddresses as $address) {
57
            $provinceName = $this->getProvinceName($address);
58
            $provinceLabel = ucfirst(strtolower($provinceName)).', '.$address->getCountryCode();
59
            $isNew = count(array_filter($addresses, function ($address) use ($provinceLabel) {
60
                return $address['province'] === $provinceLabel;
61
            })) === 0;
62
63
            if ($isNew) {
64
                $addresses[] = [
65
                    'province' => $provinceLabel,
66
                    'id' => $address->getId(),
67
                ];
68
            }
69
        }
70
71
        return $addresses;
72
    }
73
74
    private function getProvinceName(AddressInterface $address): string
75
    {
76
        $provinceName = $address->getProvinceName();
77
78
        if (!$provinceName) {
79
            /** @var ProvinceInterface $province */
80
            $province = $this->provinceRepository->findOneBy([
81
                'code' => $address->getProvinceCode(),
82
            ]);
83
            $provinceName = $province->getName();
84
        }
85
86
        return $provinceName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $provinceName could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
87
    }
88
}
89