PasteController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 13

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 1
cbo 13
dl 0
loc 111
ccs 0
cts 50
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A home() 0 7 1
A create() 0 29 2
A paste() 0 20 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nastoletni\Code\UserInterface\Web\Controller;
6
7
use Nastoletni\Code\Application\Crypter\CrypterException;
8
use Nastoletni\Code\Application\Crypter\PasteCrypter;
9
use Nastoletni\Code\Application\Form\CreatePasteFormValidator;
10
use Nastoletni\Code\Application\Service\CreatePasteService;
11
use Nastoletni\Code\Domain\Paste\Id;
12
use Nastoletni\Code\Domain\Paste\NotExistsException;
13
use Nastoletni\Code\Domain\PasteRepository;
14
use Nastoletni\Code\UserInterface\Controller\AbstractController;
15
use Psr\Http\Message\ResponseInterface as Response;
16
use Psr\Http\Message\ServerRequestInterface as Request;
17
use Slim\Exception\NotFoundException;
18
19
class PasteController extends AbstractController
20
{
21
    /**
22
     * @var PasteRepository
23
     */
24
    private $pasteRepository;
25
26
    /**
27
     * @var PasteCrypter
28
     */
29
    private $pasteCrypter;
30
31
    /**
32
     * PasteController constructor.
33
     *
34
     * @param PasteRepository $pasteRepository
35
     * @param PasteCrypter    $pasteCrypter
36
     */
37
    public function __construct(PasteRepository $pasteRepository, PasteCrypter $pasteCrypter)
38
    {
39
        $this->pasteRepository = $pasteRepository;
40
        $this->pasteCrypter = $pasteCrypter;
41
    }
42
43
    /**
44
     * home: GET /.
45
     *
46
     * @param Request  $request
47
     * @param Response $response
48
     *
49
     * @return Response
50
     */
51
    public function home(Request $request, Response $response): Response
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        return $this->twig->render($response, 'home.twig', [
54
            'errors' => $this->getFlash('errors'),
55
            'old'    => $this->getFlash('old'),
56
        ]);
57
    }
58
59
    /**
60
     * create: POST /.
61
     *
62
     * @param Request  $request
63
     * @param Response $response
64
     *
65
     * @return Response
66
     */
67
    public function create(Request $request, Response $response): Response
68
    {
69
        $data = $request->getParsedBody();
70
71
        $validator = CreatePasteFormValidator::create();
72
73
        $errors = $validator->validate($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 69 can also be of type null or object; however, Nastoletni\Code\Applicat...rmValidator::validate() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
74
75
        if (count($errors) > 0) {
76
            $this->flash('errors', $errors);
77
            $this->flash('old', $data);
78
79
            return $response
80
                ->withStatus(302)
81
                ->withHeader('Location', $this->router->relativePathFor('home'));
82
        }
83
84
        $createPasteService = new CreatePasteService($this->pasteRepository, $this->pasteCrypter);
85
        $payload = $createPasteService->handle($data);
0 ignored issues
show
Bug introduced by
It seems like $data defined by $request->getParsedBody() on line 69 can also be of type null or object; however, Nastoletni\Code\Applicat...ePasteService::handle() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
86
87
        $paste = $payload->getPaste();
88
89
        return $response
90
            ->withStatus(302)
91
            ->withHeader('Location', $this->router->relativePathFor('paste', [
92
                'id'  => $paste->getId()->getBase62Id(),
93
                'key' => $payload->getEncryptionKey(),
94
            ]));
95
    }
96
97
    /**
98
     * paste: GET /{id}/{key}.
99
     *
100
     * @param Request  $request
101
     * @param Response $response
102
     * @param string   $id
103
     * @param string   $key
104
     *
105
     * @throws NotFoundException
106
     *
107
     * @return Response
108
     */
109
    public function paste(Request $request, Response $response, string $id, string $key): Response
110
    {
111
        try {
112
            $paste = $this->pasteRepository->getById(Id::createFromBase62($id));
113
        } catch (NotExistsException $e) {
114
            throw new NotFoundException($request, $response);
115
        }
116
117
        try {
118
            $this->pasteCrypter->decrypt($paste, $key);
119
        } catch (CrypterException $e) {
120
            // CrypterException is almost always when user has modified
121
            // encryption key in the URL and that is considered as not found.
122
            throw new NotFoundException($request, $response);
123
        }
124
125
        return $this->twig->render($response, 'paste.twig', [
126
            'paste' => $paste,
127
        ]);
128
    }
129
}
130