Completed
Pull Request — master (#290)
by
unknown
36:49 queued 34:03
created

LimitedProductAttributeValuesViewFactory::create()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Factory;
6
7
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
8
9
final class LimitedProductAttributeValuesViewFactory implements ProductAttributeValuesViewFactoryInterface
10
{
11
    /** @var ProductAttributeValueViewFactoryInterface */
12
    private $productAttributeValueViewFactory;
13
14
    /** @var string[] */
15
    private $allowedAttributesCodes;
16
17
    public function __construct(ProductAttributeValueViewFactoryInterface $productAttributeValueViewFactory, array $allowedAttributesCodes)
18
    {
19
        $this->productAttributeValueViewFactory = $productAttributeValueViewFactory;
20
        $this->allowedAttributesCodes = $allowedAttributesCodes;
21
    }
22
23
    public function create(array $attributeValues, string $locale): array
24
    {
25
        $attributeValuesView = [];
26
27
        /** @var ProductAttributeValueInterface $attributeValue */
28
        foreach ($attributeValues as $attributeValue) {
29
            if (!in_array($attributeValue->getCode(), $this->allowedAttributesCodes, true)) {
30
                continue;
31
            }
32
33
            $attributeValuesView[] = $this->productAttributeValueViewFactory->create($attributeValue, $locale);
34
        }
35
36
        return $attributeValuesView;
37
    }
38
}
39