Completed
Pull Request — master (#1)
by Nikolay
02:25
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;
0 ignored issues
show
Bug introduced by
The type Greenplugin\TelegramBot\Type\InputMediaPhotoType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Greenplugin\TelegramBot\Type\InputMediaVideoType;
0 ignored issues
show
Bug introduced by
The type Greenplugin\TelegramBot\Type\InputMediaVideoType was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 5
    public function __construct(NormalizerInterface $objectNormalizer, &$files)
18
    {
19 5
        $this->objectNormalizer = $objectNormalizer;
20 5
        $this->files = &$files;
21 5
    }
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