Completed
Pull Request — master (#124)
by Łukasz
02:52
created

LimitedProductAttributeValuesViewFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 1
dl 0
loc 41
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 15 3
1
<?php
2
3
namespace Sylius\ShopApiPlugin\Factory;
4
5
use Sylius\Component\Product\Model\ProductAttributeValueInterface;
6
7
final class LimitedProductAttributeValuesViewFactory implements ProductAttributeValuesViewFactoryInterface
8
{
9
    /**
10
     * @var ProductAttributeValueViewFactoryInterface
11
     */
12
    private $productAttributeValueViewFactory;
13
14
    /**
15
     * @var string[]
16
     */
17
    private $allowedAttributesCodes;
18
19
    /**
20
     * @param ProductAttributeValueViewFactoryInterface $productAttributeValueViewFactory
21
     * @param string[] $allowedAttributesCodes
22
     */
23
    public function __construct(ProductAttributeValueViewFactoryInterface $productAttributeValueViewFactory, array $allowedAttributesCodes)
24
    {
25
        $this->productAttributeValueViewFactory = $productAttributeValueViewFactory;
26
        $this->allowedAttributesCodes = $allowedAttributesCodes;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function create(array $attributeValues)
33
    {
34
        $attributeValuesView = [];
35
36
        /** @var ProductAttributeValueInterface $attributeValue */
37
        foreach ($attributeValues as $attributeValue) {
38
            if (!in_array($attributeValue->getCode(), $this->allowedAttributesCodes, true)) {
39
                continue;
40
            }
41
42
            $attributeValuesView[] = $this->productAttributeValueViewFactory->create($attributeValue);
43
        }
44
45
        return $attributeValuesView;
46
    }
47
}
48