Passed
Push — master ( 24d2dd...159fd0 )
by Paul
04:04
created

src/Serializer/SymfonySerializerBuilder.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace CCT\Component\Rest\Serializer;
4
5
use CCT\Component\Rest\Config;
6
use CCT\Component\Rest\Serializer\Mapping\Loader\DirectoryLoader;
7
use Symfony\Component\Serializer\Encoder\EncoderInterface;
8
use Symfony\Component\Serializer\Encoder\JsonEncoder;
9
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
10
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
11
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
12
use Symfony\Component\Serializer\Normalizer\NormalizableInterface;
13
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
14
use Symfony\Component\Serializer\Serializer;
15
16
class SymfonySerializerBuilder implements SerializerBuilderInterface
17
{
18
    /**
19
     * @var Config
20
     */
21
    private $config;
22
23
    /**
24
     * @var array|EncoderInterface[]
25
     */
26
    private $encoders;
27
28
    /**
29
     * @var array|NormalizableInterface[]
30
     */
31
    private $normalizers;
32
33
    /**
34
     * SymfonySerializerBuilder constructor.
35
     *
36
     * @param Config $config
37
     */
38 1
    public function __construct(Config $config)
39
    {
40 1
        $this->config = $config;
41 1
    }
42
43
    /**
44
     * Create instance by static method
45
     *
46
     * @param Config $config
47
     *
48
     * @return static
49
     */
50 1
    public static function createByConfig(Config $config)
51
    {
52 1
        return new static($config);
53
    }
54
55
    /**
56
     * Set default configuration
57
     *
58
     * @throws \InvalidArgumentException
59
     * @throws \Symfony\Component\Serializer\Exception\RuntimeException
60
     * @throws \Symfony\Component\Serializer\Exception\MappingException
61
     */
62 1
    public function configureDefaults()
63
    {
64 1
        $this->encoders = array(new JsonEncoder());
65
66 1
        $loaders = [];
67 1
        $directoryLoader = new DirectoryLoader();
68
69 1
        $metadataDirs = $this->config->get(Config::METADATA_DIRS, []);
70 1
        foreach ($metadataDirs as $metadataDir) {
71 1
            $newLoaders = $directoryLoader->load($metadataDir['dir']);
72 1
            if (null === $newLoaders || 0 === count($newLoaders)) {
73 1
                continue;
74
            }
75
            array_push($loaders, ...$newLoaders);
0 ignored issues
show
$newLoaders is expanded, but the parameter $var of array_push() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            array_push($loaders, /** @scrutinizer ignore-type */ ...$newLoaders);
Loading history...
76
        }
77
78 1
        $classMetadataFactory = new ClassMetadataFactory(new LoaderChain($loaders));
79
80 1
        $normalizer = new ObjectNormalizer($classMetadataFactory, new CamelCaseToSnakeCaseNameConverter());
81 1
        $this->normalizers = array($normalizer);
82
83 1
        return $this;
84
    }
85
86
    /**
87
     * Builds the Symfony Serializer object.
88
     *
89
     * @return SerializerInterface
90
     */
91 1
    public function build(): SerializerInterface
92
    {
93 1
        $serializer = new Serializer($this->normalizers, $this->encoders);
94
95 1
        return new SymfonySerializerAdapter($serializer);
96
    }
97
98
    /**
99
     * @return array|EncoderInterface[]
100
     */
101
    public function getEncoders(): array
102
    {
103
        return $this->encoders;
104
    }
105
106
    /**
107
     * @param array $encoders
108
     */
109
    public function setEncoders(array $encoders): void
110
    {
111
        $this->encoders = $encoders;
112
    }
113
114
    /**
115
     * @return array|NormalizableInterface[]
116
     */
117
    public function getNormalizers(): array
118
    {
119
        return $this->normalizers;
120
    }
121
122
    /**
123
     * @param array $normalizers
124
     */
125
    public function setNormalizers(array $normalizers): void
126
    {
127
        $this->normalizers = $normalizers;
128
    }
129
130
    /**
131
     * Add directory for metadata config
132
     *
133
     * @param $dir
134
     *
135
     * @return $this
136
     * @throws \InvalidArgumentException
137
     */
138
    public function addMetadataDir($dir): self
139
    {
140
        if (!is_dir($dir)) {
141
            throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
142
        }
143
144
        return $this;
145
    }
146
}
147