Completed
Push — master ( 4d20f6...90a226 )
by Dominik
05:33
created

NormalizationFieldMappingBuilder::createCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Mapping;
6
7
use Chubbyphp\Serialization\Accessor\PropertyAccessor;
8
use Chubbyphp\Serialization\Normalizer\CallbackFieldNormalizer;
9
use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer;
10
use Chubbyphp\Serialization\Normalizer\FieldNormalizer;
11
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
12
use Chubbyphp\Serialization\Normalizer\Relation\EmbedManyFieldNormalizer;
13
use Chubbyphp\Serialization\Normalizer\Relation\EmbedOneFieldNormalizer;
14
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceManyFieldNormalizer;
15
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer;
16
17
final class NormalizationFieldMappingBuilder implements NormalizationFieldMappingBuilderInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var array
26
     */
27
    private $groups = [];
28
29
    /**
30
     * @var FieldNormalizerInterface|null
31
     */
32
    private $fieldNormalizer;
33
34
    /**
35
     * @param string $name
36
     */
37 9
    private function __construct(string $name)
38
    {
39 9
        $this->name = $name;
40 9
    }
41
42
    /**
43
     * @param string $name
44
     *
45
     * @return NormalizationFieldMappingBuilderInterface
46
     */
47 2
    public static function create(string $name): NormalizationFieldMappingBuilderInterface
48
    {
49 2
        return new self($name);
50
    }
51
52
    /**
53
     * @param string   $name
54
     * @param callable $callback
55
     *
56
     * @return NormalizationFieldMappingBuilderInterface
57
     */
58 1
    public static function createCallback(string $name, callable $callback): NormalizationFieldMappingBuilderInterface
59
    {
60 1
        $self = new self($name);
61 1
        $self->fieldNormalizer = new CallbackFieldNormalizer($callback);
62
63 1
        return $self;
64
    }
65
66
    /**
67
     * @param string $name
68
     * @param string $format
69
     *
70
     * @return NormalizationFieldMappingBuilderInterface
71
     */
72 2
    public static function createDateTime(string $name, string $format = 'c'): NormalizationFieldMappingBuilderInterface
73
    {
74 2
        $self = new self($name);
75 2
        $self->fieldNormalizer = new DateTimeFieldNormalizer(new PropertyAccessor($name), $format);
76
77 2
        return $self;
78
    }
79
80
    /**
81
     * @param string $name
82
     *
83
     * @return NormalizationFieldMappingBuilderInterface
84
     */
85 1
    public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface
86
    {
87 1
        $self = new self($name);
88 1
        $self->fieldNormalizer = new EmbedManyFieldNormalizer(new PropertyAccessor($name));
89
90 1
        return $self;
91
    }
92
93
    /**
94
     * @param string $name
95
     *
96
     * @return NormalizationFieldMappingBuilderInterface
97
     */
98 1
    public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface
99
    {
100 1
        $self = new self($name);
101 1
        $self->fieldNormalizer = new EmbedOneFieldNormalizer(new PropertyAccessor($name));
102
103 1
        return $self;
104
    }
105
106
    /**
107
     * @param string $name
108
     * @param string $idName
109
     *
110
     * @return NormalizationFieldMappingBuilderInterface
111
     */
112 1 View Code Duplication
    public static function createReferenceMany(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
113
        string $name,
114
        string $idName = 'id'
115
    ): NormalizationFieldMappingBuilderInterface {
116 1
        $self = new self($name);
117 1
        $self->fieldNormalizer = new ReferenceManyFieldNormalizer(
118 1
            new PropertyAccessor($idName),
119 1
            new PropertyAccessor($name)
120
        );
121
122 1
        return $self;
123
    }
124
125
    /**
126
     * @param string $name
127
     * @param string $idName
128
     *
129
     * @return NormalizationFieldMappingBuilderInterface
130
     */
131 1 View Code Duplication
    public static function createReferenceOne(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
132
        string $name,
133
        string $idName = 'id'
134
    ): NormalizationFieldMappingBuilderInterface {
135 1
        $self = new self($name);
136 1
        $self->fieldNormalizer = new ReferenceOneFieldNormalizer(
137 1
            new PropertyAccessor($idName),
138 1
            new PropertyAccessor($name)
139
        );
140
141 1
        return $self;
142
    }
143
144
    /**
145
     * @param array $groups
146
     *
147
     * @return NormalizationFieldMappingBuilderInterface
148
     */
149 1
    public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface
150
    {
151 1
        $this->groups = $groups;
152
153 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...tionFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...derInterface::setGroups of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
154
    }
155
156
    /**
157
     * @param FieldNormalizerInterface $fieldNormalizer
158
     *
159
     * @return NormalizationFieldMappingBuilderInterface
160
     */
161 1
    public function setFieldNormalizer(
162
        FieldNormalizerInterface $fieldNormalizer
163
    ): NormalizationFieldMappingBuilderInterface {
164 1
        $this->fieldNormalizer = $fieldNormalizer;
165
166 1
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Chubbyphp\Serialization\...tionFieldMappingBuilder) is incompatible with the return type declared by the interface Chubbyphp\Serialization\...ace::setFieldNormalizer of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
167
    }
168
169
    /**
170
     * @return NormalizationFieldMappingInterface
171
     */
172 9
    public function getMapping(): NormalizationFieldMappingInterface
173
    {
174 9
        return new NormalizationFieldMapping(
175 9
            $this->name,
176 9
            $this->groups,
177 9
            $this->fieldNormalizer ?? new FieldNormalizer(new PropertyAccessor($this->name))
178
        );
179
    }
180
}
181