1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Bundle\CoreBundle\EventListener; |
13
|
|
|
|
14
|
|
|
use Sylius\Component\Core\Model\ProductInterface; |
15
|
|
|
use Sylius\Component\Core\Model\ProductVariantInterface; |
16
|
|
|
use Sylius\Component\Core\Uploader\ImageUploaderInterface; |
17
|
|
|
use Sylius\Component\Resource\Exception\UnexpectedTypeException; |
18
|
|
|
use Sylius\Component\Taxonomy\Model\TaxonInterface; |
19
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
20
|
|
|
|
21
|
|
|
class ImageUploadListener |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var ImageUploaderInterface |
25
|
|
|
*/ |
26
|
|
|
protected $uploader; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param ImageUploaderInterface $uploader |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ImageUploaderInterface $uploader) |
32
|
|
|
{ |
33
|
|
|
$this->uploader = $uploader; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param GenericEvent $event |
38
|
|
|
*/ |
39
|
|
|
public function uploadProductImage(GenericEvent $event) |
40
|
|
|
{ |
41
|
|
|
$subject = $event->getSubject(); |
42
|
|
|
if (!$subject instanceof ProductInterface && !$subject instanceof ProductVariantInterface) { |
43
|
|
|
throw new UnexpectedTypeException( |
44
|
|
|
$subject, |
45
|
|
|
'Sylius\Component\Core\Model\ProductInterface or Sylius\Component\Core\Model\ProductVariantInterface' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$variant = $subject instanceof ProductVariantInterface ? $subject : $subject->getMasterVariant(); |
50
|
|
|
|
51
|
|
|
if (null === $variant) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$images = $variant->getImages(); |
|
|
|
|
56
|
|
|
foreach ($images as $image) { |
57
|
|
|
$this->uploader->upload($image); |
58
|
|
|
|
59
|
|
|
// Upload failed? Let's remove that image. |
60
|
|
|
if (null === $image->getPath()) { |
61
|
|
|
$images->removeElement($image); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param GenericEvent $event |
68
|
|
|
*/ |
69
|
|
|
public function uploadTaxonImage(GenericEvent $event) |
70
|
|
|
{ |
71
|
|
|
$subject = $event->getSubject(); |
72
|
|
|
|
73
|
|
|
if (!$subject instanceof TaxonInterface) { |
74
|
|
|
throw new UnexpectedTypeException( |
75
|
|
|
$subject, |
76
|
|
|
TaxonInterface::class |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($subject->hasFile()) { |
81
|
|
|
$this->uploader->upload($subject); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
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: