PluginProbe
seQura / 4.0.0
seQura v4.0.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.0.0, at src/Services/Widgets/class-widgets-service.php

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