1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Factory; |
4
|
|
|
|
5
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
6
|
|
|
use Sylius\Component\Core\Model\ProductImageInterface; |
7
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
8
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
9
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
10
|
|
|
use Sylius\ShopApiPlugin\View\ProductVariantView; |
11
|
|
|
use Sylius\ShopApiPlugin\View\ProductView; |
12
|
|
|
|
13
|
|
View Code Duplication |
final class ProductViewFactory implements ProductViewFactoryInterface |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var ImageViewFactoryInterface |
17
|
|
|
*/ |
18
|
|
|
private $imageViewFactory; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param ImageViewFactoryInterface $imageViewFactory |
22
|
|
|
*/ |
23
|
|
|
public function __construct(ImageViewFactoryInterface $imageViewFactory) |
24
|
|
|
{ |
25
|
|
|
$this->imageViewFactory = $imageViewFactory; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* {@inheritdoc} |
30
|
|
|
*/ |
31
|
|
|
public function create(ProductInterface $product, ChannelInterface $channel, $locale) |
32
|
|
|
{ |
33
|
|
|
$productView = new ProductView(); |
34
|
|
|
$productView->name = $product->getTranslation($locale)->getName(); |
|
|
|
|
35
|
|
|
$productView->code = $product->getCode(); |
36
|
|
|
$productView->slug = $product->getTranslation($locale)->getSlug(); |
|
|
|
|
37
|
|
|
|
38
|
|
|
/** @var ProductImageInterface $image */ |
39
|
|
|
foreach ($product->getImages() as $image) { |
40
|
|
|
$imageView = $this->imageViewFactory->create($image); |
41
|
|
|
$productView->images[] = $imageView; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @var TaxonInterface $taxon */ |
45
|
|
|
foreach ($product->getTaxons() as $taxon) { |
46
|
|
|
$productView->taxons[] = $taxon->getCode(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $productView; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
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.