Completed
Push — b0.25.0 ( 47a5e1...be320f )
by Sebastian
03:27
created

ProtectedController::protect()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2.5

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
ccs 2
cts 4
cp 0.5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.5
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authentication;
13
14
/**
15
 * Help protect a controller with login.
16
 */
17
trait ProtectedController
18
{
19
    /**
20
     * @var bool Contain login status
21
     */
22
    private $authentication = false;
23
24
    /**
25
     * Allow access to controller only if logged.
26
     *
27
     * @param Authentication $authentication
28
     * @param string         $redirect
29
     */
30 1
    private function protect(Authentication $authentication, string $redirect): void
31
    {
32 1
        if (($this->authentication = $authentication->isLogged()) === false) {
33
            header('Location: '.$redirect);
34
            exit;
35
        }
36 1
    }
37
}
38