Completed
Push — master ( f4f5d5...b9c330 )
by Bukashk0zzz
02:08
created

JmsPreSerializeListener::onPreSerialize()   C

Complexity

Conditions 12
Paths 8

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 40
rs 5.1612
cc 12
eloc 23
nc 8
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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 Doctrine\Common\Util\ClassUtils;
16
use JMS\Serializer\EventDispatcher\ObjectEvent;
17
18
/**
19
 * JmsPreSerializeListener
20
 *
21
 * @author Denis Golubovskiy <[email protected]>
22
 */
23
class JmsPreSerializeListener extends JmsSerializeListenerAbstract
24
{
25
    /**
26
     * @var array $serializedObjects Pre Serialized objects
27
     */
28
    private $preSerializedObjects = [];
29
30
    /**
31
     * On pre serialize
32
     *
33
     * @param ObjectEvent $event Event
34
     */
35
    public function onPreSerialize(ObjectEvent $event)
36
    {
37
        $object = $this->getObject($event);
38
        $objectUid = spl_object_hash($object);
39
40
        if (in_array($objectUid, $this->preSerializedObjects, true)) {
41
            return;
42
        }
43
44
        $classAnnotation = $this->annotationReader->getClassAnnotation(
45
            new \ReflectionClass(ClassUtils::getClass($object)),
46
            LiipImagineSerializableClass::class
47
        );
48
49
        if ($classAnnotation instanceof LiipImagineSerializableClass) {
50
            $reflectionClass = ClassUtils::newReflectionClass(get_class($object));
51
52
            foreach ($reflectionClass->getProperties() as $property) {
53
                $liipAnnotation = $this->annotationReader->getPropertyAnnotation($property, LiipImagineSerializableField::class);
54
                $property->setAccessible(true);
55
56
                if ($liipAnnotation instanceof LiipImagineSerializableField && $value = $property->getValue($object)) {
57
                    $vichField = $liipAnnotation->getVichUploaderField();
58
59
                    if (!$liipAnnotation->getVirtualField()) {
60
                        $property->setValue($object, $this->serializeValue($liipAnnotation, $object, $value));
61
                    } elseif ($vichField && array_key_exists('vichUploaderSerialize', $this->config) && $this->config['vichUploaderSerialize']) {
62
                        $originalImageUri = $this->vichStorage->resolveUri($object, $vichField);
63
64
                        if (array_key_exists('includeHost', $this->config) && $this->config['includeHost']) {
65
                            $originalImageUri = $this->getHostUrl().$originalImageUri;
66
                        }
67
                        $property->setValue($object, $originalImageUri);
68
                    }
69
                }
70
            }
71
72
            $this->preSerializedObjects[$objectUid] = $objectUid;
73
        }
74
    }
75
}
76