Book::getOneHandler()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 3
rs 10
c 2
b 0
f 0
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\AuthorExists;
11
use Psr\Http\Message\ResponseInterface;
12
use LibraryCatalog\Controller\V1\Transformer\BookWithAuthor as BookTransformer;
13
use LibraryCatalog\Entity\Book as BookEntity;
14
use Rakit\Validation\RuleQuashException;
15
use Rakit\Validation\Validator;
16
17
class Book 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 3
    public function getOneHandler(string $uri, string $id): ResponseInterface
28
    {
29
        // Check ACL first.
30 3
        if (!$this->isAllowed(Acl::BOOK, Acl::READ)) {
31 1
            return $this->forbiddenError($uri);
32
        }
33
34 2
        $book = $this->container->get('Catalogue')->fetchBook($id, true);
35
36 2
        if ($book) {
37 1
            $response = $this->createResponse((new BookTransformer())->transform($book));
38
        } else {
39 1
            $response = $this->notFoundError($uri, 'Book not found');
40
        }
41 2
        return $response;
42
    }
43
44
    /**
45
     * @param string $uri
46
     * @param array $params
47
     * @return ResponseInterface
48
     * @throws \DI\DependencyException
49
     * @throws \DI\NotFoundException
50
     * @throws TransformerException
51
     * @throws RuleQuashException
52
     */
53 3
    public function postOneHandler(string $uri, array $params): ResponseInterface
54
    {
55
        // Check ACL first.
56 3
        if (!$this->isAllowed(Acl::BOOK, Acl::ADD)) {
57 1
            return $this->forbiddenError($uri);
58
        }
59
60
        /** @var Catalogue $catalogue */
61 2
        $catalogue = $this->container->get('Catalogue');
62
63
        /**
64
         * In a simple project I do not want to add Request-Hydrators and do validation directly in controller.
65
         */
66
67 2
        $validator = new Validator();
68 2
        $validator->addValidator('authorExists', new AuthorExists($catalogue->getAuthorRepository()));
69 2
        $validation = $validator->validate($params, [
70 2
            'title'                 => 'required|min:3|max:4096',
71
            'summary'               => 'max:65534',
72
            'authorId'              => 'required|authorExists',
73
        ]);
74
75 2
        if ($validation->fails()) {
76 1
            $response = $this->validationError($uri, $validation->errors()->firstOfAll());
77
        } else {
78 1
            $book = (new BookEntity())->fill($params);
79 1
            $catalogue->createBook($book);
80 1
            $response = $this->createResponse((new BookTransformer())->transform($book));
81
        }
82
83 2
        return $response;
84
    }
85
}
86