Test Failed
Pull Request — master (#87)
by Dmitriy
02:46
created

Request::getText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Infrastructure\IO\Http\Blog\PostCreate\Request;
6
7
use App\Application\Blog\Entity\Post\PostStatus;
8
use App\Application\User\Entity\User;
9
use OpenApi\Annotations as OA;
10
use Yiisoft\Auth\Middleware\Authentication;
11
use Yiisoft\RequestModel\RequestModel;
12
use Yiisoft\Validator\Result;
13
use Yiisoft\Validator\Rule\HasLength;
14
use Yiisoft\Validator\Rule\Required;
15
use Yiisoft\Validator\RulesProviderInterface;
16
17
/**
18
 * @OA\Schema(
19
 *      schema="BlogPostCreate",
20
 *      @OA\Property(example="Title post", property="title", format="string"),
21
 *      @OA\Property(example="Text post", property="text", format="string"),
22
 *      @OA\Property(example=1, property="status", format="int"),
23
 * )
24
 */
25
final class Request extends RequestModel implements RulesProviderInterface
26
{
27
    public function getId(): int
28
    {
29
        return (int) $this->getAttributeValue('router.id');
30
    }
31
32
    public function getTitle(): string
33
    {
34
        return (string) $this->getAttributeValue('body.title');
35
    }
36
37
    public function getText(): string
38
    {
39
        return (string) $this->getAttributeValue('body.text');
40
    }
41
42
    public function getStatus(): PostStatus
43
    {
44
        return PostStatus::from($this->getAttributeValue('body.status'));
45
    }
46
47
    public function getUser(): User
48
    {
49
        /**
50
         * @var User $identity
51
         */
52
        return $this->getAttributeValue('attributes.' . Authentication::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getAttribu...\Authentication::class) could return the type null which is incompatible with the type-hinted return App\Application\User\Entity\User. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
55
    public function getRules(): array
56
    {
57
        return [
58
            'body.title' => [
59
                new Required(),
60
                new HasLength(min: 5, max: 255),
61
            ],
62
            'body.text' => [
63
                new Required(),
64
                new HasLength(min: 5, max: 1000),
65
            ],
66
            'body.status' => [
67
                new Required(),
68
                static function ($value): Result {
69
                    $result = new Result();
70
                    if (!PostStatus::isValid($value)) {
71
                        $result->addError('Incorrect status');
72
                    }
73
                    return $result;
74
                },
75
            ],
76
        ];
77
    }
78
}
79