Test Failed
Push — master ( f562c0...ed7d3a )
by Dominik
02:11
created

CourseSearchMapping::getEmbeddedFieldMappings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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