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

231 lines 6.5 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\FeatureFlagManager;
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 FeatureFlagManager
48 */
49 private $featureFlag;
50
51 /**
52 * PacketaPickupPointsConfig.
53 *
54 * @param CompoundCarrierCollectionFactory $compoundCarrierFactory CompoundCarrierCollectionFactory.
55 * @param VendorCollectionFactory $vendorCollectionFactory VendorCollectionFactory.
56 * @param FeatureFlagManager $featureFlag Feature flag.
57 */
58 public function __construct(
59 CompoundCarrierCollectionFactory $compoundCarrierFactory,
60 VendorCollectionFactory $vendorCollectionFactory,
61 FeatureFlagManager $featureFlag
62 ) {
63 $this->vendorCollectionFactory = $vendorCollectionFactory;
64 $this->compoundCarrierFactory = $compoundCarrierFactory;
65 $this->featureFlag = $featureFlag;
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, AlzaBox)', '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 carriers settings.
96 *
97 * @return VendorProvider[]
98 */
99 public function getVendorCarriers(): array {
100 if ( ! $this->featureFlag->isSplitActive() ) {
101 return [];
102 }
103
104 $translatedNames = [
105 'czzpoint' => 'CZ ' . __( 'Packeta Pick-up Point', 'packeta' ),
106 'czzbox' => 'CZ ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
107 'czalzabox' => 'CZ ' . __( 'AlzaBox via Packeta', 'packeta' ),
108 'skzpoint' => 'SK ' . __( 'Packeta Pick-up Point', 'packeta' ),
109 'skzbox' => 'SK ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
110 'huzpoint' => 'HU ' . __( 'Packeta Pick-up Point', 'packeta' ),
111 'huzbox' => 'HU ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
112 'rozpoint' => 'RO ' . __( 'Packeta Pick-up Point', 'packeta' ),
113 'rozbox' => 'RO ' . __( 'Packeta', 'packeta' ) . ' Z-BOX',
114 ];
115
116 $indexedCollection = [];
117 $vendorCollection = $this->vendorCollectionFactory->create();
118 foreach ( $vendorCollection as $vendorProvider ) {
119 $vendorId = $vendorProvider->getId();
120 assert( isset( $translatedNames[ $vendorId ] ), 'Missing name for vendor id ' . $vendorId );
121 $vendorProvider->setTranslatedName( $translatedNames[ $vendorId ] );
122 $indexedCollection[ $vendorId ] = $vendorProvider;
123 }
124
125 return $indexedCollection;
126 }
127
128 /**
129 * Gets all non-feed carriers settings.
130 *
131 * @return BaseProvider[]
132 */
133 public function getCompoundAndVendorCarriers(): array {
134 return array_merge( $this->getCompoundCarriers(), $this->getVendorCarriers() );
135 }
136
137 /**
138 * Checks if id is compound carrier id.
139 *
140 * @param string $carrierId Carrier id.
141 *
142 * @return bool
143 */
144 public function isCompoundCarrierId( string $carrierId ): bool {
145 return ( strpos( $carrierId, self::COMPOUND_CARRIER_PREFIX ) === 0 );
146 }
147
148 /**
149 * Checks if provided id is a vendor carrier id.
150 *
151 * @param string $carrierId Carrier id.
152 *
153 * @return bool
154 */
155 public function isVendorCarrierId( string $carrierId ): bool {
156 $vendorCarriers = $this->getVendorCarriers();
157 return isset( $vendorCarriers[ $carrierId ] );
158 }
159
160 /**
161 * All internal pickup point carrier ids are strings, including old 'packeta' value.
162 *
163 * @param string $carrierId Carrier id.
164 *
165 * @return bool
166 */
167 public function isInternalPickupPointCarrier( string $carrierId ): bool {
168 return ! is_numeric( $carrierId );
169 }
170
171 /**
172 * Gets non-feed carriers settings by country.
173 *
174 * @param string $country Country.
175 *
176 * @return array
177 */
178 public function getNonFeedCarriersByCountry( string $country ): array {
179 $filteredCarriers = [];
180 $nonFeedCarriers = $this->getCompoundAndVendorCarriers();
181
182 foreach ( $nonFeedCarriers as $nonFeedCarrier ) {
183 if ( $nonFeedCarrier->getCountry() === $country ) {
184 $filteredCarriers[] = $nonFeedCarrier;
185 }
186 }
187
188 return $filteredCarriers;
189 }
190
191 /**
192 * Gets internal countries.
193 *
194 * @return string[]
195 */
196 public function getInternalCountries(): array {
197 return array_keys( $this->getCompoundCarriers() );
198 }
199
200 /**
201 * Changes previously used identifier 'packeta' to zpoint type id.
202 * Returns one of ids from CompoundCarrierCollectionFactory, e.g. zpointcz.
203 *
204 * @param string $carrierId Carrier id.
205 * @param string $country Lowercase country.
206 *
207 * @return string|null Null in case of split vendor when split is off.
208 * @throws InvalidCarrierException InvalidCarrierException.
209 */
210 public function getFixedCarrierId( string $carrierId, string $country ): string {
211 if ( Entity\Carrier::INTERNAL_PICKUP_POINTS_ID === $carrierId ) {
212 $compoundCarriers = $this->getCompoundCarriers();
213
214 if ( ! isset( $compoundCarriers[ $country ] ) ) {
215 throw new InvalidCarrierException(
216 sprintf(
217 // translators: %s is country code.
218 __( 'Selected carrier does not deliver to country "%s".', 'packeta' ),
219 $country
220 )
221 );
222 }
223
224 return $compoundCarriers[ $country ]->getId();
225 }
226
227 return $carrierId;
228 }
229
230 }
231