1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file was created by developers working at BitBag |
||
5 | * Do you need more information about us and what we do? Visit our https://bitbag.io website! |
||
6 | * We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
||
7 | */ |
||
8 | |||
9 | declare(strict_types=1); |
||
10 | |||
11 | namespace BitBag\SyliusShippingExportPlugin\Context; |
||
12 | |||
13 | use BitBag\SyliusShippingExportPlugin\Entity\ShippingGatewayInterface; |
||
14 | use BitBag\SyliusShippingExportPlugin\Exception\ShippingGatewayLabelNotFound; |
||
15 | use BitBag\SyliusShippingExportPlugin\Exception\ShippingGatewayNotFoundException; |
||
16 | use BitBag\SyliusShippingExportPlugin\Repository\ShippingGatewayRepositoryInterface; |
||
17 | use Sylius\Bundle\ResourceBundle\Form\Registry\FormTypeRegistryInterface; |
||
18 | use Symfony\Component\HttpFoundation\Request; |
||
19 | use Symfony\Component\HttpFoundation\RequestStack; |
||
20 | |||
21 | final class ShippingGatewayContext implements ShippingGatewayContextInterface |
||
22 | { |
||
23 | /** @var RequestStack */ |
||
24 | private $requestStack; |
||
25 | |||
26 | /** @var FormTypeRegistryInterface */ |
||
27 | private $shippingGatewayFormTypeRegistry; |
||
28 | |||
29 | /** @var ShippingGatewayRepositoryInterface */ |
||
30 | private $shippingGatewayRepository; |
||
31 | |||
32 | /** @var array */ |
||
33 | private $shippingGateways; |
||
34 | |||
35 | public function __construct( |
||
36 | RequestStack $requestStack, |
||
37 | FormTypeRegistryInterface $shippingGatewayFormTypeRegistry, |
||
38 | ShippingGatewayRepositoryInterface $shippingGatewayRepository, |
||
39 | array $shippingGateways |
||
40 | ) { |
||
41 | $this->requestStack = $requestStack; |
||
42 | $this->shippingGatewayFormTypeRegistry = $shippingGatewayFormTypeRegistry; |
||
43 | $this->shippingGatewayRepository = $shippingGatewayRepository; |
||
44 | $this->shippingGateways = $shippingGateways; |
||
45 | } |
||
46 | |||
47 | public function getFormType(): ?string |
||
48 | { |
||
49 | $code = $this->getCode(); |
||
50 | if (null === $code) { |
||
51 | return null; |
||
52 | } |
||
53 | |||
54 | return $this->shippingGatewayFormTypeRegistry->get('shipping_gateway_config', $code); |
||
55 | } |
||
56 | |||
57 | public function getCode(): ?string |
||
58 | { |
||
59 | /** @var Request $request */ |
||
60 | $request = $this->requestStack->getCurrentRequest(); |
||
61 | $id = $request->get('id'); |
||
62 | |||
63 | if (null !== $id) { |
||
64 | return $this->getExistingShippingGateway((int) $id)->getCode(); |
||
65 | } |
||
66 | $code = $request->get('code'); |
||
67 | |||
68 | if (false === $this->shippingGatewayFormTypeRegistry->has('shipping_gateway_config', $code)) { |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
69 | throw new ShippingGatewayNotFoundException(sprintf( |
||
70 | 'Gateway with %s code could not be found', |
||
71 | $code |
||
72 | )); |
||
73 | } |
||
74 | |||
75 | return $code; |
||
76 | } |
||
77 | |||
78 | public function getLabelByCode(?string $code): ?string |
||
79 | { |
||
80 | foreach ($this->shippingGateways as $shippingGatewayCode => $shippingGatewayLabel) { |
||
81 | if ($code === $shippingGatewayCode) { |
||
82 | return $shippingGatewayLabel; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | throw new ShippingGatewayLabelNotFound($code); |
||
87 | } |
||
88 | |||
89 | private function getExistingShippingGateway(int $id): ShippingGatewayInterface |
||
90 | { |
||
91 | /** @var ShippingGatewayInterface|null $shippingGateway */ |
||
92 | $shippingGateway = $this->shippingGatewayRepository->find($id); |
||
93 | if (false === $shippingGateway instanceof ShippingGatewayInterface) { |
||
94 | throw new ShippingGatewayNotFoundException(sprintf( |
||
95 | 'Gateway with %d id could not be found in the database.', |
||
96 | $id |
||
97 | )); |
||
98 | } |
||
99 | |||
100 | return $shippingGateway; |
||
101 | } |
||
102 | } |
||
103 |