|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Omikron\Factfinder\Model\Api; |
| 5 | + |
| 6 | +use Magento\Checkout\Model\Session as CheckoutSession; |
| 7 | +use Magento\Quote\Model\Quote\Item; |
| 8 | +use Omikron\FactFinder\Communication\Client\ClientBuilder; |
| 9 | +use Omikron\Factfinder\Model\Config\AuthConfig; |
| 10 | +use Omikron\Factfinder\Model\Config\CommunicationConfig; |
| 11 | +use Omikron\Factfinder\Model\SessionData; |
| 12 | + |
| 13 | +class Example |
| 14 | +{ |
| 15 | + public function __construct( |
| 16 | + private readonly SessionData $sessionData, |
| 17 | + private readonly CommunicationConfig $communicationConfig, |
| 18 | + private readonly ClientBuilder $clientBuilder, |
| 19 | + private readonly AuthConfig $authConfig, |
| 20 | + private readonly CheckoutSession $checkoutSession, |
| 21 | + ) { |
| 22 | + } |
| 23 | + |
| 24 | + public function getPredictiveBasketSkus(): array |
| 25 | + { |
| 26 | + $cartSkus = array_map( |
| 27 | + static fn (Item $item): string => $item->getProduct()->getSku(), |
| 28 | + $this->checkoutSession->getQuote()->getItems() ?? [] |
| 29 | + ); |
| 30 | + |
| 31 | + $baseParams = [ |
| 32 | + 'idsOnly' => true, |
| 33 | + 'userId' => $this->sessionData->getUserId(), |
| 34 | + ]; |
| 35 | + |
| 36 | + $queryString = http_build_query($baseParams, '', '&', PHP_QUERY_RFC3986); |
| 37 | + |
| 38 | + foreach ($cartSkus as $sku) { |
| 39 | + $queryString .= '&blacklist=' . rawurlencode($sku); |
| 40 | + } |
| 41 | + |
| 42 | + $client = $this->clientBuilder |
| 43 | + ->withServerUrl($this->communicationConfig->getAddress()) |
| 44 | + ->withApiKey($this->authConfig->getApiKey()) |
| 45 | + ->withVersion($this->communicationConfig->getVersion()) |
| 46 | + ->build(); |
| 47 | + |
| 48 | + $result = $client->request( |
| 49 | + 'GET', |
| 50 | + 'rest/v5/predictivebasket/dev_kastner?' . $queryString |
| 51 | + ); |
| 52 | + |
| 53 | + return array_column($result['hits'] ?? [], 'id'); |
| 54 | + } |
| 55 | +} |
0 commit comments