Completed
Pull Request — master (#5)
by Dominik
06:06
created

ReferenceOneFieldDenormalizer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 65
Duplicated Lines 10.77 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 7
loc 65
ccs 17
cts 18
cp 0.9444
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C denormalizeField() 7 33 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Denormalizer\Relation;
6
7
use Chubbyphp\Deserialization\Accessor\AccessorInterface;
8
use Chubbyphp\Deserialization\Denormalizer\DenormalizerContextInterface;
9
use Chubbyphp\Deserialization\Denormalizer\DenormalizerInterface;
10
use Chubbyphp\Deserialization\Denormalizer\FieldDenormalizerInterface;
11
use Chubbyphp\Deserialization\DeserializerRuntimeException;
12
use Doctrine\Common\Persistence\Proxy;
13
14
final class ReferenceOneFieldDenormalizer implements FieldDenormalizerInterface
15
{
16
    /**
17
     * @var callable
18
     */
19
    private $repository;
20
21
    /**
22
     * @var AccessorInterface
23
     */
24
    private $accessor;
25
26
    /**
27
     * @param callable          $repository
28
     * @param AccessorInterface $accessor
29
     */
30 3
    public function __construct(callable $repository, AccessorInterface $accessor)
31
    {
32 3
        $this->repository = $repository;
33 3
        $this->accessor = $accessor;
34 3
    }
35
36
    /**
37
     * @param string                       $path
38
     * @param object                       $object
39
     * @param mixed                        $value
40
     * @param DenormalizerContextInterface $context
41
     * @param DenormalizerInterface|null   $denormalizer
42
     *
43
     * @throws DeserializerRuntimeException
44
     */
45 3
    public function denormalizeField(
46
        string $path,
47
        $object,
48
        $value,
49
        DenormalizerContextInterface $context,
50
        DenormalizerInterface $denormalizer = null
51
    ) {
52 3
        if (null === $value) {
53 1
            $this->accessor->setValue($object, $value);
54
55 1
            return;
56
        }
57
58 2
        if (is_string($value)) {
59 1
            $repository = $this->repository;
60
61 1
            $referencedObject = $repository($value);
62
63 1 View Code Duplication
            if (null !== $referencedObject) {
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...
64 1
                if (interface_exists('Doctrine\Common\Persistence\Proxy')
65 1
                    && $referencedObject instanceof Proxy && !$referencedObject->__isInitialized()
66
                ) {
67
                    $referencedObject->__load();
68
                }
69
            }
70
71 1
            $this->accessor->setValue($object, $referencedObject);
72
73 1
            return;
74
        }
75
76 1
        throw DeserializerRuntimeException::createInvalidDataType($path, gettype($value), 'string');
77
    }
78
}
79