|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the `liip/LiipImagineBundle` project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Liip\ImagineBundle\Async; |
|
13
|
|
|
|
|
14
|
|
|
use Enqueue\Client\CommandSubscriberInterface; |
|
15
|
|
|
use Enqueue\Client\ProducerInterface; |
|
16
|
|
|
use Enqueue\Consumption\QueueSubscriberInterface; |
|
17
|
|
|
use Enqueue\Consumption\Result; |
|
18
|
|
|
use Enqueue\Psr\PsrContext; |
|
19
|
|
|
use Enqueue\Psr\PsrMessage; |
|
20
|
|
|
use Enqueue\Psr\PsrProcessor; |
|
21
|
|
|
use Enqueue\Util\JSON; |
|
22
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
23
|
|
|
use Liip\ImagineBundle\Imagine\Data\DataManager; |
|
24
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterManager; |
|
25
|
|
|
|
|
26
|
|
|
class ResolveCacheProcessor implements PsrProcessor, CommandSubscriberInterface, QueueSubscriberInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var CacheManager |
|
30
|
|
|
*/ |
|
31
|
|
|
private $cacheManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var FilterManager |
|
35
|
|
|
*/ |
|
36
|
|
|
private $filterManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var DataManager |
|
40
|
|
|
*/ |
|
41
|
|
|
private $dataManager; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ProducerInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
private $producer; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param CacheManager $cacheManager |
|
50
|
|
|
* @param FilterManager $filterManager |
|
51
|
|
|
* @param DataManager $dataManager |
|
52
|
|
|
* @param ProducerInterface $producer |
|
53
|
|
|
*/ |
|
54
|
|
|
public function __construct( |
|
55
|
|
|
CacheManager $cacheManager, |
|
56
|
|
|
FilterManager $filterManager, |
|
57
|
|
|
DataManager $dataManager, |
|
58
|
|
|
ProducerInterface $producer |
|
59
|
|
|
) { |
|
60
|
|
|
$this->cacheManager = $cacheManager; |
|
61
|
|
|
$this->filterManager = $filterManager; |
|
62
|
|
|
$this->dataManager = $dataManager; |
|
63
|
|
|
$this->producer = $producer; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritdoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function process(PsrMessage $psrMessage, PsrContext $psrContext) |
|
70
|
|
|
{ |
|
71
|
|
|
try { |
|
72
|
|
|
$message = ResolveCache::jsonDeserialize($psrMessage->getBody()); |
|
73
|
|
|
|
|
74
|
|
|
$filters = $message->getFilters() ?: array_keys($this->filterManager->getFilterConfiguration()->all()); |
|
75
|
|
|
$path = $message->getPath(); |
|
76
|
|
|
$results = []; |
|
77
|
|
|
foreach ($filters as $filter) { |
|
78
|
|
|
if ($this->cacheManager->isStored($path, $filter) && $message->isForce()) { |
|
79
|
|
|
$this->cacheManager->remove($path, $filter); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
if (false == $this->cacheManager->isStored($path, $filter)) { |
|
|
|
|
|
|
83
|
|
|
$binary = $this->dataManager->find($filter, $path); |
|
84
|
|
|
$this->cacheManager->store( |
|
85
|
|
|
$this->filterManager->applyFilter($binary, $filter), |
|
86
|
|
|
$path, |
|
87
|
|
|
$filter |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$results[$filter] = $this->cacheManager->resolve($path, $filter); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$this->producer->sendEvent(Topics::CACHE_RESOLVED, new CacheResolved($path, $results)); |
|
95
|
|
|
|
|
96
|
|
|
return Result::reply($psrContext->createMessage(JSON::encode([ |
|
97
|
|
|
'status' => true, |
|
98
|
|
|
'results' => $results, |
|
99
|
|
|
]))); |
|
100
|
|
|
|
|
101
|
|
|
} catch (\Exception $e) { |
|
102
|
|
|
return Result::reply($psrContext->createMessage(JSON::encode([ |
|
103
|
|
|
'status' => false, |
|
104
|
|
|
'exception' => $e->getMessage(), |
|
105
|
|
|
])), Result::REJECT, $e->getMessage()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* {@inheritdoc} |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function getSubscribedCommand(): array |
|
113
|
|
|
{ |
|
114
|
|
|
return [ |
|
115
|
|
|
'processorName' => Commands::RESOLVE_CACHE, |
|
116
|
|
|
'queueName' => Commands::RESOLVE_CACHE, |
|
117
|
|
|
'queueNameHardcoded' => true, |
|
118
|
|
|
'exclusive' => true, |
|
119
|
|
|
]; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public static function getSubscribedQueues(): array |
|
126
|
|
|
{ |
|
127
|
|
|
return [Commands::RESOLVE_CACHE]; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.