ResolveCacheProcessor::getSubscribedQueues()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
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\Util\JSON;
19
use Interop\Queue\Context;
20
use Interop\Queue\Message;
21
use Interop\Queue\Processor;
22
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
23
use Liip\ImagineBundle\Service\FilterService;
24
25
final class ResolveCacheProcessor implements Processor, CommandSubscriberInterface, QueueSubscriberInterface
26
{
27
    /**
28
     * @var FilterManager
29
     */
30
    private $filterManager;
31
32
    /**
33
     * @var FilterService
34
     */
35
    private $filterService;
36
37
    /**
38
     * @var ProducerInterface
39
     */
40
    private $producer;
41
42
    public function __construct(
43
        FilterManager $filterManager,
44
        FilterService $filterService,
45
        ProducerInterface $producer
46
    ) {
47
        $this->filterManager = $filterManager;
48
        $this->filterService = $filterService;
49
        $this->producer = $producer;
50
    }
51
52
    public function process(Message $psrMessage, Context $psrContext)
53
    {
54
        try {
55
            $message = ResolveCache::jsonDeserialize($psrMessage->getBody());
56
57
            $filters = $message->getFilters() ?: array_keys($this->filterManager->getFilterConfiguration()->all());
58
            $path = $message->getPath();
59
            $results = [];
60
            foreach ($filters as $filter) {
61
                if ($message->isForce()) {
62
                    $this->filterService->bustCache($path, $filter);
63
                }
64
65
                $results[$filter] = $this->filterService->getUrlOfFilteredImage($path, $filter);
66
            }
67
68
            $this->producer->sendEvent(Topics::CACHE_RESOLVED, new CacheResolved($path, $results));
69
70
            return Result::reply($psrContext->createMessage(JSON::encode([
71
                'status' => true,
72
                'results' => $results,
73
            ])));
74
        } catch (\Exception $e) {
75
            return Result::reply($psrContext->createMessage(JSON::encode([
76
                'status' => false,
77
                'exception' => $e->getMessage(),
78
            ])), Result::REJECT, $e->getMessage());
79
        }
80
    }
81
82
    public static function getSubscribedCommand(): array
83
    {
84
        return [
85
            'command' => Commands::RESOLVE_CACHE,
86
            'queue' => Commands::RESOLVE_CACHE,
87
            'prefix_queue' => false,
88
            'exclusive' => true,
89
        ];
90
    }
91
92
    public static function getSubscribedQueues(): array
93
    {
94
        return [Commands::RESOLVE_CACHE];
95
    }
96
}
97