Completed
Pull Request — master (#21)
by Valentyn
01:42
created

TranslatedEntitySerializer::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 2.032
1
<?php
2
3
namespace App\Translation;
4
5
use JMS\Serializer\Serializer;
6
use JMS\Serializer\SerializerInterface;
7
8
class TranslatedEntitySerializer
9
{
10
    /**
11
     * @var Serializer
12
     */
13
    protected $serializer;
14
15 9
    public function __construct(SerializerInterface $serializer)
16
    {
17 9
        if ($serializer instanceof Serializer === false) {
18
            throw new \InvalidArgumentException(sprintf('Serializer should be instance of %s instead %s given', Serializer::class, get_class($serializer)));
19
        }
20
21 9
        $this->serializer = $serializer;
22 9
    }
23
24 5
    public function serialize(TranslatableInterface $entity, string $locale): array
25
    {
26 5
        $translation = $this->serializer->toArray($entity->getTranslation($locale));
27 5
        $entity = $this->serializer->toArray($entity);
28
29 5
        return array_merge($translation, $entity);
30
    }
31
}