Completed
Pull Request — master (#42)
by Arvids
10:06
created

TaxonRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace BitBag\SyliusElasticsearchPlugin\EntityRepository;
6
7
use Doctrine\ORM\Query\Expr\Join;
8
use Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository;
9
use Sylius\Bundle\TaxonomyBundle\Doctrine\ORM\TaxonRepository as BaseTaxonRepository;
10
use Sylius\Component\Attribute\Model\AttributeInterface;
11
use Sylius\Component\Core\Model\TaxonInterface;
12
use Sylius\Component\Core\Repository\ProductRepositoryInterface;
13
use Sylius\Component\Taxonomy\Repository\TaxonRepositoryInterface as BaseTaxonRepositoryInterface;
14
15
class TaxonRepository implements TaxonRepositoryInterface
16
{
17
    /**
18
     * @var BaseTaxonRepository
19
     */
20
    protected $taxonRepository;
21
22
    /**
23
     * @var ProductRepository
24
     */
25
    protected $productRepository;
26
27
    /**
28
     * @var string
29
     */
30
    protected $productTaxonEntityClass;
31
32
    /**
33
     * @var string
34
     */
35
    protected $productAttributeEntityClass;
36
37
    /**
38
     * TaxonRepository constructor.
39
     *
40
     * @param BaseTaxonRepositoryInterface $taxonRepository
41
     * @param ProductRepositoryInterface   $productRepository
42
     * @param string                       $productTaxonEntityClass
43
     * @param string                       $productAttributeEntityClass
44
     */
45
    public function __construct(
46
        BaseTaxonRepositoryInterface $taxonRepository,
47
        ProductRepositoryInterface $productRepository,
48
        string $productTaxonEntityClass,
49
        string $productAttributeEntityClass
50
    ) {
51
        $this->taxonRepository             = $taxonRepository;
0 ignored issues
show
Documentation Bug introduced by
$taxonRepository is of type Sylius\Component\Taxonom...axonRepositoryInterface, but the property $taxonRepository was declared to be of type Sylius\Bundle\TaxonomyBu...ine\ORM\TaxonRepository. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
52
        $this->productRepository           = $productRepository;
0 ignored issues
show
Documentation Bug introduced by
$productRepository is of type Sylius\Component\Core\Re...ductRepositoryInterface, but the property $productRepository was declared to be of type Sylius\Bundle\CoreBundle...e\ORM\ProductRepository. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
53
        $this->productTaxonEntityClass     = $productTaxonEntityClass;
54
        $this->productAttributeEntityClass = $productAttributeEntityClass;
55
    }
56
57
    /**
58
     * @param AttributeInterface $attribute
59
     *
60
     * @return array|TaxonInterface[]
61
     */
62
    public function getTaxonsByAttributeViaProduct(AttributeInterface $attribute): array
63
    {
64
        return $this->taxonRepository
65
            ->createQueryBuilder('t')
66
            ->distinct(true)
67
            ->select('t')
68
            ->leftJoin($this->productTaxonEntityClass, 'pt', Join::WITH, 'pt.taxon = t.id')
69
            ->where(
70
                'pt.product IN(' .
71
                $this
72
                    ->productRepository->createQueryBuilder('p')
73
                    ->leftJoin($this->productAttributeEntityClass, 'pav', Join::WITH, 'pav.subject = p.id')
74
                    ->where('pav.attribute = :attribute')
75
                    ->getQuery()
76
                    ->getDQL()
77
                . ')'
78
            )
79
            ->setParameter(':attribute', $attribute)
80
            ->getQuery()
81
            ->getResult();
82
    }
83
84
}
85