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

SeoTranslationImagesUploadListener::uploadMedia()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 9
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 14
rs 9.6111
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