Completed
Push — 2.0 ( d9081a...4a1e3c )
by Rob
11:15
created

ResolveCacheProcessor::getSubscribedTopics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
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\Cache\CacheManager;
22
use Liip\ImagineBundle\Imagine\Data\DataManager;
23
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
24
25
class ResolveCacheProcessor implements PsrProcessor, TopicSubscriberInterface, QueueSubscriberInterface
26
{
27
    /**
28
     * @var CacheManager
29
     */
30
    private $cacheManager;
31
32
    /**
33
     * @var FilterManager
34
     */
35
    private $filterManager;
36
37
    /**
38
     * @var DataManager
39
     */
40
    private $dataManager;
41
42
    /**
43
     * @var ProducerInterface
44
     */
45
    private $producer;
46
47
    /**
48
     * @param CacheManager      $cacheManager
49
     * @param FilterManager     $filterManager
50
     * @param DataManager       $dataManager
51
     * @param ProducerInterface $producer
52
     */
53
    public function __construct(
54
        CacheManager $cacheManager,
55
        FilterManager $filterManager,
56
        DataManager $dataManager,
57
        ProducerInterface $producer
58
    ) {
59
        $this->cacheManager = $cacheManager;
60
        $this->filterManager = $filterManager;
61
        $this->dataManager = $dataManager;
62
        $this->producer = $producer;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function process(PsrMessage $psrMessage, PsrContext $psrContext)
69
    {
70
        try {
71
            $message = ResolveCache::jsonDeserialize($psrMessage->getBody());
72
        } catch (\Exception $e) {
73
            return Result::reject($e->getMessage());
74
        }
75
76
        $filters = $message->getFilters() ?: array_keys($this->filterManager->getFilterConfiguration()->all());
77
        $path = $message->getPath();
78
        $results = [];
79
        foreach ($filters as $filter) {
80
            if ($this->cacheManager->isStored($path, $filter) && $message->isForce()) {
81
                $this->cacheManager->remove($path, $filter);
82
            }
83
84
            if (false == $this->cacheManager->isStored($path, $filter)) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
85
                $binary = $this->dataManager->find($filter, $path);
86
                $this->cacheManager->store(
87
                    $this->filterManager->applyFilter($binary, $filter),
88
                    $path,
89
                    $filter
90
                );
91
            }
92
93
            $results[$filter] = $this->cacheManager->resolve($path, $filter);
94
        }
95
96
        $this->producer->send(Topics::CACHE_RESOLVED, new CacheResolved($path, $results));
97
98
        return self::ACK;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public static function getSubscribedTopics(): array
105
    {
106
        return [
107
            Topics::RESOLVE_CACHE => ['queueName' => Topics::RESOLVE_CACHE,  'queueNameHardcoded' => true],
108
        ];
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public static function getSubscribedQueues(): array
115
    {
116
        return [Topics::RESOLVE_CACHE];
117
    }
118
}
119