Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions src/Model/Api/Example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);

namespace Omikron\Factfinder\Model\Api;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Quote\Model\Quote\Item;
use Omikron\FactFinder\Communication\Client\ClientBuilder;
use Omikron\Factfinder\Model\Config\AuthConfig;
use Omikron\Factfinder\Model\Config\CommunicationConfig;
use Omikron\Factfinder\Model\SessionData;

class Example
{
public function __construct(
private readonly SessionData $sessionData,
private readonly CommunicationConfig $communicationConfig,
private readonly ClientBuilder $clientBuilder,
private readonly AuthConfig $authConfig,
private readonly CheckoutSession $checkoutSession,
) {
}

public function getPredictiveBasketSkus(): array
{
$cartSkus = array_map(
static fn (Item $item): string => $item->getProduct()->getSku(),
$this->checkoutSession->getQuote()->getItems() ?? []
);

$baseParams = [
'idsOnly' => true,
'userId' => $this->sessionData->getUserId(),
];

$queryString = http_build_query($baseParams, '', '&', PHP_QUERY_RFC3986);

foreach ($cartSkus as $sku) {
$queryString .= '&blacklist=' . rawurlencode($sku);
}

$client = $this->clientBuilder
->withServerUrl($this->communicationConfig->getAddress())
->withApiKey($this->authConfig->getApiKey())
->withVersion($this->communicationConfig->getVersion())
->build();

$result = $client->request(
'GET',
'rest/v5/predictivebasket/dev_kastner?' . $queryString
);

return array_column($result['hits'] ?? [], 'id');
}
}
Loading