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 |
|
|
|
|
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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.