Completed
Pull Request — master (#12)
by Dominik
07:37 queued 14s
created

NormalizationFieldMappingBuilder::setPermission()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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
     * @var object|string|null
36
     */
37
    private $permission;
38
39
    /**
40
     * @param string $name
41
     */
42 9
    private function __construct(string $name)
43
    {
44 9
        $this->name = $name;
45 9
    }
46
47
    /**
48
     * @param string $name
49
     *
50
     * @return NormalizationFieldMappingBuilderInterface
51
     */
52 2
    public static function create(string $name): NormalizationFieldMappingBuilderInterface
53
    {
54 2
        return new self($name);
55
    }
56
57
    /**
58
     * @param string   $name
59
     * @param callable $callback
60
     *
61
     * @return NormalizationFieldMappingBuilderInterface
62
     */
63 1
    public static function createCallback(string $name, callable $callback): NormalizationFieldMappingBuilderInterface
64
    {
65 1
        $self = new self($name);
66 1
        $self->fieldNormalizer = new CallbackFieldNormalizer($callback);
67
68 1
        return $self;
69
    }
70
71
    /**
72
     * @param string $name
73
     * @param string $format
74
     *
75
     * @return NormalizationFieldMappingBuilderInterface
76
     */
77 2
    public static function createDateTime(string $name, string $format = 'c'): NormalizationFieldMappingBuilderInterface
78
    {
79 2
        $self = new self($name);
80 2
        $self->fieldNormalizer = new DateTimeFieldNormalizer(new PropertyAccessor($name), $format);
81
82 2
        return $self;
83
    }
84
85
    /**
86
     * @param string $name
87
     *
88
     * @return NormalizationFieldMappingBuilderInterface
89
     */
90 1
    public static function createEmbedMany(string $name): NormalizationFieldMappingBuilderInterface
91
    {
92 1
        $self = new self($name);
93 1
        $self->fieldNormalizer = new EmbedManyFieldNormalizer(new PropertyAccessor($name));
94
95 1
        return $self;
96
    }
97
98
    /**
99
     * @param string $name
100
     *
101
     * @return NormalizationFieldMappingBuilderInterface
102
     */
103 1
    public static function createEmbedOne(string $name): NormalizationFieldMappingBuilderInterface
104
    {
105 1
        $self = new self($name);
106 1
        $self->fieldNormalizer = new EmbedOneFieldNormalizer(new PropertyAccessor($name));
107
108 1
        return $self;
109
    }
110
111
    /**
112
     * @param string $name
113
     * @param string $idName
114
     *
115
     * @return NormalizationFieldMappingBuilderInterface
116
     */
117 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...
118
        string $name,
119
        string $idName = 'id'
120
    ): NormalizationFieldMappingBuilderInterface {
121 1
        $self = new self($name);
122 1
        $self->fieldNormalizer = new ReferenceManyFieldNormalizer(
123 1
            new PropertyAccessor($idName),
124 1
            new PropertyAccessor($name)
125
        );
126
127 1
        return $self;
128
    }
129
130
    /**
131
     * @param string $name
132
     * @param string $idName
133
     *
134
     * @return NormalizationFieldMappingBuilderInterface
135
     */
136 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...
137
        string $name,
138
        string $idName = 'id'
139
    ): NormalizationFieldMappingBuilderInterface {
140 1
        $self = new self($name);
141 1
        $self->fieldNormalizer = new ReferenceOneFieldNormalizer(
142 1
            new PropertyAccessor($idName),
143 1
            new PropertyAccessor($name)
144
        );
145
146 1
        return $self;
147
    }
148
149
    /**
150
     * @param array $groups
151
     *
152
     * @return NormalizationFieldMappingBuilderInterface
153
     */
154 1
    public function setGroups(array $groups): NormalizationFieldMappingBuilderInterface
155
    {
156 1
        $this->groups = $groups;
157
158 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...
159
    }
160
161
    /**
162
     * @param FieldNormalizerInterface $fieldNormalizer
163
     *
164
     * @return NormalizationFieldMappingBuilderInterface
165
     */
166 1
    public function setFieldNormalizer(
167
        FieldNormalizerInterface $fieldNormalizer
168
    ): NormalizationFieldMappingBuilderInterface {
169 1
        $this->fieldNormalizer = $fieldNormalizer;
170
171 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...
172
    }
173
174
    /**
175
     * @param object|string|null $permission
176
     *
177
     * @return NormalizationFieldMappingBuilderInterface
178
     */
179
    public function setPermission($permission): NormalizationFieldMappingBuilderInterface
180
    {
181
        $this->permission = $permission;
182
183
        return $this;
184
    }
185
186
    /**
187
     * @return NormalizationFieldMappingInterface
188
     */
189 9
    public function getMapping(): NormalizationFieldMappingInterface
190
    {
191 9
        return new NormalizationFieldMapping(
192 9
            $this->name,
193 9
            $this->groups,
194 9
            $this->fieldNormalizer ?? new FieldNormalizer(new PropertyAccessor($this->name)),
195 9
            $this->permission
196
        );
197
    }
198
}
199