Completed
Pull Request — master (#290)
by
unknown
36:49 queued 34:03
created

TaxonViewFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 3
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 37
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A create() 0 22 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Sylius\SyliusShopApiPlugin\Factory;
6
7
use Sylius\Component\Core\Model\ImageInterface;
8
use Sylius\Component\Core\Model\TaxonInterface;
9
use Sylius\Component\Taxonomy\Model\TaxonTranslationInterface;
10
use Sylius\SyliusShopApiPlugin\View\TaxonView;
11
12
final class TaxonViewFactory implements TaxonViewFactoryInterface
13
{
14
    /** @var ImageViewFactoryInterface */
15
    private $imageViewFactory;
16
17
    /** @var string */
18
    private $taxonViewClass;
19
20
    public function __construct(ImageViewFactoryInterface $imageViewFactory, string $taxonViewClass)
21
    {
22
        $this->imageViewFactory = $imageViewFactory;
23
        $this->taxonViewClass = $taxonViewClass;
24
    }
25
26
    public function create(TaxonInterface $taxon, string $locale): TaxonView
27
    {
28
        /** @var TaxonTranslationInterface $taxonTranslation */
29
        $taxonTranslation = $taxon->getTranslation($locale);
30
31
        /** @var TaxonView $taxonView */
32
        $taxonView = new $this->taxonViewClass();
33
34
        $taxonView->code = $taxon->getCode();
35
        $taxonView->position = $taxon->getPosition();
36
37
        $taxonView->name = $taxonTranslation->getName();
38
        $taxonView->slug = $taxonTranslation->getSlug();
39
        $taxonView->description = $taxonTranslation->getDescription();
40
41
        /** @var ImageInterface $image */
42
        foreach ($taxon->getImages() as $image) {
43
            $taxonView->images[] = $this->imageViewFactory->create($image);
44
        }
45
46
        return $taxonView;
47
    }
48
}
49