Completed
Pull Request — master (#8)
by
unknown
21:55
created

JmsSerializeListenerAbstract::serializeValue()   D

Complexity

Conditions 10
Paths 24

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 37
rs 4.8196
cc 10
eloc 22
nc 24
nop 3

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\LiipImagineSerializableField;
14
use Doctrine\Common\Annotations\CachedReader;
15
use JMS\Serializer\EventDispatcher\ObjectEvent;
16
use Symfony\Component\Routing\RequestContext;
17
use Doctrine\Common\Persistence\Proxy;
18
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
19
use Vich\UploaderBundle\Storage\StorageInterface;
20
21
/**
22
 * JmsSerializeListenerAbstract
23
 *
24
 * @author Denis Golubovskiy <[email protected]>
25
 */
26
class JmsSerializeListenerAbstract
27
{
28
    /**
29
     * @var RequestContext $requestContext Request context
30
     */
31
    protected $requestContext;
32
33
    /**
34
     * @var CachedReader $annotationReader Cached annotation reader
35
     */
36
    protected $annotationReader;
37
38
    /**
39
     * @var CacheManager $cacheManager LiipImagineBundle Cache Manager
40
     */
41
    protected $cacheManager;
42
43
    /**
44
     * @var StorageInterface $storage Vich storage
45
     */
46
    protected $vichStorage;
47
48
    /**
49
     * @var array $config Bundle config
50
     */
51
    protected $config;
52
53
    /** @noinspection MoreThanThreeArgumentsInspection
54
     *
55
     * JmsSerializeListenerAbstract constructor.
56
     *
57
     * @param RequestContext   $requestContext
58
     * @param CachedReader     $annotationReader
59
     * @param CacheManager     $cacheManager
60
     * @param StorageInterface $vichStorage
61
     * @param array            $config
62
     */
63
    public function __construct(
64
        RequestContext $requestContext,
65
        CachedReader $annotationReader,
66
        CacheManager $cacheManager,
67
        StorageInterface $vichStorage,
68
        array $config
69
    ) {
70
        $this->requestContext = $requestContext;
71
        $this->annotationReader = $annotationReader;
72
        $this->cacheManager = $cacheManager;
73
        $this->vichStorage = $vichStorage;
74
        $this->config = $config;
75
    }
76
77
    /**
78
     * @param ObjectEvent $event Event
79
     * @return mixed
80
     */
81
    protected function getObject(ObjectEvent $event)
82
    {
83
        $object = $event->getObject();
84
85
        if ($object instanceof Proxy
86
            && ! $object->__isInitialized()
87
        ) {
88
            $object->__load();
89
        }
90
91
        return $object;
92
    }
93
94
    /** @noinspection GenericObjectTypeUsageInspection
95
     * @param LiipImagineSerializableField $liipAnnotation
96
     * @param object $object Serialized object
97
     * @param string $value Value of field
98
     * @return array|string
99
     */
100
    protected function serializeValue(LiipImagineSerializableField $liipAnnotation, $object, $value)
101
    {
102
        if ($vichField = $liipAnnotation->getVichUploaderField()) {
103
            $value = $this->vichStorage->resolveUri($object, $vichField);
104
        }
105
106
        $result = [];
107
        if (array_key_exists('includeOriginal', $this->config) && $this->config['includeOriginal']) {
108
            if (
109
                array_key_exists('includeHostForOriginal', $this->config) &&
110
                $this->config['includeHostForOriginal'] &&
111
                $liipAnnotation->getVichUploaderField()
112
            ) {
113
                $result['original'] = $this->getHostUrl().$value;
114
            } else {
115
                $result['original'] = $value;
116
            }
117
        }
118
119
        $filters = $liipAnnotation->getFilter();
120
        if (is_array($filters)) {
121
            foreach ($filters as $filter) {
122
                $result[$filter] = $this->cacheManager->getBrowserPath($value, $filter);
123
            }
124
125
            return $result;
126
        }
127
128
        $filtered = $this->cacheManager->getBrowserPath($value, $filters);
129
        if (count($result) !== 0) {
130
            $result[$filters] = $filtered;
131
132
            return $result;
133
        }
134
135
        return $filtered;
136
    }
137
138
    /**
139
     * Get host url (scheme, host, port)
140
     *
141
     * @return string Host url
142
     */
143
    protected function getHostUrl()
144
    {
145
        $url = $this->requestContext->getScheme().'://'.$this->requestContext->getHost();
146
147
        if ($this->requestContext->getScheme() === 'http' && $this->requestContext->getHttpPort() && $this->requestContext->getHttpPort() !== 80) {
148
            $url .= ':'.$this->requestContext->getHttpPort();
149
        } elseif ($this->requestContext->getScheme() === 'https' && $this->requestContext->getHttpsPort() && $this->requestContext->getHttpsPort() !== 443) {
150
            $url .= ':'.$this->requestContext->getHttpsPort();
151
        }
152
153
        return $url;
154
    }
155
}
156