Completed
Pull Request — master (#128)
by Antonio
04:42
created

PageImageUploadListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 34
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A uploadImage() 0 15 4
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * another great project.
7
 * You can find more information about us on https://bitbag.shop and write us
8
 * an email on [email protected].
9
 */
10
11
declare(strict_types=1);
12
13
namespace BitBag\SyliusCmsPlugin\EventListener;
14
15
use BitBag\SyliusCmsPlugin\Entity\PageInterface;
16
use BitBag\SyliusCmsPlugin\Entity\PageTranslationInterface;
17
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
18
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
19
use Webmozart\Assert\Assert;
20
21
final class PageImageUploadListener
22
{
23
    /**
24
     * @var ImageUploaderInterface
25
     */
26
    private $uploader;
27
28
    /**
29
     * @param ImageUploaderInterface $uploader
30
     */
31
    public function __construct(ImageUploaderInterface $uploader)
32
    {
33
        $this->uploader = $uploader;
34
    }
35
36
    /**
37
     * @param ResourceControllerEvent $event
38
     */
39
    public function uploadImage(ResourceControllerEvent $event): void
40
    {
41
        $page = $event->getSubject();
42
43
        Assert::isInstanceOf($page, PageInterface::class);
44
45
        /** @var PageTranslationInterface $translation */
46
        foreach ($page->getTranslations() as $translation) {
47
            $image = $translation->getImage();
48
49
            if (null !== $image && true === $image->hasFile()) {
50
                $this->uploader->upload($image);
51
            }
52
        }
53
    }
54
}
55