Completed
Pull Request — master (#34)
by Artem
07:34
created

JmsSerializerSubscriber::getHostUrl()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8333
c 0
b 0
f 0
cc 7
nc 3
nop 0
1
<?php
2
/*
3
 * This file is part of the FreshVichUploaderSerializationBundle
4
 *
5
 * (c) Artem Henvald <[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
declare(strict_types=1);
12
13
namespace Fresh\VichUploaderSerializationBundle\EventListener;
14
15
use Doctrine\Common\Annotations\Reader;
16
use Doctrine\Common\Util\ClassUtils;
17
use Doctrine\Persistence\Proxy;
18
use Fresh\VichUploaderSerializationBundle\Annotation\VichSerializableClass;
19
use Fresh\VichUploaderSerializationBundle\Annotation\VichSerializableField;
20
use Fresh\VichUploaderSerializationBundle\Exception\IncompatibleUploadableAndSerializableFieldAnnotationException;
21
use JMS\Serializer\EventDispatcher\Events;
22
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
23
use JMS\Serializer\EventDispatcher\ObjectEvent;
24
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
25
use Psr\Log\LoggerInterface;
26
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
27
use Symfony\Component\Routing\RequestContext;
28
use Vich\UploaderBundle\Mapping\Annotation\UploadableField;
29
use Vich\UploaderBundle\Storage\StorageInterface;
30
31
/**
32
 * JmsSerializerSubscriber.
33
 *
34
 * @author Artem Henvald <[email protected]>
35
 */
36
class JmsSerializerSubscriber implements EventSubscriberInterface
37
{
38
    private const HTTP_PORT = 80;
39
    private const HTTPS_PORT = 443;
40
41
    /** @var StorageInterface */
42
    private $storage;
43
44
    /** @var RequestContext */
45
    private $requestContext;
46
47
    /** @var Reader */
48
    private $annotationReader;
49
50
    /** @var PropertyAccessorInterface */
51
    private $propertyAccessor;
52
53
    /** @var LoggerInterface */
54
    private $logger;
55
56
    /** @var mixed[] */
57
    private $serializedObjects = [];
58
59
    /**
60
     * @param StorageInterface          $storage
61
     * @param RequestContext            $requestContext
62
     * @param Reader                    $annotationReader
63
     * @param PropertyAccessorInterface $propertyAccessor
64
     * @param LoggerInterface           $logger
65
     */
66
    public function __construct(StorageInterface $storage, RequestContext $requestContext, Reader $annotationReader, PropertyAccessorInterface $propertyAccessor, LoggerInterface $logger)
67
    {
68
        $this->storage = $storage;
69
        $this->requestContext = $requestContext;
70
        $this->annotationReader = $annotationReader;
71
        $this->propertyAccessor = $propertyAccessor;
72
        $this->logger = $logger;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public static function getSubscribedEvents(): \Generator
79
    {
80
        yield => ['event' => Events::PRE_SERIALIZE, 'method' => 'onPreSerialize'];
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_DOUBLE_ARROW
Loading history...
81
        yield => ['event' => Events::POST_SERIALIZE, 'method' => 'onPostSerialize'];
82
    }
83
84
    /**
85
     * @param PreSerializeEvent $event
86
     *
87
     * @throws IncompatibleUploadableAndSerializableFieldAnnotationException
88
     */
89
    public function onPreSerialize(PreSerializeEvent $event): void
90
    {
91
        $object = $event->getObject();
92
        if (!\is_object($object)) {
93
            return;
94
        }
95
96
        if ($object instanceof Proxy && !$object->__isInitialized()) {
97
            $object->__load();
98
        }
99
100
        $objectUid = \spl_object_hash($object);
101
        if (\array_key_exists($objectUid, $this->serializedObjects)) {
102
            return;
103
        }
104
105
        /** @var class-string<object> $className */
106
        $className = ClassUtils::getClass($object);
107
108
        $classAnnotation = $this->annotationReader->getClassAnnotation(
109
            new \ReflectionClass($className),
110
            VichSerializableClass::class
111
        );
112
113
        if ($classAnnotation instanceof VichSerializableClass) {
114
            $reflectionClass = ClassUtils::newReflectionClass(\get_class($object));
115
            $this->logger->debug(\sprintf(
116
                'Found @VichSerializableClass annotation for the class "%s"',
117
                $reflectionClass->getName()
118
            ));
119
120
            foreach ($reflectionClass->getProperties() as $property) {
121
                $vichSerializableAnnotation = $this->annotationReader->getPropertyAnnotation($property, VichSerializableField::class);
122
123
                if ($vichSerializableAnnotation instanceof VichSerializableField) {
124
                    $vichUploadableFileAnnotation = $this->annotationReader->getPropertyAnnotation($property, UploadableField::class);
125
126
                    if ($vichUploadableFileAnnotation instanceof UploadableField) {
127
                        $exceptionMessage = \sprintf(
128
                            'The field "%s" in the class "%s" cannot have @UploadableField and @VichSerializableField annotations at the same moment.',
129
                            $property->getName(),
130
                            $reflectionClass->getName()
131
                        );
132
133
                        throw new IncompatibleUploadableAndSerializableFieldAnnotationException($exceptionMessage);
134
                    }
135
                    $this->logger->debug(\sprintf(
136
                        'Found @VichSerializableField annotation for the field "%s" in the class "%s"',
137
                        $property->getName(),
138
                        $reflectionClass->getName()
139
                    ));
140
141
                    $uri = null;
142
                    $property->setAccessible(true);
143
144
                    if ($property->getValue($event->getObject())) {
145
                        $uri = $this->storage->resolveUri($object, $vichSerializableAnnotation->getField());
146
                        if ($vichSerializableAnnotation->isIncludeHost() && false === \filter_var($uri, FILTER_VALIDATE_URL)) {
147
                            $uri = $this->getHostUrl().$uri;
148
                        }
149
                    }
150
                    $this->serializedObjects[$objectUid][$property->getName()] = $property->getValue($event->getObject());
151
                    $property->setValue($object, $uri);
152
                }
153
            }
154
        }
155
    }
156
157
    /**
158
     * @param ObjectEvent $event
159
     */
160
    public function onPostSerialize(ObjectEvent $event): void
161
    {
162
        $object = $event->getObject();
163
        if (!\is_object($object)) {
164
            return;
165
        }
166
167
        if ($object instanceof Proxy && !$object->__isInitialized()) {
168
            $object->__load();
169
        }
170
171
        $objectUid = \spl_object_hash($object);
172
        if (!\array_key_exists($objectUid, $this->serializedObjects)) {
173
            return;
174
        }
175
176
        foreach ($this->serializedObjects[$objectUid] as $propertyName => $propertyValue) {
177
            $this->propertyAccessor->setValue($object, $propertyName, $propertyValue);
178
        }
179
        unset($this->serializedObjects[$objectUid]);
180
    }
181
182
    /**
183
     * Get host url (scheme://host:port).
184
     *
185
     * @return string
186
     */
187
    private function getHostUrl(): string
188
    {
189
        $scheme = $this->requestContext->getScheme();
190
        $url = $scheme.'://'.$this->requestContext->getHost();
191
192
        $httpPort = $this->requestContext->getHttpPort();
193
        if ('http' === $scheme && $httpPort && self::HTTP_PORT !== $httpPort) {
194
            return $url.':'.$httpPort;
195
        }
196
197
        $httpsPort = $this->requestContext->getHttpsPort();
198
        if ('https' === $scheme && $httpsPort && self::HTTPS_PORT !== $httpsPort) {
199
            return $url.':'.$httpsPort;
200
        }
201
202
        return $url;
203
    }
204
}
205