PluginProbe
Packeta / 2.1
Packeta v2.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 2.1, at src/Packetery/Module/Carrier/PacketaPickupPointsConfig.php

255 lines 7.3 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\Entity;
13 use Packetery\Core\PickupPointProvider\BaseProvider;
14 use Packetery\Core\PickupPointProvider\CompoundCarrierCollectionFactory;
15 use Packetery\Core\PickupPointProvider\CompoundProvider;
16 use Packetery\Core\PickupPointProvider\VendorCollectionFactory;
17 use Packetery\Core\PickupPointProvider\VendorProvider;
18 use Packetery\Module\Exception\InvalidCarrierException;
19 use Packetery\Module\Options\FlagManager\FeatureFlagProvider;
20
21 /**
22 * Packeta pickup points configuration.
23 *
24 * @package Packetery
25 */
26 class PacketaPickupPointsConfig {
27
28 public const COMPOUND_CARRIER_PREFIX = 'zpoint';
29
30 /**
31 * CompoundCarrierCollectionFactory.
32 *
33 * @var CompoundCarrierCollectionFactory
34 */
35 private $compoundCarrierFactory;
36
37 /**
38 * VendorCollectionFactory.
39 *
40 * @var VendorCollectionFactory
41 */
42 private $vendorCollectionFactory;
43
44 /**
45 * Feature flag.
46 *
47 * @var FeatureFlagProvider
48 */
49 private $featureFlagProvider;
50
51 /**
52 * PacketaPickupPointsConfig.
53 *
54 * @param CompoundCarrierCollectionFactory $compoundCarrierFactory CompoundCarrierCollectionFactory.
55 * @param VendorCollectionFactory $vendorCollectionFactory VendorCollectionFactory.
56 * @param FeatureFlagProvider $featureFlagProvider Feature flag.
57 */
58 public function __construct(
59 CompoundCarrierCollectionFactory $compoundCarrierFactory,
60 VendorCollectionFactory $vendorCollectionFactory,
61 FeatureFlagProvider $featureFlagProvider
62 ) {
63 $this->vendorCollectionFactory = $vendorCollectionFactory;
64 $this->compoundCarrierFactory = $compoundCarrierFactory;
65 $this->featureFlagProvider = $featureFlagProvider;
66 }
67
68 /**
69 * Returns internal pickup points configuration
70 *
71 * @return CompoundProvider[]
72 */
73 public function getCompoundCarriers(): array {
74 $translatedNames = [
75 'zpointcz' => 'CZ ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ),
76 'zpointsk' => 'SK ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ),
77 'zpointhu' => 'HU ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ),
78 'zpointro' => 'RO ' . __( 'Packeta Pick-up Point (Z-Point, Z-Box)', 'packeta' ),
79 ];
80
81 $indexedCollection = [];
82 $compoundCarrierCollection = $this->compoundCarrierFactory->create();
83 foreach ( $compoundCarrierCollection as $compoundProvider ) {
84 $carrierId = $compoundProvider->getId();
85 assert( isset( $translatedNames[ $carrierId ] ), 'Missing name for carrier id ' . $carrierId );
86 $compoundProvider->setTranslatedName( $translatedNames[ $carrierId ] );
87 // There is only one provider for each country.
88 $indexedCollection[ $compoundProvider->getCountry() ] = $compoundProvider;
89 }
90
91 return $indexedCollection;
92 }
93
94 /**
95 * Gets vendor groups for compound carrier.
96 *
97 * @param string $carrierId Carrier id.
98 *
99 * @return string[]
100 */
101 public function getCompoundCarrierVendorGroups( string $carrierId ): array {
102 $vendorGroups = [];
103 $vendorCarriers = $this->getVendorCarriers();
104 $compoundCarrierCollection = $this->compoundCarrierFactory->create();
105 foreach ( $compoundCarrierCollection as $compoundProvider ) {
106 if ( $compoundProvider->getId() === $carrierId ) {
107 $vendorCodes = $compoundProvider->getVendorCodes();
108 foreach ( $vendorCodes as $vendorCode ) {
109 $vendorGroups[] = $vendorCarriers[ $vendorCode ]->getGroup();
110 }
111 }
112 }
113
114 return $vendorGroups;
115 }
116
117 /**
118 * Gets vendor carriers settings.
119 *
120 * @param bool $forceReturnVendorCarriers Set true to get vendor carriers if split is disabled.
121 *
122 * @return VendorProvider[]
123 */
124 public function getVendorCarriers( bool $forceReturnVendorCarriers = false ): array {
125 if ( ! $forceReturnVendorCarriers && ! $this->featureFlagProvider->isSplitActive() ) {
126 return [];
127 }
128
129 $translatedNames = [
130 'czzpoint' => 'CZ ' . __( 'Packeta Pick-up Point', 'packeta' ),
131 'czzbox' => 'CZ ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
132 'skzpoint' => 'SK ' . __( 'Packeta Pick-up Point', 'packeta' ),
133 'skzbox' => 'SK ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
134 'huzpoint' => 'HU ' . __( 'Packeta Pick-up Point', 'packeta' ),
135 'huzbox' => 'HU ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
136 'rozpoint' => 'RO ' . __( 'Packeta Pick-up Point', 'packeta' ),
137 'rozbox' => 'RO ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
138 ];
139
140 $indexedCollection = [];
141 $vendorCollection = $this->vendorCollectionFactory->create();
142 foreach ( $vendorCollection as $vendorProvider ) {
143 $vendorId = $vendorProvider->getId();
144 assert( isset( $translatedNames[ $vendorId ] ), 'Missing name for vendor id ' . $vendorId );
145 $vendorProvider->setTranslatedName( $translatedNames[ $vendorId ] );
146 $indexedCollection[ $vendorId ] = $vendorProvider;
147 }
148
149 return $indexedCollection;
150 }
151
152 /**
153 * Gets all non-feed carriers settings.
154 *
155 * @return BaseProvider[]
156 */
157 public function getCompoundAndVendorCarriers(): array {
158 return array_merge( $this->getCompoundCarriers(), $this->getVendorCarriers() );
159 }
160
161 /**
162 * Checks if id is compound carrier id.
163 *
164 * @param string $carrierId Carrier id.
165 *
166 * @return bool
167 */
168 public function isCompoundCarrierId( string $carrierId ): bool {
169 return ( strpos( $carrierId, self::COMPOUND_CARRIER_PREFIX ) === 0 );
170 }
171
172 /**
173 * Checks if provided id is a vendor carrier id.
174 *
175 * @param string $carrierId Carrier id.
176 *
177 * @return bool
178 */
179 public function isVendorCarrierId( string $carrierId ): bool {
180 $vendorCarriers = $this->getVendorCarriers();
181
182 return isset( $vendorCarriers[ $carrierId ] );
183 }
184
185 /**
186 * All internal pickup point carrier ids are strings, including old 'packeta' value.
187 *
188 * @param string $carrierId Carrier id.
189 *
190 * @return bool
191 */
192 public function isInternalPickupPointCarrier( string $carrierId ): bool {
193 return ! is_numeric( $carrierId );
194 }
195
196 /**
197 * Gets non-feed carriers settings by country.
198 *
199 * @param string $country Country.
200 *
201 * @return array<int<0, max>, BaseProvider>
202 */
203 public function getNonFeedCarriersByCountry( string $country ): array {
204 $filteredCarriers = [];
205 $nonFeedCarriers = $this->getCompoundAndVendorCarriers();
206
207 foreach ( $nonFeedCarriers as $nonFeedCarrier ) {
208 if ( $nonFeedCarrier->getCountry() === $country ) {
209 $filteredCarriers[] = $nonFeedCarrier;
210 }
211 }
212
213 return $filteredCarriers;
214 }
215
216 /**
217 * Gets internal countries.
218 *
219 * @return string[]
220 */
221 public function getInternalCountries(): array {
222 return array_keys( $this->getCompoundCarriers() );
223 }
224
225 /**
226 * Changes previously used identifier 'packeta' to zpoint type id.
227 * Returns one of ids from CompoundCarrierCollectionFactory, e.g. zpointcz.
228 *
229 * @param string $carrierId Carrier id.
230 * @param string $country Lowercase country.
231 *
232 * @return string
233 * @throws InvalidCarrierException InvalidCarrierException.
234 */
235 public function getFixedCarrierId( string $carrierId, string $country ): string {
236 if ( $carrierId === Entity\Carrier::INTERNAL_PICKUP_POINTS_ID ) {
237 $compoundCarriers = $this->getCompoundCarriers();
238
239 if ( ! isset( $compoundCarriers[ $country ] ) ) {
240 throw new InvalidCarrierException(
241 sprintf(
242 // translators: %s is country code.
243 __( 'Selected carrier does not deliver to country "%s".', 'packeta' ),
244 $country
245 )
246 );
247 }
248
249 return $compoundCarriers[ $country ]->getId();
250 }
251
252 return $carrierId;
253 }
254 }
255