Completed
Pull Request — 2.0 (#922)
by
unknown
02:29
created

ResolveCacheProcessor::getSubscribedQueues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\ProducerInterface;
15
use Enqueue\Client\TopicSubscriberInterface;
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 Liip\ImagineBundle\Imagine\Filter\FilterManager;
22
use Liip\ImagineBundle\Service\FilterService;
23
24
class ResolveCacheProcessor implements PsrProcessor, TopicSubscriberInterface, QueueSubscriberInterface
25
{
26
    /**
27
     * @var FilterManager
28
     */
29
    private $filterManager;
30
31
    /**
32
     * @var FilterService
33
     */
34
    private $filterService;
35
36
    /**
37
     * @var ProducerInterface
38
     */
39
    private $producer;
40
41
    /**
42
     * @param FilterManager     $filterManager
43
     * @param FilterService     $filterService
44
     * @param ProducerInterface $producer
45
     */
46
    public function __construct(
47
        FilterManager $filterManager,
48
        FilterService $filterService,
49
        ProducerInterface $producer
50
    ) {
51
        $this->filterManager = $filterManager;
52
        $this->filterService = $filterService;
53
        $this->producer = $producer;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function process(PsrMessage $psrMessage, PsrContext $psrContext)
60
    {
61
        try {
62
            $message = ResolveCache::jsonDeserialize($psrMessage->getBody());
63
        } catch (\Exception $e) {
64
            return Result::reject($e->getMessage());
65
        }
66
67
        $filters = $message->getFilters() ?: array_keys($this->filterManager->getFilterConfiguration()->all());
68
        $path = $message->getPath();
69
        $results = [];
70
        foreach ($filters as $filter) {
71
            if ($message->isForce()) {
72
                $this->filterService->bustCache($path, $filter);
73
            }
74
75
            $results[$filter] = $this->filterService->getUrlOfFilteredImage($path, $filter);
76
        }
77
78
        $this->producer->send(Topics::CACHE_RESOLVED, new CacheResolved($path, $results));
79
80
        return self::ACK;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public static function getSubscribedTopics(): array
87
    {
88
        return [
89
            Topics::RESOLVE_CACHE => ['queueName' => Topics::RESOLVE_CACHE,  'queueNameHardcoded' => true],
90
        ];
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public static function getSubscribedQueues(): array
97
    {
98
        return [Topics::RESOLVE_CACHE];
99
    }
100
}
101