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; |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|