Test Failed
Push — master ( 98997f...e65f7f )
by Dominik
02:18
created

app/Serialization/CourseSearchMapping.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
        ];
68
    }
69
70
    /**
71
     * @return FieldMappingInterface[]
72
     */
73
    public function getEmbeddedFieldMappings(): array
74
    {
75
        return [
76
            new FieldMapping('count'),
77
            new FieldMapping('pages'),
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) {
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) {
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
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...
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...
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