Passed
Pull Request — master (#87)
by Dmitriy
14:25 queued 11:27
created

Request   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 94.74%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatus() 0 3 1
A getTitle() 0 3 1
A getId() 0 3 1
A getText() 0 3 1
A getRules() 0 19 2
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
                new Required(),
50 1
                new HasLength(min: 5, max: 255),
51
            ],
52
            'body.text' => [
53 1
                new Required(),
54 1
                new HasLength(min: 5, max: 1000),
55
            ],
56
            'body.status' => [
57 1
                new Required(),
58 1
                static function ($value): Result {
59 1
                    $result = new Result();
60 1
                    if (!PostStatus::isValid($value)) {
61
                        $result->addError('Incorrect status');
62
                    }
63 1
                    return $result;
64
                },
65
            ],
66
        ];
67
    }
68
}
69