Completed
Push — master ( 62cb7b...4b7fdd )
by Nikolay
02:34
created

InputMediaNormalizer::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TgBotApi\BotApiBase\Normalizer;
6
7
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
8
use Symfony\Component\Serializer\Serializer;
9
use TgBotApi\BotApiBase\Type\InputMedia\InputMediaPhotoType;
10
use TgBotApi\BotApiBase\Type\InputMedia\InputMediaVideoType;
11
12
class InputMediaNormalizer implements NormalizerInterface
13
{
14
    private $files;
15
    private $objectNormalizer;
16
17 39
    public function __construct(NormalizerInterface $objectNormalizer, &$files)
18
    {
19 39
        $this->objectNormalizer = $objectNormalizer;
20 39
        $this->files = &$files;
21 39
    }
22
23 1
    public function normalize($topic, $format = null, array $context = [])
24
    {
25 1
        if ($topic->media instanceof \SplFileInfo) {
26 1
            $uniqid = \uniqid();
27 1
            $this->files[$uniqid] = $topic->media;
28 1
            $topic->media = 'attach://' . $uniqid;
29
        }
30
31 1
        if (\property_exists($topic, 'thumb') && $topic->thumb instanceof \SplFileInfo) {
32 1
            $uniqid = \uniqid();
33 1
            $this->files[$uniqid] = $topic->thumb;
34 1
            $topic->thumb = 'attach://' . $uniqid;
35
        }
36
37 1
        $serializer = new Serializer([$this->objectNormalizer]);
38
39 1
        return $serializer->normalize($topic, null, ['skip_null_values' => true]);
40
    }
41
42 1
    public function supportsNormalization($data, $format = null)
43
    {
44 1
        return $data instanceof InputMediaPhotoType ||
45 1
            $data instanceof InputMediaVideoType;
46
    }
47
}
48