Passed
Push — draft ( 541bbe...117fb3 )
by Maxim
02:29
created

InputMediaNormalizer::supportsNormalization()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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