Passed
Push — main ( 77b74b...4cb13d )
by Mr.
03:38 queued 01:33
created

Author   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 35
c 1
b 0
f 0
dl 0
loc 82
ccs 28
cts 28
cp 1
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A postOneHandler() 0 39 3
A getOneHandler() 0 22 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LibraryCatalog\Controller\V1;
6
7
use LibraryCatalog\Controller\TransformerException;
8
use LibraryCatalog\Service\Acl;
9
use LibraryCatalog\Service\Catalogue;
10
use LibraryCatalog\Service\Validation\Rule\AuthorUniqueName;
11
use Psr\Http\Message\ResponseInterface;
12
use LibraryCatalog\Controller\V1\Transformer\AuthorWithBooks as AuthorTransformer;
13
use LibraryCatalog\Entity\Author as AuthorEntity;
14
use Rakit\Validation\RuleQuashException;
15
use Rakit\Validation\Validator;
16
17
class Author extends AbstractController
18
{
19
    /**
20
     * @param string $uri
21
     * @param string $id
22
     * @return ResponseInterface
23
     * @throws \DI\DependencyException
24
     * @throws \DI\NotFoundException
25
     * @throws TransformerException
26
     */
27 4
    public function getOneHandler(string $uri, string $id): ResponseInterface
28
    {
29
        // Check ACL first.
30
        if (
31 4
            !$this->getAcl()->isAllowed(
32 4
                $this->getAcl()->parseUserPayload($this->getAuthIn()->getPayload()),
0 ignored issues
show
Bug introduced by
$this->getAcl()->parseUs...AuthIn()->getPayload()) of type LibraryCatalog\ValueObject\SessionUser is incompatible with the type Laminas\Permissions\Acl\Role\RoleInterface|string expected by parameter $role of Laminas\Permissions\Acl\AclInterface::isAllowed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

32
                /** @scrutinizer ignore-type */ $this->getAcl()->parseUserPayload($this->getAuthIn()->getPayload()),
Loading history...
33 4
                Acl::AUTHOR,
34 4
                Acl::READ
35
            )
36
        ) {
37 1
            return $this->forbiddenError($uri);
38
        }
39
40 3
        $author = $this->container->get('Catalogue')->fetchAuthor($id, true);
41
42 3
        if ($author) {
43 2
            $response = $this->createResponse((new AuthorTransformer())->transform($author));
44
        } else {
45 1
            $response = $this->notFoundError($uri, 'Author not found');
46
        }
47
48 3
        return $response;
49
    }
50
51
    /**
52
     * @param string $uri
53
     * @param array $params
54
     * @return ResponseInterface
55
     * @throws \DI\DependencyException
56
     * @throws \DI\NotFoundException
57
     * @throws TransformerException
58
     * @throws RuleQuashException
59
     */
60 3
    public function postOneHandler(string $uri, array $params): ResponseInterface
61
    {
62
        // Check ACL first.
63
        if (
64 3
            !$this->getAcl()->isAllowed(
65 3
                $this->getAcl()->parseUserPayload($this->getAuthIn()->getPayload()),
0 ignored issues
show
Bug introduced by
$this->getAcl()->parseUs...AuthIn()->getPayload()) of type LibraryCatalog\ValueObject\SessionUser is incompatible with the type Laminas\Permissions\Acl\Role\RoleInterface|string expected by parameter $role of Laminas\Permissions\Acl\AclInterface::isAllowed(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
                /** @scrutinizer ignore-type */ $this->getAcl()->parseUserPayload($this->getAuthIn()->getPayload()),
Loading history...
66 3
                Acl::AUTHOR,
67 3
                Acl::ADD
68
            )
69
        ) {
70 1
            return $this->forbiddenError($uri);
71
        }
72
73
        /** @var Catalogue $catalogue */
74 2
        $catalogue = $this->container->get('Catalogue');
75
76
        /**
77
         * In a simple project I do not want to add Request-Hydrators and do validation directly in controller.
78
         */
79
80 2
        $validator = new Validator();
81 2
        $validator->addValidator('uniqueAuthor', new AuthorUniqueName($catalogue->getAuthorRepository(), $params['birthdate'] ?? ''));
82 2
        $validation = $validator->validate($params, [
83 2
            'name'                  => 'required|min:3|max:255|uniqueAuthor',
84
            'birthdate'             => 'required|date',
85
            'deathdate'             => 'date',
86
            'biography'             => 'max:65534',
87
            'summary'               => 'max:65534',
88
        ]);
89
90 2
        if ($validation->fails()) {
91 1
            $response = $this->validationError($uri, $validation->errors()->firstOfAll());
92
        } else {
93 1
            $author = (new AuthorEntity())->fill($params);
94 1
            $catalogue->createAuthor($author);
95 1
            $response = $this->createResponse((new AuthorTransformer())->transform($author));
96
        }
97
98 2
        return $response;
99
    }
100
}
101