Completed
Push — master ( aec3f7...9dc84f )
by Łukasz
14s
created

TaxonDetailsViewFactory::getTaxonWithAncestors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
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
    /**
14
     * @var TaxonViewFactoryInterface
15
     */
16
    private $taxonViewFactory;
17
18
    public function __construct(TaxonViewFactoryInterface $taxonViewFactory)
19
    {
20
        $this->taxonViewFactory = $taxonViewFactory;
21
    }
22
23
    public function create(TaxonInterface $taxon, string $localeCode): TaxonDetailsView
24
    {
25
        $detailTaxonView = new TaxonDetailsView();
26
27
        $detailTaxonView->self = $this->buildTaxonView($taxon, $localeCode);
28
        $detailTaxonView->parentTree = $this->getTaxonWithAncestors($taxon, $localeCode);
29
30
        return $detailTaxonView;
31
    }
32
33 View Code Duplication
    private function getTaxonWithAncestors(TaxonInterface $taxon, string $localeCode): 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...
34
    {
35
        $currentTaxonView = $this->taxonViewFactory->create($taxon, $localeCode);
36
37
        while (null !== $taxon->getParent()) {
38
            $taxon = $taxon->getParent();
39
40
            $taxonView = $this->taxonViewFactory->create($taxon, $localeCode);
0 ignored issues
show
Compatibility introduced by
$taxon of type object<Sylius\Component\...y\Model\TaxonInterface> is not a sub-type of object<Sylius\Component\...e\Model\TaxonInterface>. It seems like you assume a child interface of the interface Sylius\Component\Taxonomy\Model\TaxonInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
41
            $taxonView->children[] = $currentTaxonView;
42
            $currentTaxonView = $taxonView;
43
        }
44
45
        return $currentTaxonView;
46
    }
47
48 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...
49
    {
50
        $taxonView = $this->taxonViewFactory->create($taxon, $locale);
51
52
        foreach ($taxon->getChildren() as $childTaxon) {
53
            $taxonView->children[] = $this->buildTaxonView($childTaxon, $locale);
54
        }
55
56
        return $taxonView;
57
    }
58
}
59