Test Failed
Pull Request — master (#20)
by Alexander
04:07 queued 01:32
created

EditPostRequest::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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