|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sylius\ShopApiPlugin\Factory; |
|
4
|
|
|
|
|
5
|
|
|
use Sylius\Component\Attribute\Model\AttributeValueInterface; |
|
6
|
|
|
use Sylius\Component\Core\Model\ChannelInterface; |
|
7
|
|
|
use Sylius\Component\Core\Model\ProductImageInterface; |
|
8
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
|
9
|
|
|
use Sylius\Component\Core\Model\TaxonInterface; |
|
10
|
|
|
use Sylius\ShopApiPlugin\View\ProductView; |
|
11
|
|
|
|
|
12
|
|
|
final class ProductViewFactory implements ProductViewFactoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var ImageViewFactoryInterface |
|
16
|
|
|
*/ |
|
17
|
|
|
private $imageViewFactory; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var ProductAttributeValueViewFactoryInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $attributeValueViewFactory; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $fallback; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param ImageViewFactoryInterface $imageViewFactory |
|
31
|
|
|
* @param ProductAttributeValueViewFactoryInterface $attributeValueViewFactory |
|
32
|
|
|
* @param string $fallback |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
ImageViewFactoryInterface $imageViewFactory, |
|
36
|
|
|
ProductAttributeValueViewFactoryInterface $attributeValueViewFactory, |
|
37
|
|
|
$fallback |
|
38
|
|
|
) { |
|
39
|
|
|
$this->imageViewFactory = $imageViewFactory; |
|
40
|
|
|
$this->attributeValueViewFactory = $attributeValueViewFactory; |
|
41
|
|
|
$this->fallback = $fallback; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function create(ProductInterface $product, ChannelInterface $channel, $locale) |
|
48
|
|
|
{ |
|
49
|
|
|
$productView = new ProductView(); |
|
50
|
|
|
$productView->name = $product->getTranslation($locale)->getName(); |
|
|
|
|
|
|
51
|
|
|
$productView->code = $product->getCode(); |
|
52
|
|
|
$productView->slug = $product->getTranslation($locale)->getSlug(); |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** @var ProductImageInterface $image */ |
|
55
|
|
|
foreach ($product->getImages() as $image) { |
|
56
|
|
|
$imageView = $this->imageViewFactory->create($image); |
|
57
|
|
|
$productView->images[] = $imageView; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** @var TaxonInterface $taxon */ |
|
61
|
|
|
foreach ($product->getTaxons() as $taxon) { |
|
62
|
|
|
$productView->taxons[] = $taxon->getCode(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** @var AttributeValueInterface $attribute */ |
|
66
|
|
|
foreach ($product->getAttributesByLocale($locale, $this->fallback) as $attribute) { |
|
67
|
|
|
$productView->attributes[] = $this->attributeValueViewFactory->create($attribute); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $productView; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: