EditPostRequest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 6
eloc 19
c 4
b 0
f 0
dl 0
loc 41
ccs 18
cts 19
cp 0.9474
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getText() 0 3 1
A getId() 0 3 1
A getStatus() 0 3 1
A getTitle() 0 3 1
A getRules() 0 19 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Blog;
6
7
use Yiisoft\RequestModel\RequestModel;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule\HasLength;
10
use Yiisoft\Validator\Rule\Required;
11
use Yiisoft\Validator\RulesProviderInterface;
12
use OpenApi\Annotations as OA;
13
14
/**
15
 * @OA\Schema(
16
 *      schema="EditPostRequest",
17
 *      @OA\Property(example="Title post", property="title", format="string"),
18
 *      @OA\Property(example="Text post", property="text", format="string"),
19
 *      @OA\Property(example=1, property="status", format="int"),
20
 * )
21
 */
22
final class EditPostRequest extends RequestModel implements RulesProviderInterface
23
{
24 1
    public function getId(): int
25
    {
26 1
        return (int)$this->getAttributeValue('router.id');
27
    }
28
29 2
    public function getTitle(): string
30
    {
31 2
        return (string)$this->getAttributeValue('body.title');
32
    }
33
34 2
    public function getText(): string
35
    {
36 2
        return (string)$this->getAttributeValue('body.text');
37
    }
38
39 2
    public function getStatus(): PostStatus
40
    {
41 2
        return PostStatus::from($this->getAttributeValue('body.status'));
42
    }
43
44 3
    public function getRules(): array
45
    {
46
        return [
47
            'body.title' => [
48 3
                new Required(),
49 3
                new HasLength(min: 5, max: 255),
50
            ],
51
            'body.text' => [
52 3
                new Required(),
53 3
                new HasLength(min: 5, max: 1000),
54
            ],
55
            'body.status' => [
56 3
                new Required(),
57 3
                static function ($value): Result {
58 3
                    $result = new Result();
59 3
                    if (!PostStatus::isValid($value)) {
60
                        $result->addError('Incorrect status');
61
                    }
62 3
                    return $result;
63
                },
64
            ],
65
        ];
66
    }
67
}
68