ConsumerThumbnail   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 7
lcom 2
cbo 4
dl 0
loc 89
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A generatePublicUrl() 0 4 1
A __construct() 0 18 2
A generatePrivateUrl() 0 4 1
A generate() 0 26 2
A delete() 0 4 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\Thumbnail;
15
16
use Sonata\MediaBundle\Model\MediaInterface;
17
use Sonata\MediaBundle\Provider\MediaProviderInterface;
18
use Sonata\NotificationBundle\Backend\BackendInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
21
/**
22
 * @final since sonata-project/media-bundle 3.21.0
23
 */
24
class ConsumerThumbnail implements ThumbnailInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $id;
30
31
    /**
32
     * @var ThumbnailInterface
33
     */
34
    protected $thumbnail;
35
36
    /**
37
     * @var BackendInterface
38
     */
39
    protected $backend;
40
41
    /**
42
     * @var EventDispatcherInterface
43
     */
44
    protected $dispatcher;
45
46
    /**
47
     * NEXT_MAJOR: remove optional null for EventDispatcherInterface.
48
     *
49
     * @param string                   $id
50
     * @param EventDispatcherInterface $dispatcher
51
     */
52
    public function __construct($id, ThumbnailInterface $thumbnail, BackendInterface $backend, ?EventDispatcherInterface $dispatcher = null)
53
    {
54
        /*
55
         * NEXT_MAJOR: remove this check
56
         */
57
        if (null === $dispatcher) {
58
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
59
                'Since version 3.0, passing an empty parameter in argument 4 for __construct() in '.__CLASS__.' is
60
                 deprecated and the workaround for it will be removed in 4.0.',
61
                E_USER_DEPRECATED
62
            );
63
        }
64
65
        $this->id = $id;
66
        $this->thumbnail = $thumbnail;
67
        $this->backend = $backend;
68
        $this->dispatcher = $dispatcher;
69
    }
70
71
    public function generatePublicUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
72
    {
73
        return $this->thumbnail->generatePrivateUrl($provider, $media, $format);
74
    }
75
76
    public function generatePrivateUrl(MediaProviderInterface $provider, MediaInterface $media, $format)
77
    {
78
        return $this->thumbnail->generatePrivateUrl($provider, $media, $format);
79
    }
80
81
    public function generate(MediaProviderInterface $provider, MediaInterface $media): void
82
    {
83
        $backend = $this->backend;
84
        $id = $this->id;
85
86
        $publish = static function () use ($backend, $media, $id): void {
87
            $backend->createAndPublish('sonata.media.create_thumbnail', [
88
                'thumbnailId' => $id,
89
                'mediaId' => $media->getId(),
90
91
                // force this value as the message is sent inside a transaction,
92
                // so we have a race condition
93
                'providerReference' => $media->getProviderReference(),
94
            ]);
95
        };
96
97
        /*
98
         * NEXT_MAJOR: remove this check
99
         */
100
        if (null === $this->dispatcher) {
101
            $publish();
102
        } else {
103
            $this->dispatcher->addListener('kernel.finish_request', $publish);
104
            $this->dispatcher->addListener('console.terminate', $publish);
105
        }
106
    }
107
108
    public function delete(MediaProviderInterface $provider, MediaInterface $media, $formats = null)
109
    {
110
        return $this->thumbnail->delete($provider, $media, $formats);
111
    }
112
}
113