| 1 |
<?php |
| 2 |
/** |
| 3 |
* Packeta carrier repository |
| 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\Module\EntityFactory; |
| 14 |
|
| 15 |
/** |
| 16 |
* Class CarrierRepository |
| 17 |
* TODO: cache - some queries may run more times during request. |
| 18 |
* |
| 19 |
* @package Packetery |
| 20 |
*/ |
| 21 |
class Repository { |
| 22 |
|
| 23 |
public const INTERNAL_PICKUP_POINTS_ID = 'packeta'; |
| 24 |
|
| 25 |
/** |
| 26 |
* WordPress wpdb object from global |
| 27 |
* |
| 28 |
* @var \wpdb |
| 29 |
*/ |
| 30 |
private $wpdb; |
| 31 |
|
| 32 |
/** |
| 33 |
* Carrier Entity Factory. |
| 34 |
* |
| 35 |
* @var EntityFactory\Carrier |
| 36 |
*/ |
| 37 |
private $carrierEntityFactory; |
| 38 |
|
| 39 |
/** |
| 40 |
* Repository constructor. |
| 41 |
* |
| 42 |
* @param \wpdb $wpdb wpdb. |
| 43 |
* @param EntityFactory\Carrier $carrierEntityFactory Carrier Entity Factory. |
| 44 |
*/ |
| 45 |
public function __construct( \wpdb $wpdb, EntityFactory\Carrier $carrierEntityFactory ) { |
| 46 |
$this->wpdb = $wpdb; |
| 47 |
$this->carrierEntityFactory = $carrierEntityFactory; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Gets wpdb object from global variable with custom tables names set. |
| 52 |
* |
| 53 |
* @return \wpdb |
| 54 |
*/ |
| 55 |
private function get_wpdb(): \wpdb { |
| 56 |
return $this->wpdb; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Create table to store carriers. |
| 61 |
* |
| 62 |
* @return bool |
| 63 |
*/ |
| 64 |
public function createTable(): bool { |
| 65 |
$wpdb = $this->get_wpdb(); |
| 66 |
return $wpdb->query( |
| 67 |
'CREATE TABLE IF NOT EXISTS `' . $wpdb->packetery_carrier . '` ( |
| 68 |
`id` int NOT NULL, |
| 69 |
`name` varchar(255) NOT NULL, |
| 70 |
`is_pickup_points` boolean NOT NULL, |
| 71 |
`has_carrier_direct_label` boolean NOT NULL, |
| 72 |
`separate_house_number` boolean NOT NULL, |
| 73 |
`customs_declarations` boolean NOT NULL, |
| 74 |
`requires_email` boolean NOT NULL, |
| 75 |
`requires_phone` boolean NOT NULL, |
| 76 |
`requires_size` boolean NOT NULL, |
| 77 |
`disallows_cod` boolean NOT NULL, |
| 78 |
`country` varchar(255) NOT NULL, |
| 79 |
`currency` varchar(255) NOT NULL, |
| 80 |
`max_weight` float NOT NULL, |
| 81 |
`deleted` boolean NOT NULL, |
| 82 |
PRIMARY KEY (`id`) |
| 83 |
) ' . $wpdb->get_charset_collate() |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Drop table used to store carriers. |
| 89 |
*/ |
| 90 |
public function drop(): void { |
| 91 |
$wpdb = $this->get_wpdb(); |
| 92 |
$wpdb->query( 'DROP TABLE IF EXISTS `' . $wpdb->packetery_carrier . '`' ); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Gets known carrier ids. |
| 97 |
* |
| 98 |
* @return array|null |
| 99 |
*/ |
| 100 |
public function get_carrier_ids(): ?array { |
| 101 |
$wpdb = $this->get_wpdb(); |
| 102 |
|
| 103 |
return $wpdb->get_results( 'SELECT `id` FROM `' . $wpdb->packetery_carrier . '`', ARRAY_A ); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Gets all active carriers including internal pickup point carriers. |
| 108 |
* |
| 109 |
* @return array|null |
| 110 |
*/ |
| 111 |
public function getAllIncludingZpoints(): ?array { |
| 112 |
$wpdb = $this->get_wpdb(); |
| 113 |
|
| 114 |
$carriers = $wpdb->get_results( 'SELECT `id`, `name`, `is_pickup_points` FROM `' . $wpdb->packetery_carrier . '`', ARRAY_A ); |
| 115 |
$zpointCarriers = $this->getZpointCarriers(); |
| 116 |
foreach ( $zpointCarriers as $zpointCarrier ) { |
| 117 |
array_unshift( $carriers, $zpointCarrier ); |
| 118 |
} |
| 119 |
|
| 120 |
return $carriers; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Gets is_pickup_point attribute of a carrier. |
| 125 |
* |
| 126 |
* @param int $carrierId Carrier id. |
| 127 |
* |
| 128 |
* @return bool |
| 129 |
*/ |
| 130 |
public function hasPickupPoints( int $carrierId ): bool { |
| 131 |
$wpdb = $this->get_wpdb(); |
| 132 |
|
| 133 |
return (bool) $wpdb->get_var( $wpdb->prepare( 'SELECT `is_pickup_points` FROM `' . $wpdb->packetery_carrier . '` WHERE `id` = %d', $carrierId ) ); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Gets Carrier value object by id. |
| 138 |
* |
| 139 |
* @param int $carrierId Carrier id. |
| 140 |
* |
| 141 |
* @return Entity\Carrier|null |
| 142 |
*/ |
| 143 |
public function getById( int $carrierId ): ?Entity\Carrier { |
| 144 |
$wpdb = $this->get_wpdb(); |
| 145 |
$result = $wpdb->get_row( |
| 146 |
$wpdb->prepare( |
| 147 |
'SELECT |
| 148 |
`id`, |
| 149 |
`name`, |
| 150 |
`is_pickup_points`, |
| 151 |
`has_carrier_direct_label`, |
| 152 |
`separate_house_number`, |
| 153 |
`customs_declarations`, |
| 154 |
`requires_email`, |
| 155 |
`requires_phone`, |
| 156 |
`requires_size`, |
| 157 |
`disallows_cod`, |
| 158 |
`country`, |
| 159 |
`currency`, |
| 160 |
`max_weight`, |
| 161 |
`deleted` |
| 162 |
FROM `' . $wpdb->packetery_carrier . '` WHERE `id` = %s', |
| 163 |
$carrierId |
| 164 |
), |
| 165 |
ARRAY_A |
| 166 |
); |
| 167 |
if ( null === $result ) { |
| 168 |
return null; |
| 169 |
} |
| 170 |
|
| 171 |
return $this->carrierEntityFactory->fromDbResult( $result ); |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Gets all active carriers for a country. |
| 176 |
* |
| 177 |
* @param string $country ISO code. |
| 178 |
* |
| 179 |
* @return Entity\Carrier[] |
| 180 |
*/ |
| 181 |
public function getByCountry( string $country ): array { |
| 182 |
$wpdb = $this->get_wpdb(); |
| 183 |
|
| 184 |
$entities = []; |
| 185 |
$countryCarriers = $wpdb->get_results( |
| 186 |
$wpdb->prepare( |
| 187 |
'SELECT |
| 188 |
`id`, |
| 189 |
`name`, |
| 190 |
`is_pickup_points`, |
| 191 |
`has_carrier_direct_label`, |
| 192 |
`separate_house_number`, |
| 193 |
`customs_declarations`, |
| 194 |
`requires_email`, |
| 195 |
`requires_phone`, |
| 196 |
`requires_size`, |
| 197 |
`disallows_cod`, |
| 198 |
`country`, |
| 199 |
`currency`, |
| 200 |
`max_weight`, |
| 201 |
`deleted` |
| 202 |
FROM `' . $wpdb->packetery_carrier . '` WHERE `country` = %s AND `deleted` = false', |
| 203 |
$country |
| 204 |
), |
| 205 |
ARRAY_A |
| 206 |
); |
| 207 |
|
| 208 |
foreach ( $countryCarriers as $carrierData ) { |
| 209 |
$entities[] = $this->carrierEntityFactory->fromDbResult( $carrierData ); |
| 210 |
} |
| 211 |
|
| 212 |
return $entities; |
| 213 |
} |
| 214 |
|
| 215 |
/** |
| 216 |
* Gets all active carriers for a country including internal pickup point carriers. |
| 217 |
* |
| 218 |
* @param string $country ISO code. |
| 219 |
* |
| 220 |
* @return Entity\Carrier[] |
| 221 |
*/ |
| 222 |
public function getByCountryIncludingZpoints( string $country ): array { |
| 223 |
$countryCarriers = $this->getByCountry( $country ); |
| 224 |
$zpointCarriers = $this->getZpointCarriers(); |
| 225 |
if ( ! empty( $zpointCarriers[ $country ] ) ) { |
| 226 |
$zpointCarrierData = $zpointCarriers[ $country ]; |
| 227 |
$zpointCarrierData['country'] = $country; |
| 228 |
$zpointCarrier = $this->carrierEntityFactory->fromZpointCarrierData( $zpointCarrierData ); |
| 229 |
array_unshift( $countryCarriers, $zpointCarrier ); |
| 230 |
} |
| 231 |
|
| 232 |
return $countryCarriers; |
| 233 |
} |
| 234 |
|
| 235 |
/** |
| 236 |
* Gets all active countries. |
| 237 |
* |
| 238 |
* @return array |
| 239 |
*/ |
| 240 |
public function getCountries(): array { |
| 241 |
$wpdb = $this->get_wpdb(); |
| 242 |
$countries = $wpdb->get_results( 'SELECT `country` FROM `' . $wpdb->packetery_carrier . '` WHERE `deleted` = false GROUP BY `country` ORDER BY `country`', ARRAY_A ); |
| 243 |
|
| 244 |
return array_column( ( $countries ? $countries : [] ), 'country' ); |
| 245 |
} |
| 246 |
|
| 247 |
/** |
| 248 |
* Set those not in feed as deleted. |
| 249 |
* |
| 250 |
* @param array $carriers_in_feed Carriers in feed. |
| 251 |
*/ |
| 252 |
public function set_others_as_deleted( array $carriers_in_feed ): void { |
| 253 |
$wpdb = $this->get_wpdb(); |
| 254 |
$wpdb->query( |
| 255 |
'UPDATE `' . |
| 256 |
$wpdb->packetery_carrier . |
| 257 |
'` SET `deleted` = 1 WHERE `id` NOT IN (' . |
| 258 |
// @codingStandardsIgnoreStart |
| 259 |
implode( ',', $carriers_in_feed ) |
| 260 |
// @codingStandardsIgnoreEnd |
| 261 |
. ')' |
| 262 |
); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Inserts carrier data to db. |
| 267 |
* |
| 268 |
* @param array $data Carrier data. |
| 269 |
*/ |
| 270 |
public function insert( array $data ): void { |
| 271 |
$wpdb = $this->get_wpdb(); |
| 272 |
$wpdb->insert( $wpdb->packetery_carrier, $data ); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Updates carrier data in db. |
| 277 |
* |
| 278 |
* @param array $data Carrier data. |
| 279 |
* @param int $carrier_id Carrier id. |
| 280 |
*/ |
| 281 |
public function update( array $data, int $carrier_id ): void { |
| 282 |
$wpdb = $this->get_wpdb(); |
| 283 |
$wpdb->update( $wpdb->packetery_carrier, $data, array( 'id' => $carrier_id ) ); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* Returns internal pickup points configuration |
| 288 |
* |
| 289 |
* @return array[] |
| 290 |
*/ |
| 291 |
public function getZpointCarriers(): array { |
| 292 |
return [ |
| 293 |
'cz' => [ |
| 294 |
'id' => 'zpointcz', |
| 295 |
'name' => __( 'CZ Packeta pickup points', 'packeta' ), |
| 296 |
'is_pickup_points' => 1, |
| 297 |
'currency' => 'CZK', |
| 298 |
], |
| 299 |
'sk' => [ |
| 300 |
'id' => 'zpointsk', |
| 301 |
'name' => __( 'SK Packeta pickup points', 'packeta' ), |
| 302 |
'is_pickup_points' => 1, |
| 303 |
'currency' => 'EUR', |
| 304 |
], |
| 305 |
'hu' => [ |
| 306 |
'id' => 'zpointhu', |
| 307 |
'name' => __( 'HU Packeta pickup points', 'packeta' ), |
| 308 |
'is_pickup_points' => 1, |
| 309 |
'currency' => 'HUF', |
| 310 |
], |
| 311 |
'ro' => [ |
| 312 |
'id' => 'zpointro', |
| 313 |
'name' => __( 'RO Packeta pickup points', 'packeta' ), |
| 314 |
'is_pickup_points' => 1, |
| 315 |
'currency' => 'RON', |
| 316 |
], |
| 317 |
]; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Checks if chosen carrier has pickup points and sets carrier id in provided array. |
| 322 |
* |
| 323 |
* @param string $carrierId Carrier id. |
| 324 |
* |
| 325 |
* @return bool |
| 326 |
*/ |
| 327 |
public function isPickupPointCarrier( string $carrierId ): bool { |
| 328 |
if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) { |
| 329 |
return true; |
| 330 |
} |
| 331 |
|
| 332 |
return $this->hasPickupPoints( (int) $carrierId ); |
| 333 |
} |
| 334 |
|
| 335 |
/** |
| 336 |
* Checks if carrier is home delivery carrier. |
| 337 |
* |
| 338 |
* @param string $carrierId Carrier ID. |
| 339 |
* |
| 340 |
* @return bool |
| 341 |
*/ |
| 342 |
public function isHomeDeliveryCarrier( string $carrierId ): bool { |
| 343 |
if ( self::INTERNAL_PICKUP_POINTS_ID === $carrierId ) { |
| 344 |
return false; |
| 345 |
} |
| 346 |
|
| 347 |
return false === $this->hasPickupPoints( (int) $carrierId ); |
| 348 |
} |
| 349 |
|
| 350 |
} |
| 351 |
|