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 |
|
|
|
|
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
|
|
|
|
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.