CreateThumbnailConsumer   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 23 3
A getThumbnail() 0 13 2
A __construct() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\MediaBundle\Consumer;
15
16
use Sonata\Doctrine\Model\ManagerInterface;
17
use Sonata\MediaBundle\Provider\Pool;
18
use Sonata\MediaBundle\Thumbnail\ThumbnailInterface;
19
use Sonata\NotificationBundle\Consumer\ConsumerEvent;
20
use Sonata\NotificationBundle\Consumer\ConsumerInterface;
21
use Sonata\NotificationBundle\Exception\HandlingException;
22
use Symfony\Component\DependencyInjection\ContainerInterface;
23
24
/**
25
 * @final since sonata-project/media-bundle 3.21.0
26
 */
27
class CreateThumbnailConsumer implements ConsumerInterface
28
{
29
    /**
30
     * @var ManagerInterface
31
     */
32
    protected $mediaManager;
33
34
    /**
35
     * @var Pool
36
     */
37
    protected $pool;
38
39
    /**
40
     * @var ContainerInterface
41
     */
42
    protected $container;
43
44
    public function __construct(ManagerInterface $mediaManager, Pool $pool, ContainerInterface $container)
45
    {
46
        $this->mediaManager = $mediaManager;
47
        $this->pool = $pool;
48
        $this->container = $container;
49
    }
50
51
    public function process(ConsumerEvent $event): void
52
    {
53
        $media = $this->mediaManager->findOneBy([
54
            'id' => $event->getMessage()->getValue('mediaId'),
55
        ]);
56
57
        if (!$media) {
58
            throw new HandlingException(sprintf('Media not found - id: %s', $event->getMessage()->getValue('mediaId')));
59
        }
60
61
        // solve race condition between message queue and database transaction
62
        $media->setProviderReference($event->getMessage()->getValue('providerReference'));
63
64
        try {
65
            $this->getThumbnail($event)->generate($this->pool->getProvider($media->getProviderName()), $media);
66
        } catch (\LogicException $e) {
67
            throw new HandlingException(
68
                sprintf('Error while generating exception for media.id: %s', $event->getMessage()->getValue('mediaId')),
69
                0,
70
                $e
71
            );
72
        }
73
    }
74
75
    /**
76
     * @return ThumbnailInterface
77
     */
78
    protected function getThumbnail(ConsumerEvent $event)
79
    {
80
        $thumbnail = $this->container->get($event->getMessage()->getValue('thumbnailId'));
81
82
        if (!$thumbnail instanceof ThumbnailInterface) {
83
            throw new HandlingException(sprintf(
84
                'Invalid thumbnail instance requested - id: %s',
85
                $event->getMessage()->getValue('thumbnailId')
86
            ));
87
        }
88
89
        return $thumbnail;
90
    }
91
}
92