Completed
Push — 2.0 ( 057aa6...b272ae )
by Maksim
02:01
created

ResolveCacheProcessor::process()   B

Complexity

Conditions 5
Paths 35

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 8.439
c 0
b 0
f 0
cc 5
eloc 19
nc 35
nop 2
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 Interop\Queue\PsrContext;
19
use Interop\Queue\PsrMessage;
20
use Interop\Queue\PsrProcessor;
21
use Enqueue\Util\JSON;
22
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
23
use Liip\ImagineBundle\Service\FilterService;
24
25
final class ResolveCacheProcessor implements PsrProcessor, 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
    /**
43
     * @param FilterManager     $filterManager
44
     * @param FilterService     $filterService
45
     * @param ProducerInterface $producer
46
     */
47
    public function __construct(
48
        FilterManager $filterManager,
49
        FilterService $filterService,
50
        ProducerInterface $producer
51
    ) {
52
        $this->filterManager = $filterManager;
53
        $this->filterService = $filterService;
54
        $this->producer = $producer;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function process(PsrMessage $psrMessage, PsrContext $psrContext)
61
    {
62
        try {
63
            $message = ResolveCache::jsonDeserialize($psrMessage->getBody());
64
65
            $filters = $message->getFilters() ?: array_keys($this->filterManager->getFilterConfiguration()->all());
66
            $path = $message->getPath();
67
            $results = [];
68
            foreach ($filters as $filter) {
69
                if ($message->isForce()) {
70
                    $this->filterService->bustCache($path, $filter);
71
                }
72
73
                $results[$filter] = $this->filterService->getUrlOfFilteredImage($path, $filter);
74
            }
75
76
            $this->producer->sendEvent(Topics::CACHE_RESOLVED, new CacheResolved($path, $results));
77
78
            return Result::reply($psrContext->createMessage(JSON::encode([
79
                'status' => true,
80
                'results' => $results,
81
            ])));
82
        } catch (\Exception $e) {
83
            return Result::reply($psrContext->createMessage(JSON::encode([
84
                'status' => false,
85
                'exception' => $e->getMessage(),
86
            ])), Result::REJECT, $e->getMessage());
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public static function getSubscribedCommand(): array
94
    {
95
        return [
96
            'processorName' => Commands::RESOLVE_CACHE,
97
            'queueName' => Commands::RESOLVE_CACHE,
98
            'queueNameHardcoded' => true,
99
            'exclusive' => true,
100
        ];
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106
    public static function getSubscribedQueues(): array
107
    {
108
        return [Commands::RESOLVE_CACHE];
109
    }
110
}
111