Files
2026-04-28 15:13:50 +02:00

109 lines
2.4 KiB
PHP

<?php
/**
* ShipX Points
*
* @package WooCommerce Paczkomaty InPost
*/
/**
* Can get points from API.
*/
class WPDesk_Paczkomaty_ShipX_Points {
const MAX_PER_PAGE = 500;
/**
* @var WPDesk_Paczkomaty_ShipX
*/
private $api_client;
/**
* @param WPDesk_Paczkomaty_ShipX $api_client .
*/
public function __construct( $api_client ) {
$this->api_client = $api_client;
}
/**
* @param stdClass $item .
* @param string $field_name .
*
* @return stdClass
*/
private function unset_item_field( $item, $field_name ) {
unset( $item->{$field_name} );
return $item;
}
/**
* @param stdClass $item .
*
* @return stdClass
*/
private function unset_item_unnecessary_data( $item ) {
foreach ( $item as $property => $value ) {
if ( ! in_array( $property, [ 'name', 'type', 'address', 'payment_available' ], true ) ) {
$item = $this->unset_item_field( $item, $property );
}
}
return $item;
}
/**
* Get points from api.
*
* @param string $type .
*
* @return array
* @throws Exception|WPDesk_Paczkomaty_ShipX_Exception .
*/
public function get_points_from_api( $type = 'parcel_locker' ) {
$params = [
'per_page' => self::MAX_PER_PAGE,
];
if ( $type ) {
$params['type'] = $type;
}
$get_points = true;
while ( $get_points ) {
$get_points = false;
try {
$api_points = $this->api_client->get_points( $params );
} catch ( Exception $e ) {
$per_page = $params['per_page'];
if ( self::MAX_PER_PAGE === $per_page && WPDesk_Paczkomaty_ShipX::API_PER_PAGE !== $per_page ) {
$params['per_page'] = WPDesk_Paczkomaty_ShipX::API_PER_PAGE;
$api_points = $this->api_client->get_points( $params );
} else {
throw $e;
}
}
foreach ( $api_points->items as $item ) {
yield $this->unset_item_unnecessary_data( $item );
}
if ( $api_points->page < $api_points->total_pages ) {
$get_points = true;
$params['page'] = intval( $api_points->page ) + 1;
}
unset( $api_points );
}
}
/**
* Returns point data from API.
*
* @see https://docs.inpost24.com/display/PL/%5B1.5.2%5D+Points#id-[1.5.2]Points-Informationaboutpoints
*
* @param string $point_name .
*
* @return stdClass
* @throws Exception .
* @throws WPDesk_Paczkomaty_ShipX_Exception .
*/
public function get_point_from_api( $point_name ) {
return $this->api_client->get_point( $point_name );
}
}