|
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( |
|
|
|
|
|
|
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
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: