PluginProbe
seQura / 4.1.0
seQura v4.1.0
4.3.4 4.3.3 4.3.2 4.3.1 trunk 2.0.0 2.0.10 2.0.11 2.0.12 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 3.0.0 3.0.2 3.0.5 3.0.6 3.0.7 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 4.0.0 All 30 releases
sequra / src / Services / Widgets / class-widgets-service.php

class-widgets-service.php in seQura 4.1.0, at src/Services/Widgets/class-widgets-service.php

221 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Widgets Service Interface
4 *
5 * @package SeQura/WC
6 * @subpackage SeQura/WC/Services
7 */
8
9 namespace SeQura\WC\Services\Widgets;
10
11 use SeQura\Core\BusinessLogic\AdminAPI\Response\Response;
12 use SeQura\Core\BusinessLogic\CheckoutAPI\CheckoutAPI;
13 use SeQura\Core\BusinessLogic\CheckoutAPI\PromotionalWidgets\Requests\PromotionalWidgetsCheckoutRequest;
14 use SeQura\Core\BusinessLogic\CheckoutAPI\PromotionalWidgets\Responses\GetWidgetsCheckoutResponse;
15 use SeQura\Core\BusinessLogic\Domain\Integration\PromotionalWidgets\WidgetConfiguratorInterface;
16 use SeQura\Core\BusinessLogic\Domain\Multistore\StoreContext;
17 use SeQura\WC\Services\I18n\Interface_I18n;
18 use SeQura\WC\Services\Log\Interface_Logger_Service;
19 use SeQura\WC\Services\Shopper\Interface_Shopper_Service;
20
21 /**
22 * Widgets Service Interface
23 *
24 * @phpstan-import-type WidgetDataArray from Interface_Widgets_Service
25 * @phpstan-import-type WidgetConfigParamsDataArray from Interface_Widgets_Service
26 */
27 class Widgets_Service implements Interface_Widgets_Service {
28
29 /**
30 * Logger service
31 *
32 * @var Interface_Logger_Service
33 */
34 private $logger;
35
36 /**
37 * Internationalization service
38 *
39 * @var Interface_I18n
40 */
41 private $i18n;
42
43 /**
44 * Shopper service
45 *
46 * @var Interface_Shopper_Service
47 */
48 private $shopper_service;
49
50 /**
51 * Widget configurator
52 *
53 * @var WidgetConfiguratorInterface
54 */
55 private $widget_configurator;
56
57 /**
58 * Store context
59 *
60 * @var StoreContext
61 */
62 private $store_context;
63
64 /**
65 * Constructor
66 */
67 public function __construct(
68 Interface_Logger_Service $logger,
69 Interface_I18n $i18n,
70 Interface_Shopper_Service $shopper_service,
71 WidgetConfiguratorInterface $widget_configurator,
72 StoreContext $store_context
73 ) {
74 $this->logger = $logger;
75 $this->i18n = $i18n;
76 $this->shopper_service = $shopper_service;
77 $this->widget_configurator = $widget_configurator;
78 $this->store_context = $store_context;
79 }
80
81 /**
82 * Get available widget for cart page
83 *
84 * @return WidgetDataArray|null The available widget, or null if cannot be determined
85 */
86 public function get_widget_for_cart_page(): ?array {
87 $country = $this->i18n->get_current_country();
88
89 /**
90 * Response
91 *
92 * @var Response $response */
93 $response = CheckoutAPI::get()
94 ->promotionalWidgets( $this->store_context->getStoreId() )
95 ->getAvailableWidgetForCartPage(
96 new PromotionalWidgetsCheckoutRequest(
97 $country,
98 $country,
99 $this->widget_configurator->getCurrency() ?? '',
100 $this->shopper_service->get_ip()
101 )
102 );
103
104 if ( ! $response->isSuccessful() ) {
105 $this->logger->log_debug( 'Cannot retrieve cart widget', __FUNCTION__, __CLASS__ );
106 return null;
107 }
108
109 /**
110 * Promotional widgets
111 *
112 * @var WidgetDataArray[] $widgets */
113 $widgets = $response->toArray();
114
115 if ( empty( $widgets ) ) {
116 $this->logger->log_info( 'No cart widget available', __FUNCTION__, __CLASS__ );
117 return null;
118 }
119
120 return $widgets[0];
121 }
122
123 /**
124 * Get available widget for product listing page
125 *
126 * @return WidgetDataArray|null The available widget, or null if cannot be determined
127 */
128 public function get_widget_for_product_listing_page(): ?array {
129 $country = $this->i18n->get_current_country();
130
131 /**
132 * Response
133 *
134 * @var Response $response */
135 $response = CheckoutAPI::get()
136 ->promotionalWidgets( $this->store_context->getStoreId() )
137 ->getAvailableMiniWidgetForProductListingPage(
138 new PromotionalWidgetsCheckoutRequest(
139 $country,
140 $country,
141 $this->widget_configurator->getCurrency() ?? '',
142 $this->shopper_service->get_ip()
143 )
144 );
145
146 if ( ! $response->isSuccessful() ) {
147 $this->logger->log_debug( 'Cannot retrieve product listing widget', __FUNCTION__, __CLASS__ );
148 return null;
149 }
150
151 /**
152 * Promotional widgets
153 *
154 * @var WidgetDataArray[] $widgets */
155 $widgets = $response->toArray();
156 if ( empty( $widgets ) ) {
157 $this->logger->log_info( 'No product listing widget available', __FUNCTION__, __CLASS__ );
158 return null;
159 }
160
161 return $widgets[0];
162 }
163
164 /**
165 * Get available widgets for product page
166 *
167 * @return WidgetDataArray[]|null The available widgets, or null if cannot be determined
168 */
169 public function get_widgets_for_product_page( string $product_id ): ?array {
170 $country = $this->i18n->get_current_country();
171 /**
172 * Response from CheckoutAPI
173 *
174 * @var GetWidgetsCheckoutResponse $response */
175 $response = CheckoutAPI::get()
176 ->promotionalWidgets( $this->store_context->getStoreId() )
177 ->getAvailableWidgetsForProductPage(
178 new PromotionalWidgetsCheckoutRequest(
179 $country,
180 $country,
181 $this->widget_configurator->getCurrency() ?? '',
182 $this->shopper_service->get_ip(),
183 empty( $product_id ) ? '' : $product_id
184 )
185 );
186
187 if ( ! $response->isSuccessful() ) {
188 $this->logger->log_debug( 'Cannot retrieve product page widgets', __FUNCTION__, __CLASS__ );
189 return null;
190 }
191
192 return $response->toArray();
193 }
194
195 /**
196 * Get widget configuration parameters required in the frontend
197 *
198 * @return WidgetConfigParamsDataArray|null The configuration parameters, or null if cannot be determined
199 */
200 public function get_widget_config_params(): ?array {
201 $country = $this->i18n->get_current_country();
202 /**
203 * Response
204 *
205 * @var Response $response
206 */
207 $response = CheckoutAPI::get()
208 ->promotionalWidgets( $this->store_context->getStoreId() )
209 ->getPromotionalWidgetInitializeData(
210 new PromotionalWidgetsCheckoutRequest( $country, $country )
211 );
212
213 if ( ! $response->isSuccessful() ) {
214 $this->logger->log_debug( 'Cannot retrieve widget configuration parameters', __FUNCTION__, __CLASS__ );
215 return null;
216 }
217
218 return $response->toArray();
219 }
220 }
221