Passed
Pull Request — master (#17)
by Joppe
03:41
created

SeoTranslationImagesUploadListener   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 24
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A uploadMedia() 0 14 5
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JoppeDc\SyliusBetterSeoPlugin\EventListener;
6
7
use JoppeDc\SyliusBetterSeoPlugin\Entity\SeoImageInterface;
8
use JoppeDc\SyliusBetterSeoPlugin\Entity\SeoInterface;
9
use JoppeDc\SyliusBetterSeoPlugin\Entity\SeoTranslation;
10
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
11
use Symfony\Component\EventDispatcher\GenericEvent;
12
use Webmozart\Assert\Assert;
13
14
class SeoTranslationImagesUploadListener
15
{
16
    /** @var ImageUploaderInterface */
17
    private $uploader;
18
19
    public function __construct(ImageUploaderInterface $uploader)
20
    {
21
        $this->uploader = $uploader;
22
    }
23
24
    public function uploadMedia(GenericEvent $event): void
25
    {
26
        $subject = $event->getSubject();
27
        Assert::isInstanceOf($subject, SeoInterface::class);
28
        /** @var SeoTranslation $translation */
29
        foreach ($subject->getSeo()->getTranslations() as $translation) {
30
            $image = $translation->getImage();
31
            if ($image instanceof SeoImageInterface) {
32
                if ($image->hasFile()) {
0 ignored issues
show
Bug introduced by
The method hasFile() does not exist on JoppeDc\SyliusBetterSeoP...ntity\SeoImageInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to JoppeDc\SyliusBetterSeoP...ntity\SeoImageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

32
                if ($image->/** @scrutinizer ignore-call */ hasFile()) {
Loading history...
33
                    $this->uploader->upload($image);
34
                }
35
                // Upload failed? Let's remove that image.
36
                if (null === $image->getPath()) {
0 ignored issues
show
Bug introduced by
The method getPath() does not exist on JoppeDc\SyliusBetterSeoP...ntity\SeoImageInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to JoppeDc\SyliusBetterSeoP...ntity\SeoImageInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
                if (null === $image->/** @scrutinizer ignore-call */ getPath()) {
Loading history...
37
                    $translation->setImage(null);
38
                }
39
            }
40
        }
41
    }
42
}
43