PluginProbe
Packeta / 1.3.1
Packeta v1.3.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 / Downloader.php

Downloader.php in Packeta 1.3.1, at src/Packetery/Module/Carrier/Downloader.php

163 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Packeta carrier downloader
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Module\Carrier;
11
12 use Packetery\Module\Options\Provider;
13 use PacketeryGuzzleHttp\Client;
14 use PacketeryGuzzleHttp\Exception\GuzzleException;
15 use PacketeryGuzzleHttp\PacketeryPsr7\Response;
16
17 /**
18 * Class Downloader
19 *
20 * @package Packetery
21 */
22 class Downloader {
23 private const API_URL = 'https://www.zasilkovna.cz/api/v4/%s/branch.json?address-delivery';
24 public const OPTION_LAST_CARRIER_UPDATE = 'packetery_last_carrier_update';
25
26 /**
27 * Guzzle client.
28 *
29 * @var Client Guzzle client.
30 */
31 private $client;
32
33 /**
34 * Carrier updater.
35 *
36 * @var Updater Carrier updater.
37 */
38 private $carrier_updater;
39
40 /**
41 * Options provider.
42 *
43 * @var Provider Options provider.
44 */
45 private $options_provider;
46
47 /**
48 * Downloader constructor.
49 *
50 * @param Client $guzzle_client Guzzle client.
51 * @param Updater $carrier_updater Carrier updater.
52 * @param Provider $options_provider Options provider.
53 */
54 public function __construct( Client $guzzle_client, Updater $carrier_updater, Provider $options_provider ) {
55 $this->client = $guzzle_client;
56 $this->carrier_updater = $carrier_updater;
57 $this->options_provider = $options_provider;
58 }
59
60 /**
61 * Runs update and returns result.
62 *
63 * @return array
64 */
65 public function run(): array {
66 try {
67 $carriers = $this->fetch_as_array();
68 } catch ( \Exception $e ) {
69 return [
70 strtr(
71 // translators: keep %failReason placeholder intact.
72 __( 'Carrier download failed: %failReason Please try again later.', 'packeta' ),
73 array( '%failReason' => $e->getMessage() )
74 ),
75 'error',
76 ];
77 }
78 if ( ! $carriers ) {
79 return [
80 strtr(
81 // translators: keep %failReason placeholder intact.
82 __( 'Carrier download failed: %failReason Please try again later.', 'packeta' ),
83 array( '%failReason' => __( 'Failed to get the list.', 'packeta' ) )
84 ),
85 'error',
86 ];
87 }
88 $validation_result = $this->carrier_updater->validate_carrier_data( $carriers );
89 if ( ! $validation_result ) {
90 return [
91 strtr(
92 // translators: keep %failReason placeholder intact.
93 __( 'Carrier download failed: %failReason Please try again later.', 'packeta' ),
94 array( '%failReason' => __( 'Invalid API response.', 'packeta' ) )
95 ),
96 'error',
97 ];
98 }
99 $this->carrier_updater->save( $carriers );
100 update_option( self::OPTION_LAST_CARRIER_UPDATE, gmdate( DATE_ATOM ) );
101
102 return [
103 __( 'Carriers were updated.', 'packeta' ),
104 'success',
105 ];
106 }
107
108 /**
109 * Cron job. No authorization needed - job is registered internally.
110 */
111 public function runAndRender(): void {
112 [ $message, $class ] = $this->run();
113 echo esc_html( $message );
114 }
115
116 /**
117 * Downloads carriers and returns in array.
118 *
119 * @return array|null
120 * @throws DownloadException DownloadException.
121 */
122 private function fetch_as_array(): ?array {
123 $json = $this->download_json();
124
125 return $this->get_from_json( $json );
126 }
127
128 /**
129 * Downloads carriers in JSON.
130 *
131 * @return string
132 * @throws DownloadException DownloadException.
133 */
134 private function download_json(): string {
135 $url = sprintf( self::API_URL, $this->options_provider->get_api_key() );
136 try {
137 /**
138 * Guzzle response.
139 *
140 * @var Response $result Guzzle response.
141 */
142 $result = $this->client->get( $url );
143 } catch ( GuzzleException $exception ) {
144 throw new DownloadException( $exception->getMessage() );
145 }
146
147 return $result->getBody()->getContents();
148 }
149
150 /**
151 * Converts JSON to array.
152 *
153 * @param string $json JSON.
154 *
155 * @return array|null
156 */
157 private function get_from_json( string $json ): ?array {
158 $carriers_data = json_decode( $json, true );
159
160 return ( $carriers_data['carriers'] ?? null );
161 }
162 }
163