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

CourseMapping::getLinkMappings()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 20
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\Mapping\FieldMapping;
8
use Chubbyphp\Serialization\Mapping\FieldMappingInterface;
9
use Chubbyphp\Serialization\Mapping\LinkMapping;
10
use Chubbyphp\Serialization\Mapping\LinkMappingInterface;
11
use Chubbyphp\Serialization\Mapping\ObjectMappingInterface;
12
use Chubbyphp\Serialization\Serializer\Link\CallbackLinkSerializer;
13
use Psr\Http\Message\ServerRequestInterface as Request;
14
use Chubbyphp\ApiSkeleton\Model\Course;
15
16
final class CourseMapping implements ObjectMappingInterface
17
{
18
    /**
19
     * @var LinkGenerator
20
     */
21
    private $linkGenerator;
22
23
    /**
24
     * @param LinkGenerator $linkGenerator
25
     */
26
    public function __construct(LinkGenerator $linkGenerator)
27
    {
28
        $this->linkGenerator = $linkGenerator;
29
    }
30
31
    /**
32
     * @return string
33
     */
34
    public function getClass(): string
35
    {
36
        return Course::class;
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getType(): string
43
    {
44
        return 'course';
45
    }
46
47
    /**
48
     * @return FieldMappingInterface[]
49
     */
50
    public function getFieldMappings(): array
51
    {
52
        return [
53
            new FieldMapping('id'),
54
            new FieldMapping('name'),
55
            new FieldMapping('level'),
56
            new FieldMapping('progress'),
57
            new FieldMapping('active'),
58
        ];
59
    }
60
61
    /**
62
     * @return FieldMappingInterface[]
63
     */
64
    public function getEmbeddedFieldMappings(): array
65
    {
66
        return [];
67
    }
68
69
    /**
70
     * @return LinkMappingInterface[]
71
     */
72
    public function getLinkMappings(): array
73
    {
74
        return [
75
            new LinkMapping('read', new CallbackLinkSerializer(
76
                function (Request $request, Course $course) {
77
                    return $this->linkGenerator->generateLink('course_read', ['id' => $course->getId()]);
78
                }
79
            )),
80
            new LinkMapping('update', new CallbackLinkSerializer(
81
                function (Request $request, Course $course) {
82
                    return $this->linkGenerator->generateLink('course_update', ['id' => $course->getId()]);
83
                }
84
            )),
85
            new LinkMapping('delete', new CallbackLinkSerializer(
86
                function (Request $request, Course $course) {
87
                    return $this->linkGenerator->generateLink('course_delete', ['id' => $course->getId()]);
88
                }
89
            )),
90
        ];
91
    }
92
}
93