| Total Complexity | 4 |
| Total Lines | 34 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 19 | trait ProtectedController |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var bool Contain login status |
||
| 23 | */ |
||
| 24 | private $authentication = false; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Allow access to controller class or methods only if logged. |
||
| 28 | * Return a status code, useful with AJAX requests. |
||
| 29 | * |
||
| 30 | * @param Authentication $authentication |
||
| 31 | * @param int $httpResponseCode |
||
| 32 | */ |
||
| 33 | 3 | private function protect(Authentication $authentication, int $httpResponseCode = 403): void |
|
| 34 | { |
||
| 35 | 3 | if (($this->authentication = $authentication->isLogged()) === false) { |
|
| 36 | 2 | http_response_code($httpResponseCode); |
|
| 37 | 2 | throw new AuthenticationException(''); |
|
| 38 | } |
||
| 39 | 1 | } |
|
| 40 | |||
| 41 | /** |
||
| 42 | * Allow access to controller class or methods only if logged |
||
| 43 | * and do a redirection. |
||
| 44 | * |
||
| 45 | * @param Authentication $authentication |
||
| 46 | * @param string $location |
||
| 47 | */ |
||
| 48 | 2 | private function protectWithRedirect(Authentication $authentication, string $location): void |
|
| 53 | } |
||
| 54 | } |
||
| 56 |