Completed
Pull Request — master (#110)
by Mikołaj
15:33 queued 06:38
created

BlockImageUploadListener::uploadImage()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
c 1
b 0
f 0
rs 8.8571
cc 5
eloc 9
nc 4
nop 1
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\BlockInterface;
16
use BitBag\SyliusCmsPlugin\Entity\BlockTranslationInterface;
17
use Sylius\Bundle\ResourceBundle\Event\ResourceControllerEvent;
18
use Sylius\Component\Core\Uploader\ImageUploaderInterface;
19
use Webmozart\Assert\Assert;
20
21
final class BlockImageUploadListener
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
        $block = $event->getSubject();
42
43
        Assert::isInstanceOf($block,BlockInterface::class);
44
45
        if (BlockInterface::IMAGE_BLOCK_TYPE !== $block->getType()) {
46
            return;
47
        }
48
49
        /** @var BlockTranslationInterface $translation */
50
        foreach ($block->getTranslations() as $translation) {
51
            $image = $translation->getImage();
52
53
            if (null !== $image && true === $image->hasFile()) {
54
                $this->uploader->upload($image);
55
            }
56
        }
57
    }
58
}
59