Completed
Pull Request — master (#95)
by
unknown
01:26
created

ImageBlockUploadListener::uploadImage()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 8.7624
cc 6
eloc 10
nc 5
nop 1
1
<?php
2
3
/**
4
 * This file was created by the 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
20
/**
21
 * @author Patryk Drapik <[email protected]>
22
 */
23
final class ImageBlockUploadListener
24
{
25
    /**
26
     * @var ImageUploaderInterface
27
     */
28
    private $uploader;
29
30
    /**
31
     * @param ImageUploaderInterface $uploader
32
     */
33
    public function __construct(ImageUploaderInterface $uploader)
34
    {
35
        $this->uploader = $uploader;
36
    }
37
38
    /**
39
     * @param ResourceControllerEvent $event
40
     */
41
    public function uploadImage(ResourceControllerEvent $event): void
42
    {
43
        $block = $event->getSubject();
44
45
        if (false === $block instanceof BlockInterface) {
46
            return;
47
        }
48
49
        if (BlockInterface::IMAGE_BLOCK_TYPE !== $block->getType()) {
50
            return;
51
        }
52
53
        /** @var BlockTranslationInterface $translation */
54
        foreach ($block->getTranslations() as $translation) {
55
            $image = $translation->getImage();
56
57
            if (null !== $image && true === $image->hasFile()) {
58
                $this->uploader->upload($image);
59
            }
60
        }
61
    }
62
}
63