Passed
Push — master ( 0ad767...e2ff7c )
by Mikołaj
04:09
created

ProductAttributesFinder::findByTaxon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag. 
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project. 
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected]. 
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusElasticsearchPlugin\Finder;
14
15
use BitBag\SyliusElasticsearchPlugin\QueryBuilder\QueryBuilderInterface;
16
use FOS\ElasticaBundle\Finder\FinderInterface;
17
use Sylius\Component\Core\Model\TaxonInterface;
18
19
final class ProductAttributesFinder implements ProductAttributesFinderInterface
20
{
21
    /**
22
     * @var FinderInterface
23
     */
24
    private $attributesFinder;
25
26
    /**
27
     * @var QueryBuilderInterface
28
     */
29
    private $attributesByTaxonQueryBuilder;
30
31
    /**
32
     * @var string
33
     */
34
    private $taxonsProperty;
35
36
    /**
37
     * @param FinderInterface $attributesFinder
38
     * @param QueryBuilderInterface $attributesByTaxonQueryBuilder
39
     * @param string $taxonsProperty
40
     */
41
    public function __construct(
42
        FinderInterface $attributesFinder,
43
        QueryBuilderInterface $attributesByTaxonQueryBuilder,
44
        string $taxonsProperty
45
    )
46
    {
47
        $this->attributesFinder = $attributesFinder;
48
        $this->attributesByTaxonQueryBuilder = $attributesByTaxonQueryBuilder;
49
        $this->taxonsProperty = $taxonsProperty;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function findByTaxon(TaxonInterface $taxon): ?array
56
    {
57
        $data = [];
58
        $data[$this->taxonsProperty] = strtolower($taxon->getCode());
59
60
        $query = $this->attributesByTaxonQueryBuilder->buildQuery($data);
61
        $attributes = $this->attributesFinder->find($query);
62
63
        return $attributes;
64
    }
65
}
66