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

Request   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 95.65%

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 45
ccs 22
cts 23
cp 0.9565
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRules() 0 23 2
A getStatus() 0 3 1
A getTitle() 0 3 1
A getId() 0 3 1
A getText() 0 3 1
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