CourseSearchMapping   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 20.18 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 22
loc 109
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getClass() 0 4 1
A getType() 0 4 1
A getFieldMappings() 0 17 1
A getEmbeddedFieldMappings() 0 6 1
A getLinkMappings() 22 43 3

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\ApiSkeleton\Serialization;
6
7
use Chubbyphp\Serialization\Accessor\PropertyAccessor;
8
use Chubbyphp\Serialization\Link\LinkGeneratorInterface;
9
use Chubbyphp\Serialization\Link\NullLink;
10
use Chubbyphp\Serialization\Mapping\FieldMapping;
11
use Chubbyphp\Serialization\Mapping\FieldMappingInterface;
12
use Chubbyphp\Serialization\Mapping\LinkMapping;
13
use Chubbyphp\Serialization\Mapping\LinkMappingInterface;
14
use Chubbyphp\Serialization\Mapping\ObjectMappingInterface;
15
use Chubbyphp\Serialization\Serializer\Field\CollectionFieldSerializer;
16
use Chubbyphp\Serialization\Serializer\Field\ValueFieldSerializer;
17
use Chubbyphp\Serialization\Serializer\Link\CallbackLinkSerializer;
18
use Psr\Http\Message\ServerRequestInterface as Request;
19
use Chubbyphp\ApiSkeleton\Search\CourseSearch;
20
21
final class CourseSearchMapping implements ObjectMappingInterface
22
{
23
    /**
24
     * @var LinkGeneratorInterface
25
     */
26
    private $linkGenerator;
27
28
    /**
29
     * @param LinkGeneratorInterface $linkGenerator
30
     */
31 6
    public function __construct(LinkGeneratorInterface $linkGenerator)
32
    {
33 6
        $this->linkGenerator = $linkGenerator;
34 6
    }
35
36
    /**
37
     * @return string
38
     */
39 1
    public function getClass(): string
40
    {
41 1
        return CourseSearch::class;
42
    }
43
44
    /**
45
     * @return string
46
     */
47 1
    public function getType(): string
48
    {
49 1
        return 'course-search';
50
    }
51
52
    /**
53
     * @return FieldMappingInterface[]
54
     */
55 1
    public function getFieldMappings(): array
56
    {
57
        return [
58 1
            new FieldMapping('page', new ValueFieldSerializer(
59 1
                new PropertyAccessor('page'),
60 1
                ValueFieldSerializer::CAST_INT)
61
            ),
62 1
            new FieldMapping('perPage', new ValueFieldSerializer(
63 1
                new PropertyAccessor('perPage'),
64 1
                ValueFieldSerializer::CAST_INT)
65
            ),
66 1
            new FieldMapping('sort'),
67 1
            new FieldMapping('order'),
68 1
            new FieldMapping('count'),
69 1
            new FieldMapping('pages'),
70
        ];
71
    }
72
73
    /**
74
     * @return FieldMappingInterface[]
75
     */
76 1
    public function getEmbeddedFieldMappings(): array
77
    {
78
        return [
79 1
            new FieldMapping('courses', new CollectionFieldSerializer(new PropertyAccessor('courses'))),
80
        ];
81
    }
82
83
    /**
84
     * @return LinkMappingInterface[]
85
     */
86 2
    public function getLinkMappings(): array
87
    {
88
        return [
89 2
            new LinkMapping('self', new CallbackLinkSerializer(
90 2
                function (Request $request, CourseSearch $courseSearch, array $fields) {
91 2
                    unset($fields['count'], $fields['pages']);
92
93 2
                    return $this->linkGenerator->generateLink($request, 'course_search', [], $fields);
94 2
                }
95
            )),
96 2
            new LinkMapping('prev', new CallbackLinkSerializer(
97 2 View Code Duplication
                function (Request $request, CourseSearch $courseSearch, array $fields) {
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...
98 2
                    if ($courseSearch->getPage() > 1) {
99 1
                        $fields['page'] -= 1;
100
101 1
                        unset($fields['count'], $fields['pages']);
102
103 1
                        return $this->linkGenerator->generateLink($request, 'course_search', [], $fields);
104
                    }
105
106 1
                    return new NullLink();
107 2
                }
108
            )),
109 2
            new LinkMapping('next', new CallbackLinkSerializer(
110 2 View Code Duplication
                function (Request $request, CourseSearch $courseSearch, array $fields) {
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...
111 2
                    if ($fields['page'] < $courseSearch->getPages()) {
112 1
                        $fields['page'] += 1;
113
114 1
                        unset($fields['count'], $fields['pages']);
115
116 1
                        return $this->linkGenerator->generateLink($request, 'course_search', [], $fields);
117
                    }
118
119 1
                    return new NullLink();
120 2
                }
121
            )),
122 2
            new LinkMapping('create', new CallbackLinkSerializer(
123 2
                function (Request $request, CourseSearch $courseSearch, array $fields) {
0 ignored issues
show
Unused Code introduced by
The parameter $courseSearch is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $fields is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
124 2
                    return $this->linkGenerator->generateLink($request, 'course_create');
125 2
                }
126
            )),
127
        ];
128
    }
129
}
130