RoadmapDtoDenormalizer::denormalize()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 28
rs 9.7
cc 4
nc 4
nop 4
1
<?php
2
3
/*
4
 * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>.
5
 *
6
 * (C) 2019 Pavel Petrov <[email protected]>.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0
12
 */
13
14
declare(strict_types=1);
15
16
namespace Veslo\AnthillBundle\Vacancy\Normalizer;
17
18
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
19
use UnexpectedValueException;
20
use Veslo\AnthillBundle\Dto\Vacancy\ConfigurableRoadmapDto;
21
use Veslo\AnthillBundle\Dto\Vacancy\Roadmap\ConfigurationDto;
22
use Veslo\AnthillBundle\Dto\Vacancy\Roadmap\StrategyDto;
23
use Veslo\AnthillBundle\Dto\Vacancy\RoadmapDto;
24
25
/**
26
 * Converts an array of scalars to valid RoadmapDto instance or to any suitable child instance
27
 */
28
class RoadmapDtoDenormalizer implements DenormalizerInterface
29
{
30
    /**
31
     * Converts an array of scalars to valid roadmap StrategyDto instance
32
     *
33
     * @var DenormalizerInterface
34
     */
35
    private $strategyDenormalizer;
36
37
    /**
38
     * Converts an array of scalars to valid roadmap ConfigurationDto instance
39
     *
40
     * @var DenormalizerInterface
41
     */
42
    private $configurationDenormalizer;
43
44
    /**
45
     * RoadmapDtoDenormalizer constructor.
46
     *
47
     * @param DenormalizerInterface $strategyDenormalizer      Converts an array of scalars to StrategyDto instance
48
     * @param DenormalizerInterface $configurationDenormalizer Converts an array of scalars to ConfigurationDto instance
49
     */
50
    public function __construct(
51
        DenormalizerInterface $strategyDenormalizer,
52
        DenormalizerInterface $configurationDenormalizer
53
    ) {
54
        $this->strategyDenormalizer      = $strategyDenormalizer;
55
        $this->configurationDenormalizer = $configurationDenormalizer;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function denormalize($data, $class, $format = null, array $context = [])
62
    {
63
        if (array_key_exists(ConfigurableRoadmapDto::PROPERTY_STRATEGY, $data)
64
            && array_key_exists(ConfigurableRoadmapDto::PROPERTY_CONFIGURATION, $data)
65
        ) {
66
            $roadmapDto = new ConfigurableRoadmapDto();
67
68
            $strategyData = $data[ConfigurableRoadmapDto::PROPERTY_STRATEGY];
69
            $strategyDto  = $this->strategyDenormalizer->denormalize($strategyData, StrategyDto::class);
70
            $roadmapDto->setStrategy($strategyDto);
0 ignored issues
show
Bug introduced by
It seems like $strategyDto can also be of type array; however, parameter $strategy of Veslo\AnthillBundle\Dto\...admapDto::setStrategy() does only seem to accept Veslo\AnthillBundle\Dto\...ncy\Roadmap\StrategyDto, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $roadmapDto->setStrategy(/** @scrutinizer ignore-type */ $strategyDto);
Loading history...
71
72
            $configurationData = $data[ConfigurableRoadmapDto::PROPERTY_CONFIGURATION];
73
            $configurationDto  = $this->configurationDenormalizer->denormalize(
74
                $configurationData,
75
                ConfigurationDto::class
76
            );
77
            $roadmapDto->setConfiguration($configurationDto);
0 ignored issues
show
Bug introduced by
It seems like $configurationDto can also be of type array; however, parameter $configuration of Veslo\AnthillBundle\Dto\...Dto::setConfiguration() does only seem to accept Veslo\AnthillBundle\Dto\...oadmap\ConfigurationDto, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
            $roadmapDto->setConfiguration(/** @scrutinizer ignore-type */ $configurationDto);
Loading history...
78
        } else {
79
            $roadmapDto = new RoadmapDto();
80
        }
81
82
        if (!array_key_exists(RoadmapDto::PROPERTY_NAME, $data)) {
83
            throw new UnexpectedValueException('Value for roadmap name property is not exists for hydration.');
84
        }
85
86
        $roadmapDto->setName($data[RoadmapDto::PROPERTY_NAME]);
87
88
        return $roadmapDto;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function supportsDenormalization($data, $type, $format = null)
95
    {
96
        return RoadmapDto::class === $type;
97
    }
98
}
99