Completed
Push — master ( 8253ba...a023d2 )
by Daniel
16:49 queued 05:23
created

EntitySubscriber::processUpdatedEntities()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 19
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 42.3281

Importance

Changes 0
Metric Value
cc 8
eloc 15
nc 9
nop 1
dl 0
loc 19
ccs 3
cts 16
cp 0.1875
crap 42.3281
rs 7.7777
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\EventListener\Doctrine;
4
5
use Doctrine\Common\EventSubscriber;
6
use Doctrine\DBAL\Platforms\SqlitePlatform;
7
use Doctrine\ORM\Event\OnFlushEventArgs;
8
use Doctrine\ORM\UnitOfWork;
9
// use Enqueue\Client\TraceableProducer;
10
use Liip\ImagineBundle\Async\Commands;
11
use Liip\ImagineBundle\Async\ResolveCache;
12
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
13
use Liip\ImagineBundle\Service\FilterService;
14
use Silverback\ApiComponentBundle\Entity\Component\FileInterface;
15
use Silverback\ApiComponentBundle\Entity\SortableInterface;
16
use Silverback\ApiComponentBundle\Serializer\ApiNormalizer;
17
18
/**
19
 * Class FileEntitySubscriber
20
 * @package Silverback\ApiComponentBundle\EventListener
21
 * @author Daniel West <[email protected]>
22
 */
23
class EntitySubscriber implements EventSubscriber
24
{
25
    /**
26
     * @var CacheManager
27
     */
28
    private $imagineCacheManager;
29
    /**
30
     * @var FilterService
31
     */
32
    private $filterService;
33
    /**
34
     * @var ApiNormalizer
35
     */
36
    private $fileNormalizer;
37
38
    // private $producer;
39
40
    /**
41
     * FileListener constructor.
42
     * @param CacheManager $imagineCacheManager
43
     * @param FilterService $filterService
44
     * @param ApiNormalizer $fileNormalizer
45
     */
46
    public function __construct(
47
        CacheManager $imagineCacheManager,
48
        FilterService $filterService,
49
        ApiNormalizer $fileNormalizer //,
50
        // TraceableProducer $producer
51
    ) {
52
        $this->imagineCacheManager = $imagineCacheManager;
53
        $this->fileNormalizer = $fileNormalizer;
54
        $this->filterService = $filterService;
55
        // $this->producer = $producer;
56
    }
57
58
    /**
59
     * @return array
60
     */
61
    public function getSubscribedEvents(): array
62
    {
63
        return [
64
            'onFlush'
65
        ];
66
    }
67
68
    /**
69
     * @param OnFlushEventArgs $eventArgs
70
     * @throws \Enqueue\Rpc\TimeoutException
71
     * @throws \Doctrine\DBAL\DBALException
72
     */
73 1
    public function onFlush(OnFlushEventArgs $eventArgs): void
74
    {
75 1
        $entityManager = $eventArgs->getEntityManager();
76 1
        if ($entityManager->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
77 1
            $entityManager->getConnection()->exec('PRAGMA foreign_keys = ON;');
78
        }
79 1
        $unitOfWork = $entityManager->getUnitOfWork();
80 1
        $this->processNewEntities($unitOfWork);
81 1
        $this->processUpdatedEntities($unitOfWork);
82 1
        $this->processDeletedEntities($unitOfWork);
83 1
    }
84
85
    /**
86
     * @param UnitOfWork $unitOfWork
87
     */
88 1
    private function processNewEntities(UnitOfWork $unitOfWork): void
89
    {
90 1
        $newEntities = $unitOfWork->getScheduledEntityInsertions();
91 1
        foreach ($newEntities as $entity) {
92 1
            if ($entity instanceof SortableInterface) {
93
                try {
94
                    $entity->getSort();
95
                } catch (\TypeError $e) {
96
                    $entity->setSort($entity->calculateSort(true));
97
                }
98
            }
99
            if (
100 1
                $entity instanceof FileInterface &&
101 1
                $this->fileNormalizer->isImagineSupportedFile($entity->getFilePath())
102
            ) {
103 1
                $this->createFilteredImages($entity);
104
                // $this->sendCommand($entity);
105
            }
106
        }
107 1
    }
108
109
    /**
110
     * @param UnitOfWork $unitOfWork
111
     */
112 1
    private function processUpdatedEntities(UnitOfWork $unitOfWork): void
113
    {
114 1
        $updatedEntities = $unitOfWork->getScheduledEntityUpdates();
115 1
        foreach ($updatedEntities as $entity) {
116
            if ($entity instanceof FileInterface) {
117
                $changes = $unitOfWork->getEntityChangeSet($entity);
118
                if (!\is_array($changes)) {
119
                    return;
120
                }
121
                if (array_key_exists('filePath', $changes)) {
122
                    $fpChanges = $changes['filePath'];
123
                    $previousValueForField = $fpChanges[0] ?? null;
124
                    $newValueForField = $fpChanges[1] ?? null;
125
                    if ($previousValueForField !== $newValueForField) {
126
                        if ($this->fileNormalizer->isImagineSupportedFile($previousValueForField)) {
127
                            $this->imagineCacheManager->remove($previousValueForField);
128
                        }
129
                        if ($this->fileNormalizer->isImagineSupportedFile($newValueForField)) {
130
                            $this->createFilteredImages($entity);
131
                            // $this->sendCommand($entity);
132
                        }
133
                    }
134
                }
135
            }
136
        }
137 1
    }
138
139
    /**
140
     * @param UnitOfWork $unitOfWork
141
     */
142 1
    private function processDeletedEntities(UnitOfWork $unitOfWork): void
143
    {
144 1
        $deletedEntities = $unitOfWork->getScheduledEntityDeletions();
145 1
        foreach ($deletedEntities as $entity) {
146
            if (
147
                $entity instanceof FileInterface &&
148
                $this->fileNormalizer->isImagineSupportedFile($entity->getFilePath())
149
            ) {
150
                $this->imagineCacheManager->remove($entity->getFilePath());
151
            }
152
        }
153 1
    }
154
155
    private function createFilteredImages(FileInterface $file): void
156
    {
157
        $filters = $file::getImagineFilters();
158
        foreach ($filters as $filter) {
159
            $this->filterService->getUrlOfFilteredImage($file->getFilePath(), $filter);
160
        }
161
    }
162
163
    /*
164
    private function sendCommand(FileInterface $file): void
165
    {
166
        $this->producer
167
            ->sendCommand(
168
                Commands::RESOLVE_CACHE,
169
                new ResolveCache($file->getFilePath(), $file::getImagineFilters()),
170
                true
171
            )
172
            ->receive(20000)
173
        ;
174
    }
175
    */
176
}
177