ProductTaxonRepository::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
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.io and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusVueStorefrontPlugin\Sylius\Repository;
14
15
use Sylius\Component\Core\Model\ProductTaxonInterface;
16
use Sylius\Component\Core\Model\TaxonInterface;
17
use Sylius\Component\Core\Repository\ProductTaxonRepositoryInterface as BaseProductTaxonRepositoryInterface;
18
19
final class ProductTaxonRepository
20
{
21
    /** @var BaseProductTaxonRepositoryInterface */
22
    private $baseProductTaxonRepository;
23
24
    /** @var int[] */
25
    private $childrenIds = [];
26
27
    public function __construct(BaseProductTaxonRepositoryInterface $baseProductTaxonRepository)
28
    {
29
        $this->baseProductTaxonRepository = $baseProductTaxonRepository;
30
    }
31
32
    public function getAmountOfProducts(TaxonInterface $taxon): int
33
    {
34
        $this->childrenIds = [];
35
        $this->getChildrenIdsRecursive($taxon);
36
37
        /** @var ProductTaxonInterface[] $productTaxons */
38
        $productTaxons = $this->baseProductTaxonRepository->findBy(['taxon' => $this->childrenIds]);
39
40
        return count($productTaxons);
41
    }
42
43
    private function getChildrenIdsRecursive(TaxonInterface $taxon): void
44
    {
45
        $this->childrenIds[] = $taxon;
46
47
        foreach ($taxon->getChildren() as $child) {
48
            if ($child->hasChildren()) {
49
                $this->getChildrenIdsRecursive($child);
50
            }
51
        }
52
    }
53
}
54