JmsPreSerializeListener   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 5
dl 0
loc 68
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C onPreSerialize() 0 53 15
1
<?php declare(strict_types = 1);
2
/*
3
 * This file is part of the Bukashk0zzzLiipImagineSerializationBundle
4
 *
5
 * (c) Denis Golubovskiy <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Bukashk0zzz\LiipImagineSerializationBundle\EventListener;
12
13
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation\LiipImagineSerializableClass;
14
use Bukashk0zzz\LiipImagineSerializationBundle\Annotation\LiipImagineSerializableField;
15
use Bukashk0zzz\LiipImagineSerializationBundle\Normalizer\UrlNormalizerInterface;
16
use Doctrine\Common\Util\ClassUtils;
17
use JMS\Serializer\EventDispatcher\ObjectEvent;
18
use Vich\UploaderBundle\Mapping\Annotation\UploadableField;
19
20
/**
21
 * JmsPreSerializeListener.
22
 */
23
class JmsPreSerializeListener extends JmsSerializeListenerAbstract
24
{
25
    /**
26
     * Cache attributes already processed (in case of collection serialization).
27
     *
28
     * @var mixed[]
29
     */
30
    private $cache = [];
31
32
    /**
33
     * On pre serialize.
34
     *
35
     * @param ObjectEvent $event Event
36
     */
37
    public function onPreSerialize(ObjectEvent $event): void
38
    {
39
        $object = $this->getObject($event);
40
41
        $classAnnotation = $this->annotationReader->getClassAnnotation(
42
            new \ReflectionClass(ClassUtils::getClass($object)),
43
            LiipImagineSerializableClass::class
44
        );
45
46
        if ($classAnnotation instanceof LiipImagineSerializableClass) {
47
            $reflectionClass = ClassUtils::newReflectionClass(\get_class($object));
48
49
            foreach ($reflectionClass->getProperties() as $property) {
50
                $liipAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class);
51
                $property->setAccessible(true);
52
                if ($liipAnnotation instanceof LiipImagineSerializableField) {
53
                    $value = $property->getValue($object);
54
55
                    if ($value && !\is_array($value)) {
56
                        $vichField = $liipAnnotation->getVichUploaderField();
57
58
                        $cacheKey = null;
59
                        if ($vichField) {
60
                            $uriComponents = \explode('/', $value);
61
                            $vichProperty = $reflectionClass->getProperty($vichField);
62
                            $vichAnnotation = $this->annotationReader->getPropertyAnnotation($vichProperty, UploadableField::class);
63
                            $cacheKey = $vichField.\array_pop($uriComponents).$vichAnnotation->getMapping();
64
65
                            if (\array_key_exists($cacheKey, $this->cache)) {
66
                                $property->setValue($object, $this->cache[$cacheKey]);
67
                                continue;
68
                            }
69
                        }
70
71
                        if (!$liipAnnotation->getVirtualField()) {
72
                            $property->setValue($object, $this->serializeValue($liipAnnotation, $object, $value));
73
                        } elseif ($vichField && \array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize']) {
74
                            $originalImageUri = $this->vichStorage->resolveUri($object, $vichField);
75
76
                            if (\array_key_exists('includeHost', $this->config) && $this->config['includeHost']) {
77
                                $originalImageUri = $this->getHostUrl().$originalImageUri;
78
                            }
79
                            $property->setValue($object, $this->normalizeUrl($originalImageUri, UrlNormalizerInterface::TYPE_ORIGIN));
80
                        }
81
82
                        if ($vichField) {
83
                            $this->cache[$cacheKey] = $property->getValue($object);
84
                        }
85
                    }
86
                }
87
            }
88
        }
89
    }
90
}
91