Passed
Pull Request — master (#87)
by Dmitriy
02:45
created

Request::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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