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

InputMediaNormalizer   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 28.57%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 28
ccs 4
cts 14
cp 0.2857
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A normalize() 0 11 2
A supportsNormalization() 0 4 2
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