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