1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Traits; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
6
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait ErrorHandling |
10
|
|
|
* @package Nip\Controllers\Traits |
11
|
|
|
*/ |
12
|
|
|
trait ErrorHandling |
13
|
|
|
{ |
14
|
|
|
protected function dispatchAccessDeniedResponse() |
15
|
|
|
{ |
16
|
|
|
throw $this->createAccessDeniedException(); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
protected function dispatchNotFoundResponse() |
20
|
|
|
{ |
21
|
|
|
throw $this->createNotFoundException(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Returns a NotFoundHttpException. |
26
|
|
|
* |
27
|
|
|
* This will result in a 404 response code. Usage example: |
28
|
|
|
* |
29
|
|
|
* throw $this->createNotFoundException('Page not found!'); |
30
|
|
|
*/ |
31
|
|
|
protected function createNotFoundException( |
32
|
|
|
string $message = 'Not Found', |
33
|
|
|
\Throwable $previous = null |
34
|
|
|
): NotFoundHttpException { |
35
|
|
|
return new NotFoundHttpException($message, $previous); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns an AccessDeniedException. |
40
|
|
|
* |
41
|
|
|
* This will result in a 403 response code. Usage example: |
42
|
|
|
* |
43
|
|
|
* throw $this->createAccessDeniedException('Unable to access this page!'); |
44
|
|
|
* |
45
|
|
|
* @throws \LogicException If the Security component is not available |
46
|
|
|
*/ |
47
|
|
|
protected function createAccessDeniedException( |
48
|
|
|
string $message = 'Access Denied.', |
49
|
|
|
\Throwable $previous = null |
50
|
|
|
): AccessDeniedException { |
51
|
|
|
if (!class_exists(AccessDeniedException::class)) { |
52
|
|
|
throw new \LogicException('You can not use the "createAccessDeniedException" method if the Security component is not available. Try running "composer require symfony/security-bundle".'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return new AccessDeniedException($message, $previous); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|