TaxonDetailsViewFactory::buildTaxonView()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\ShopApiPlugin\Factory;
6
7
use Sylius\Component\Core\Model\TaxonInterface;
8
use Sylius\ShopApiPlugin\View\TaxonDetailsView;
9
use Sylius\ShopApiPlugin\View\TaxonView;
10
11
final class TaxonDetailsViewFactory implements TaxonDetailsViewFactoryInterface
12
{
13
    /** @var TaxonViewFactoryInterface */
14
    private $taxonViewFactory;
15
16
    /** @var string */
17
    private $taxonDetailsViewClass;
18
19
    public function __construct(TaxonViewFactoryInterface $taxonViewFactory, string $taxonDetailsViewClass)
20
    {
21
        $this->taxonViewFactory = $taxonViewFactory;
22
        $this->taxonDetailsViewClass = $taxonDetailsViewClass;
23
    }
24
25
    public function create(TaxonInterface $taxon, string $localeCode): TaxonDetailsView
26
    {
27
        /** @var TaxonDetailsView $detailTaxonView */
28
        $detailTaxonView = new $this->taxonDetailsViewClass();
29
30
        $detailTaxonView->self = $this->buildTaxonView($taxon, $localeCode);
31
        $detailTaxonView->parentTree = $this->getTaxonWithAncestors($taxon, $localeCode);
32
33
        return $detailTaxonView;
34
    }
35
36
    private function getTaxonWithAncestors(TaxonInterface $taxon, string $localeCode): TaxonView
37
    {
38
        $currentTaxonView = $this->taxonViewFactory->create($taxon, $localeCode);
39
40
        while (null !== $taxon->getParent()) {
41
            $taxon = $taxon->getParent();
42
43
            $taxonView = $this->taxonViewFactory->create($taxon, $localeCode);
44
            $taxonView->children[] = $currentTaxonView;
45
            $currentTaxonView = $taxonView;
46
        }
47
48
        return $currentTaxonView;
49
    }
50
51 View Code Duplication
    private function buildTaxonView(TaxonInterface $taxon, $locale): TaxonView
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $taxonView = $this->taxonViewFactory->create($taxon, $locale);
54
55
        foreach ($taxon->getChildren() as $childTaxon) {
56
            $taxonView->children[] = $this->buildTaxonView($childTaxon, $locale);
57
        }
58
59
        return $taxonView;
60
    }
61
}
62