-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathAssetValueBuilderProvider.php
92 lines (76 loc) · 3.02 KB
/
AssetValueBuilderProvider.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
declare(strict_types=1);
namespace Synolia\SyliusAkeneoPlugin\Provider\Asset;
use Psr\Log\LoggerInterface;
use Synolia\SyliusAkeneoPlugin\Builder\Asset\Attribute\AssetAttributeValueBuilderInterface;
use Synolia\SyliusAkeneoPlugin\Checker\EditionCheckerInterface;
use Synolia\SyliusAkeneoPlugin\Exceptions\UnsupportedAttributeTypeException;
final class AssetValueBuilderProvider implements AssetValueBuilderProviderInterface
{
/** @var array<AssetAttributeValueBuilderInterface> */
private array $assetAttributeValueBuilders;
public function __construct(
private LoggerInterface $akeneoLogger,
private EditionCheckerInterface $editionChecker,
) {
}
public function addBuilder(AssetAttributeValueBuilderInterface $assetAttributeValueBuilder): void
{
$this->assetAttributeValueBuilders[$assetAttributeValueBuilder::class] = $assetAttributeValueBuilder;
}
/**
* @param mixed $value
*
* @return mixed|null
*/
public function build(string $assetFamilyCode, string $assetCode, ?string $locale, ?string $scope, $value)
{
foreach ($this->assetAttributeValueBuilders as $assetAttributeValueBuilder) {
if ($assetAttributeValueBuilder->support($assetFamilyCode, $assetCode)) {
return $assetAttributeValueBuilder->build($assetFamilyCode, $assetCode, $locale, $scope, $value);
}
}
return null;
}
/**
* @return mixed|null
*/
public function findBuilderByClassName(string $className)
{
foreach ($this->assetAttributeValueBuilders as $attributeValueBuilder) {
if (!$attributeValueBuilder instanceof $className) {
continue;
}
return $attributeValueBuilder;
}
return null;
}
public function hasSupportedBuilder(string $assetFamilyCode, string $assetCode): bool
{
$isEnterprise = $this->editionChecker->isEnterprise() || $this->editionChecker->isSerenityEdition();
if (!$isEnterprise) {
return false;
}
foreach ($this->assetAttributeValueBuilders as $attributeValueBuilder) {
try {
if ($attributeValueBuilder->support($assetFamilyCode, $assetCode)) {
return true;
}
} catch (UnsupportedAttributeTypeException $throwable) {
$this->akeneoLogger->info('Unsupported AssetAttributeType', [
'family_code' => $assetFamilyCode,
'asset_code' => $assetCode,
]);
} catch (\Throwable $throwable) {
$this->akeneoLogger->critical(\sprintf(
'AssetValueBuilderInterface "%s" failed to execute method support() for asset "%s" in family "%s"',
$attributeValueBuilder::class,
$assetCode,
$assetFamilyCode,
), ['exception' => $throwable]);
return false;
}
}
return false;
}
}