Course   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 205
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 205
ccs 51
cts 51
cp 1
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 3

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 10 1
A fromPersistence() 0 12 1
A toPersistence() 0 11 1
A getId() 0 4 1
A getName() 0 4 1
A setName() 0 6 1
A getLevel() 0 4 1
A setLevel() 0 6 1
A getProgress() 0 4 1
A setProgress() 0 6 1
A isActive() 0 4 1
A setActive() 0 6 1
A setDocuments() 0 6 1
A getDocuments() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ApiSkeleton\Model;
6
7
use Chubbyphp\Model\Collection\ModelCollection;
8
use Chubbyphp\Model\Collection\ModelCollectionInterface;
9
use Chubbyphp\Model\ModelInterface;
10
use Ramsey\Uuid\Uuid;
11
12
final class Course implements ModelInterface
13
{
14
    /**
15
     * @var string
16
     */
17
    private $id;
18
19
    /**
20
     * @var string
21
     */
22
    private $name;
23
24
    /**
25
     * @var int
26
     */
27
    private $level;
28
29
    const LEVEL_START = 1;
30
    const LEVEL_MEDIUM = 2;
31
    const LEVEL_ADVANCED = 3;
32
    const LEVEL_EXPERT = 4;
33
34
    const LEVEL = [
35
        self::LEVEL_START,
36
        self::LEVEL_MEDIUM,
37
        self::LEVEL_ADVANCED,
38
        self::LEVEL_EXPERT,
39
    ];
40
41
    /**
42
     * @var float
43
     */
44
    private $progress;
45
46
    /**
47
     * @var bool
48
     */
49
    private $active;
50
51
    /**
52
     * @var ModelCollectionInterface
53
     */
54
    private $documents;
55
56 2
    private function __construct()
57
    {
58 2
    }
59
60
    /**
61
     * @param string|null $id
62
     *
63
     * @return self
64
     */
65 1
    public static function create(string $id = null): self
66
    {
67 1
        $self = new self();
68 1
        $self->id = $id ?? (string) Uuid::uuid4();
69 1
        $self->documents = new ModelCollection(
70 1
            Document::class, 'courseId', $self->id, ['name' => 'ASC']
71
        );
72
73 1
        return $self;
74
    }
75
76
    /**
77
     * @param array $data
78
     *
79
     * @return self|ModelInterface
80
     */
81 1
    public static function fromPersistence(array $data): ModelInterface
82
    {
83 1
        $self = new self();
84 1
        $self->id = $data['id'];
85 1
        $self->name = $data['name'];
86 1
        $self->level = (int) $data['level'];
87 1
        $self->progress = (float) $data['progress'];
88 1
        $self->active = (bool) $data['active'];
89 1
        $self->documents = $data['documents'];
90
91 1
        return $self;
92
    }
93
94
    /**
95
     * @return array
96
     */
97 1
    public function toPersistence(): array
98
    {
99
        return [
100 1
            'id' => $this->id,
101 1
            'name' => $this->name,
102 1
            'level' => $this->level,
103 1
            'progress' => $this->progress,
104 1
            'active' => $this->active,
105 1
            'documents' => $this->documents,
106
        ];
107
    }
108
109
    /**
110
     * @return string
111
     */
112 1
    public function getId(): string
113
    {
114 1
        return $this->id;
115
    }
116
117
    /**
118
     * @return string
119
     */
120 1
    public function getName(): string
121
    {
122 1
        return $this->name;
123
    }
124
125
    /**
126
     * @param string $name
127
     *
128
     * @return self
129
     */
130 1
    public function setName(string $name): self
131
    {
132 1
        $this->name = $name;
133
134 1
        return $this;
135
    }
136
137
    /**
138
     * @return int
139
     */
140 1
    public function getLevel(): int
141
    {
142 1
        return $this->level;
143
    }
144
145
    /**
146
     * @param int $level
147
     *
148
     * @return self
149
     */
150 1
    public function setLevel(int $level): self
151
    {
152 1
        $this->level = $level;
153
154 1
        return $this;
155
    }
156
157
    /**
158
     * @return float
159
     */
160 1
    public function getProgress(): float
161
    {
162 1
        return $this->progress;
163
    }
164
165
    /**
166
     * @param float $progress
167
     *
168
     * @return self
169
     */
170 1
    public function setProgress(float $progress): self
171
    {
172 1
        $this->progress = $progress;
173
174 1
        return $this;
175
    }
176
177
    /**
178
     * @return bool
179
     */
180 1
    public function isActive(): bool
181
    {
182 1
        return $this->active;
183
    }
184
185
    /**
186
     * @param bool $active
187
     *
188
     * @return self
189
     */
190 1
    public function setActive(bool $active): self
191
    {
192 1
        $this->active = $active;
193
194 1
        return $this;
195
    }
196
197
    /**
198
     * @param array $documents
199
     *
200
     * @return self
201
     */
202 1
    public function setDocuments(array $documents): self
203
    {
204 1
        $this->documents->setModels($documents);
205
206 1
        return $this;
207
    }
208
209
    /**
210
     * @return Document[]|array
211
     */
212 1
    public function getDocuments(): array
213
    {
214 1
        return array_values($this->documents->getModels());
215
    }
216
}
217