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 / Downloader.php

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

166 lines 3.9 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 Packetery\GuzzleHttp\Client;
15 use Packetery\GuzzleHttp\Exception\GuzzleException;
16 use Packetery\GuzzleHttp\Psr7\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 // translators: keep %failReason placeholder intact.
81 $translatedMessage = __( 'Carrier download failed: %failReason Please try again later.', 'packeta' );
82 return [
83 strtr(
84 $translatedMessage,
85 array( '%failReason' => __( 'Failed to get the list.', 'packeta' ) )
86 ),
87 'error',
88 ];
89 }
90 $validation_result = $this->carrier_updater->validate_carrier_data( $carriers );
91 if ( ! $validation_result ) {
92 // translators: keep %failReason placeholder intact.
93 $translatedMessage = __( 'Carrier download failed: %failReason Please try again later.', 'packeta' );
94 return [
95 strtr(
96 $translatedMessage,
97 array( '%failReason' => __( 'Invalid API response.', 'packeta' ) )
98 ),
99 'error',
100 ];
101 }
102 $this->carrier_updater->save( $carriers );
103 update_option( self::OPTION_LAST_CARRIER_UPDATE, gmdate( DATE_ATOM ) );
104
105 return [
106 __( 'Carriers were updated.', 'packeta' ),
107 'success',
108 ];
109 }
110
111 /**
112 * Cron job. No authorization needed - job is registered internally.
113 */
114 public function runAndRender(): void {
115 [ $message, $class ] = $this->run();
116 echo esc_html( $message );
117 }
118
119 /**
120 * Downloads carriers and returns in array.
121 *
122 * @return array|null
123 * @throws DownloadException DownloadException.
124 */
125 private function fetch_as_array(): ?array {
126 $json = $this->download_json();
127
128 return $this->get_from_json( $json );
129 }
130
131 /**
132 * Downloads carriers in JSON.
133 *
134 * @return string
135 * @throws DownloadException DownloadException.
136 */
137 private function download_json(): string {
138 $url = sprintf( self::API_URL, $this->options_provider->get_api_key() );
139 /**
140 * Guzzle response.
141 *
142 * @var Response $result Guzzle response.
143 */
144 try {
145 $result = $this->client->get( $url );
146 } catch ( GuzzleException $exception ) {
147 throw new DownloadException( $exception->getMessage() );
148 }
149
150 return $result->getBody()->getContents();
151 }
152
153 /**
154 * Converts JSON to array.
155 *
156 * @param string $json JSON.
157 *
158 * @return array|null
159 */
160 private function get_from_json( string $json ): ?array {
161 $carriers_data = json_decode( $json, true );
162
163 return ( $carriers_data['carriers'] ?? null );
164 }
165 }
166