Completed
Push — master ( 6b5ec1...af67b7 )
by San
04:08 queued 02:35
created

supportsDenormalization()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
/*
3
 * This file is part of Pomm's SymfonyBidge package.
4
 *
5
 * (c) 2017 Grégoire HUBERT <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace PommProject\SymfonyBridge\Serializer\Normalizer;
11
12
use PommProject\Foundation\Pomm;
13
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
14
15
/**
16
 * Denormalizer for flexible entities.
17
 *
18
 * @package PommSymfonyBridge
19
 * @copyright 2017 Grégoire HUBERT
20
 * @author Nicolas Joseph
21
 * @license X11 {@link http://opensource.org/licenses/mit-license.php}
22
 */
23
class FlexibleEntityDenormalizer implements DenormalizerInterface
24
{
25
    private $pomm;
26
27
    public function __construct(Pomm $pomm)
28
    {
29
        $this->pomm = $pomm;
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function denormalize($data, $class, $format = null, array $context = array())
36
    {
37 View Code Duplication
        if (isset($context['session:name'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
38
            $session = $this->pomm->getSession($context['session:name']);
39
        } else {
40
            $session = $this->pomm->getDefaultSession();
41
        }
42
43
        if (isset($context['model:name'])) {
44
            $model_name = $context['model:name'];
45
        } else {
46
            $model_name = "${class}Model";
47
        }
48
49
        $model = $session->getModel($model_name);
50
        return $model->createEntity($data);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function supportsDenormalization($data, $type, $format = null)
57
    {
58
        $reflection = new \ReflectionClass($type);
59
        $interfaces = $reflection->getInterfaces();
60
61
        // @TODO Use FlexibleEntityInterface::class with php >= 5.5
62
        return isset($interfaces['PommProject\ModelManager\Model\FlexibleEntity\FlexibleEntityInterface']);
63
    }
64
}
65