InputMediaNormalizer::normalize()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 17
ccs 11
cts 11
cp 1
rs 9.9332
c 0
b 0
f 0
cc 4
nc 4
nop 3
crap 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TgBotApi\BotApiBase\Normalizer;
6
7
use Symfony\Component\Serializer\Exception\ExceptionInterface;
8
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
9
use Symfony\Component\Serializer\Serializer;
10
use TgBotApi\BotApiBase\Type\InputFileType;
11
use TgBotApi\BotApiBase\Type\InputMedia\InputMediaType;
12
13
/**
14
 * Class InputMediaNormalizer.
15
 */
16
class InputMediaNormalizer implements NormalizerInterface
17
{
18
    /**
19
     * @var
20
     */
21
    private $files;
22
    /**
23
     * @var NormalizerInterface
24
     */
25
    private $objectNormalizer;
26
27
    /**
28
     * InputMediaNormalizer constructor.
29
     *
30
     * @param $files
31
     */
32 120
    public function __construct(NormalizerInterface $objectNormalizer, &$files)
33
    {
34 120
        $this->objectNormalizer = $objectNormalizer;
35 120
        $this->files = &$files;
36 120
    }
37
38
    /**
39
     * @param InputMediaType $topic
40
     * @param null           $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
41
     *
42
     * @throws ExceptionInterface
43
     *
44
     * @return array|bool|float|int|mixed|string
45
     */
46 5
    public function normalize($topic, $format = null, array $context = [])
47
    {
48 5
        if ($topic->media instanceof InputFileType) {
49 5
            $uniqid = \uniqid('', true);
50 5
            $this->files[$uniqid] = $topic->media;
51 5
            $topic->media = 'attach://' . $uniqid;
52
        }
53
54 5
        if (\property_exists($topic, 'thumb') && $topic->thumb instanceof InputFileType) {
55 5
            $uniqid = \uniqid('', true);
56 5
            $this->files[$uniqid] = $topic->thumb;
57 5
            $topic->thumb = 'attach://' . $uniqid;
58
        }
59
60 5
        $serializer = new Serializer([$this->objectNormalizer]);
61
62 5
        return $serializer->normalize($topic, null, ['skip_null_values' => true]);
63
    }
64
65
    /**
66
     * @param mixed $data
67
     * @param null  $format
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $format is correct as it would always require null to be passed?
Loading history...
68
     */
69 5
    public function supportsNormalization($data, $format = null): bool
70
    {
71 5
        return $data instanceof InputMediaType;
72
    }
73
}
74