Completed
Push — 1.0 ( 09dc0e...a109cf )
by Maksim
9s
created

ResolveCacheProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
namespace Liip\ImagineBundle\Async;
3
4
use Enqueue\Client\ProducerInterface;
5
use Enqueue\Client\TopicSubscriberInterface;
6
use Enqueue\Consumption\QueueSubscriberInterface;
7
use Enqueue\Consumption\Result;
8
use Enqueue\Psr\PsrContext;
9
use Enqueue\Psr\PsrMessage;
10
use Enqueue\Psr\PsrProcessor;
11
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
12
use Liip\ImagineBundle\Imagine\Data\DataManager;
13
use Liip\ImagineBundle\Imagine\Filter\FilterManager;
14
15
class ResolveCacheProcessor implements PsrProcessor, TopicSubscriberInterface, QueueSubscriberInterface
16
{
17
    /**
18
     * @var CacheManager
19
     */
20
    private $cacheManager;
21
22
    /**
23
     * @var FilterManager
24
     */
25
    private $filterManager;
26
27
    /**
28
     * @var DataManager
29
     */
30
    private $dataManager;
31
32
    /**
33
     * @var ProducerInterface
34
     */
35
    private $producer;
36
37
    /**
38
     * @param CacheManager $cacheManager
39
     * @param FilterManager $filterManager
40
     * @param DataManager $dataManager
41
     * @param ProducerInterface $producer
42
     */
43
    public function __construct(
44
        CacheManager $cacheManager,
45
        FilterManager $filterManager,
46
        DataManager $dataManager,
47
        ProducerInterface $producer
48
    ) {
49
        $this->cacheManager = $cacheManager;
50
        $this->filterManager = $filterManager;
51
        $this->dataManager = $dataManager;
52
        $this->producer = $producer;
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function process(PsrMessage $psrMessage, PsrContext $psrContext)
59
    {
60
        try {
61
            $message = ResolveCache::jsonDeserialize($psrMessage->getBody());
62
        } catch (\Exception $e) {
63
            return Result::reject($e->getMessage());
64
        }
65
66
        $filters = $message->getFilters() ?: array_keys($this->filterManager->getFilterConfiguration()->all());
67
        $path = $message->getPath();
68
        $results = array();
69
        foreach ($filters as $filter) {
70
            if ($this->cacheManager->isStored($path, $filter) && $message->isForce()) {
71
                $this->cacheManager->remove($path, $filter);
72
            }
73
74
            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...
75
                $binary = $this->dataManager->find($filter, $path);
76
                $this->cacheManager->store(
77
                    $this->filterManager->applyFilter($binary, $filter),
78
                    $path,
79
                    $filter
80
                );
81
            }
82
83
            $results[$filter] = $this->cacheManager->resolve($path, $filter);
84
        }
85
86
        $this->producer->send(Topics::CACHE_RESOLVED, new CacheResolved($path, $results));
87
88
        return self::ACK;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public static function getSubscribedTopics()
95
    {
96
        return array(
97
            Topics::RESOLVE_CACHE => array('queueName' => Topics::RESOLVE_CACHE,  'queueNameHardcoded' => true),
98
        );
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public static function getSubscribedQueues()
105
    {
106
        return array(Topics::RESOLVE_CACHE);
107
    }
108
}
109