PluginProbe
Packeta / 1.5.1
Packeta v1.5.1
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Module / Carrier / PacketaPickupPointsConfig.php

PacketaPickupPointsConfig.php in Packeta 1.5.1, at src/Packetery/Module/Carrier/PacketaPickupPointsConfig.php

205 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Packeta pickup points configuration.
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Carrier;
11
12 use Packetery\Core\PickupPointProvider\BaseProvider;
13 use Packetery\Core\PickupPointProvider\CompoundCarrierCollectionFactory;
14 use Packetery\Core\PickupPointProvider\CompoundProvider;
15 use Packetery\Core\PickupPointProvider\VendorCollectionFactory;
16 use Packetery\Core\PickupPointProvider\VendorProvider;
17 use Packetery\Module\Options\FeatureFlagManager;
18
19 /**
20 * Packeta pickup points configuration.
21 *
22 * @package Packetery
23 */
24 class PacketaPickupPointsConfig {
25
26 public const COMPOUND_CARRIER_PREFIX = 'zpoint';
27
28 /**
29 * CompoundCarrierCollectionFactory.
30 *
31 * @var CompoundCarrierCollectionFactory
32 */
33 private $compoundCarrierFactory;
34
35 /**
36 * VendorCollectionFactory.
37 *
38 * @var VendorCollectionFactory
39 */
40 private $vendorCollectionFactory;
41
42 /**
43 * Feature flag.
44 *
45 * @var FeatureFlagManager
46 */
47 private $featureFlag;
48
49 /**
50 * PacketaPickupPointsConfig.
51 *
52 * @param CompoundCarrierCollectionFactory $compoundCarrierFactory CompoundCarrierCollectionFactory.
53 * @param VendorCollectionFactory $vendorCollectionFactory VendorCollectionFactory.
54 * @param FeatureFlagManager $featureFlag Feature flag.
55 */
56 public function __construct(
57 CompoundCarrierCollectionFactory $compoundCarrierFactory,
58 VendorCollectionFactory $vendorCollectionFactory,
59 FeatureFlagManager $featureFlag
60 ) {
61 $this->vendorCollectionFactory = $vendorCollectionFactory;
62 $this->compoundCarrierFactory = $compoundCarrierFactory;
63 $this->featureFlag = $featureFlag;
64 }
65
66 /**
67 * Returns internal pickup points configuration
68 *
69 * @return CompoundProvider[]
70 */
71 public function getCompoundCarriers(): array {
72 $translatedNames = [
73 'zpointcz' => 'CZ ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box, AlzaBox)', 'packeta' ),
74 'zpointsk' => 'SK ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ),
75 'zpointhu' => 'HU ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ),
76 'zpointro' => 'RO ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ),
77 ];
78
79 $indexedCollection = [];
80 $compoundCarrierCollection = $this->compoundCarrierFactory->create();
81 foreach ( $compoundCarrierCollection as $compoundProvider ) {
82 $carrierId = $compoundProvider->getId();
83 assert( isset( $translatedNames[ $carrierId ] ), 'Missing name for carrier id ' . $carrierId );
84 $compoundProvider->setTranslatedName( $translatedNames[ $carrierId ] );
85 // There is only one provider for each country.
86 $indexedCollection[ $compoundProvider->getCountry() ] = $compoundProvider;
87 }
88
89 return $indexedCollection;
90 }
91
92 /**
93 * Gets vendor carriers settings.
94 *
95 * @return VendorProvider[]
96 */
97 public function getVendorCarriers(): array {
98 if ( ! $this->featureFlag->isSplitActive() ) {
99 return [];
100 }
101
102 $translatedNames = [
103 'czzpoint' => 'CZ ' . __( 'Packeta Pick-up Point', 'packeta' ),
104 'czzbox' => 'CZ ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
105 'czalzabox' => 'CZ ' . __( 'AlzaBox via Packeta', 'packeta' ),
106 'skzpoint' => 'SK ' . __( 'Packeta Pick-up Point', 'packeta' ),
107 'skzbox' => 'SK ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
108 'huzpoint' => 'HU ' . __( 'Packeta Pick-up Point', 'packeta' ),
109 'huzbox' => 'HU ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
110 'rozpoint' => 'RO ' . __( 'Packeta Pick-up Point', 'packeta' ),
111 'rozbox' => 'RO ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
112 ];
113
114 $indexedCollection = [];
115 $vendorCollection = $this->vendorCollectionFactory->create();
116 foreach ( $vendorCollection as $vendorProvider ) {
117 $vendorId = $vendorProvider->getId();
118 assert( isset( $translatedNames[ $vendorId ] ), 'Missing name for vendor id ' . $vendorId );
119 $vendorProvider->setTranslatedName( $translatedNames[ $vendorId ] );
120 $indexedCollection[ $vendorId ] = $vendorProvider;
121 }
122
123 return $indexedCollection;
124 }
125
126 /**
127 * Gets all non-feed carriers settings.
128 *
129 * @return BaseProvider[]
130 */
131 public function getCompoundAndVendorCarriers(): array {
132 return array_merge( $this->getCompoundCarriers(), $this->getVendorCarriers() );
133 }
134
135 /**
136 * Gets zpoint carrier id for given country.
137 *
138 * @param string $country Country ISO code.
139 *
140 * @return string|null
141 */
142 public function getCompoundCarrierIdByCountry( string $country ): ?string {
143 $compoundCarriers = $this->getCompoundCarriers();
144
145 if ( ! isset( $compoundCarriers[ $country ] ) ) {
146 return null;
147 }
148
149 return $compoundCarriers[ $country ]->getId();
150 }
151
152 /**
153 * Checks if id is compound carrier id.
154 *
155 * @param string $carrierId Carrier id.
156 *
157 * @return bool
158 */
159 public function isCompoundCarrierId( string $carrierId ): bool {
160 return ( strpos( $carrierId, self::COMPOUND_CARRIER_PREFIX ) === 0 );
161 }
162
163 /**
164 * Checks if provided id is a vendor carrier id.
165 *
166 * @param string $carrierId Carrier id.
167 *
168 * @return bool
169 */
170 public function isVendorCarrierId( string $carrierId ): bool {
171 $vendorCarriers = $this->getVendorCarriers();
172 return isset( $vendorCarriers[ $carrierId ] );
173 }
174
175 /**
176 * Gets non-feed carriers settings by country.
177 *
178 * @param string $country Country.
179 *
180 * @return array
181 */
182 public function getNonFeedCarriersByCountry( string $country ): array {
183 $filteredCarriers = [];
184 $nonFeedCarriers = $this->getCompoundAndVendorCarriers();
185
186 foreach ( $nonFeedCarriers as $nonFeedCarrier ) {
187 if ( $nonFeedCarrier->getCountry() === $country ) {
188 $filteredCarriers[] = $nonFeedCarrier;
189 }
190 }
191
192 return $filteredCarriers;
193 }
194
195 /**
196 * Gets internal countries.
197 *
198 * @return string[]
199 */
200 public function getInternalCountries(): array {
201 return array_keys( $this->getCompoundCarriers() );
202 }
203
204 }
205