PluginProbe
Packeta / 1.5.1
Packeta v1.5.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.5.1, at src/Packetery/Module/Carrier/Downloader.php

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