Passed
Push — master ( a023d2...6d474d )
by Daniel
06:11
created

ApiNormalizer::isImagineSupportedFile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3.0261

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 3.0261
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Silverback\ApiComponentBundle\Serializer;
4
5
use Liip\ImagineBundle\Imagine\Cache\CacheManager;
6
use Silverback\ApiComponentBundle\Entity\Content\Component\FileInterface;
7
use Silverback\ApiComponentBundle\Entity\Content\Component\Form\Form;
8
use Silverback\ApiComponentBundle\Factory\Entity\Content\Component\Form\FormViewFactory;
9
use Silverback\ApiComponentBundle\Imagine\FileSystemLoader;
10
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
11
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
12
use Symfony\Component\Serializer\SerializerAwareInterface;
13
use Symfony\Component\Serializer\SerializerInterface;
14
15
final class ApiNormalizer implements NormalizerInterface, DenormalizerInterface, SerializerAwareInterface
16
{
17
    private $decorated;
18
    private $imagineCacheManager;
19
    private $formViewFactory;
20
    private $fileSystemLoader;
21
22
    /**
23
     * FileNormalizer constructor.
24
     * @param NormalizerInterface $decorated
25
     * @param CacheManager $imagineCacheManager
26
     * @param FormViewFactory $formViewFactory
27
     * @param FileSystemLoader $fileSystemLoader
28
     */
29 10
    public function __construct(
30
        NormalizerInterface $decorated,
31
        CacheManager $imagineCacheManager,
32
        FormViewFactory $formViewFactory,
33
        FileSystemLoader $fileSystemLoader
34
    ) {
35 10
        if (!$decorated instanceof DenormalizerInterface) {
36
            throw new \InvalidArgumentException(sprintf('The decorated normalizer must implement the %s.', DenormalizerInterface::class));
37
        }
38 10
        $this->decorated = $decorated;
39 10
        $this->imagineCacheManager = $imagineCacheManager;
40 10
        $this->formViewFactory = $formViewFactory;
41 10
        $this->fileSystemLoader = $fileSystemLoader;
42 10
    }
43
44
    /**
45
     * @param mixed $data
46
     * @param string|null $format
47
     * @return bool
48
     */
49 3
    public function supportsNormalization($data, $format = null): bool
50
    {
51 3
        return $this->decorated->supportsNormalization($data, $format);
52
    }
53
54
    /**
55
     * @param $object
56
     * @param string|null $format
57
     * @param array $context
58
     * @return array|bool|float|int|string
59
     * @throws \Symfony\Component\Serializer\Exception\LogicException
60
     * @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException
61
     * @throws \Symfony\Component\Serializer\Exception\CircularReferenceException
62
     */
63 4
    public function normalize($object, $format = null, array $context = [])
64
    {
65 4
        $data = $this->decorated->normalize($object, $format, $context);
66
67 4
        if ($object instanceof FileInterface) {
68 1
            $data = array_merge($data, $this->getFileData($object));
69
        }
70 4
        if ($object instanceof Form) {
71 3
            $data['form'] = $this->formViewFactory->create($object);
72
        }
73
74 4
        return $data;
75
    }
76
77
    /**
78
     * @param FileInterface $object
79
     * @return array
80
     */
81 1
    private function getFileData(FileInterface $object): array
82
    {
83 1
        $data = [];
84 1
        $filePath = $object->getFilePath();
85 1
        if ($filePath) {
86 1
            if (false !== \exif_imagetype($filePath)) {
87 1
                [$width, $height] = getimagesize($filePath);
88
            } else {
89
                $width = $height = 0;
90
            }
91 1
            $data['width'] = $width;
92 1
            $data['height'] = $height;
93
94 1
            $supported = $this->isImagineSupportedFile($filePath);
95 1
            foreach ($object::getImagineFilters() as $returnKey => $filter) {
96 1
                $data[$returnKey] = $supported ? parse_url(
97 1
                    $this->imagineCacheManager->getBrowserPath($this->fileSystemLoader->getImaginePath($filePath), $filter),
98 1
                    PHP_URL_PATH
99 1
                ) : null;
100
            }
101
        }
102 1
        return $data;
103
    }
104
105
    /**
106
     * @param mixed $data
107
     * @param string $type
108
     * @param string|null $format
109
     * @return bool
110
     */
111 3
    public function supportsDenormalization($data, $type, $format = null): bool
112
    {
113 3
        return $this->decorated->supportsDenormalization($data, $type, $format);
114
    }
115
116
    /**
117
     * @param mixed $data
118
     * @param string $class
119
     * @param string|null $format
120
     * @param array $context
121
     * @return object
122
     * @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException
123
     * @throws \Symfony\Component\Serializer\Exception\RuntimeException
124
     * @throws \Symfony\Component\Serializer\Exception\LogicException
125
     * @throws \Symfony\Component\Serializer\Exception\InvalidArgumentException
126
     * @throws \Symfony\Component\Serializer\Exception\ExtraAttributesException
127
     * @throws \Symfony\Component\Serializer\Exception\BadMethodCallException
128
     * @throws \InvalidArgumentException
129
     */
130 1
    public function denormalize($data, $class, $format = null, array $context = [])
131
    {
132 1
        $context['allow_extra_attributes'] = $class === Form::class;
133 1
        return $this->decorated->denormalize($data, $class, $format, $context);
134
    }
135
136
    /**
137
     * @param SerializerInterface $serializer
138
     */
139 5
    public function setSerializer(SerializerInterface $serializer)
140
    {
141 5
        if ($this->decorated instanceof SerializerAwareInterface) {
142 5
            $this->decorated->setSerializer($serializer);
143
        }
144 5
    }
145
146
    /**
147
     * @param string $filePath
148
     * @return bool
149
     */
150 3
    public function isImagineSupportedFile(?string $filePath): bool
151
    {
152 3
        if (!$filePath) {
153
            return false;
154
        }
155
        try {
156 3
            $imageType = \exif_imagetype($filePath);
157 2
        } catch (\Exception $e) {
158 2
            return false;
159
        }
160 3
        return \in_array($imageType, [IMAGETYPE_JPEG, IMAGETYPE_JPEG2000, IMAGETYPE_PNG, IMAGETYPE_GIF], true);
161
    }
162
}
163