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