-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathFamilyVariantRetriever.php
66 lines (52 loc) · 2.18 KB
/
FamilyVariantRetriever.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace Synolia\SyliusAkeneoPlugin\Retriever;
use Akeneo\Pim\ApiClient\AkeneoPimClientInterface;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Synolia\SyliusAkeneoPlugin\Component\Cache\CacheKey;
use Synolia\SyliusAkeneoPlugin\Exceptions\Retriever\FamilyVariantNotFountException;
use Synolia\SyliusAkeneoPlugin\Provider\Configuration\Api\ApiConnectionProviderInterface;
final class FamilyVariantRetriever implements FamilyVariantRetrieverInterface
{
private array $variantsByFamily = [];
public function __construct(
private AkeneoPimClientInterface $akeneoPimClient,
private LoggerInterface $logger,
private ApiConnectionProviderInterface $apiConnectionProvider,
private CacheInterface $akeneoFamilyVariants,
) {
}
public function getVariants(string $familyCode): array
{
if ($this->variantsByFamily !== []) {
return $this->variantsByFamily[$familyCode] ?? [];
}
/** @phpstan-ignore-next-line */
return $this->variantsByFamily[$familyCode] = $this->akeneoFamilyVariants->get(\sprintf(CacheKey::FAMILY_VARIANTS, $familyCode), function () use ($familyCode): array {
$paginationSize = $this->apiConnectionProvider->get()->getPaginationSize();
try {
$results = $this->akeneoPimClient->getFamilyVariantApi()->all($familyCode, $paginationSize);
$familyVariants = iterator_to_array($results);
} catch (\Throwable $exception) {
$this->logger->warning($exception->getMessage());
return [];
}
return $familyVariants;
});
}
/**
* @throws FamilyVariantNotFountException
*/
public function getVariant(string $familyCode, string $familyVariantCode): array
{
$familyVariants = $this->getVariants($familyCode);
foreach ($familyVariants as $familyVariant) {
if ($familyVariant['code'] !== $familyVariantCode) {
continue;
}
return $familyVariant;
}
throw new FamilyVariantNotFountException('Could not determine variant');
}
}