UserAuth::extensionHandles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Route
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Route\Extension;
16
17
use Phossa2\Route\Status;
18
use Phossa2\Route\Dispatcher;
19
use Phossa2\Event\Interfaces\EventInterface;
20
use Phossa2\Route\Interfaces\ResultInterface;
21
use Phossa2\Event\EventableExtensionAbstract;
22
23
/**
24
 * UserAuth
25
 *
26
 * Redirect to auth page if not authed and uri is '/user/' prefixed
27
 *
28
 * @package Phossa2\Route
29
 * @author  Hong Zhang <[email protected]>
30
 * @see     EventableExtensionAbstract
31
 * @see     ResultInterface
32
 * @version 2.0.0
33
 * @since   2.0.0 added
34
 */
35
class UserAuth extends EventableExtensionAbstract
36
{
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function methodsAvailable()/*# : array */
41
    {
42
        return ['doAuth'];
43
    }
44
45
    /**
46
     * Extension methods takes an event as input.
47
     *
48
     * Event params has `Phossa2\Route\Result` set as 'result'
49
     *
50
     * MUST RETURN A BOOLEAN VALUE !!!
51
     *
52
     * @param  EventInterface $event
53
     * @return bool
54
     * @access protected
55
     */
56
    public function doAuth(EventInterface $event)/*# : bool */
0 ignored issues
show
Coding Style introduced by
doAuth uses the super-global variable $_SESSION which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
57
    {
58
        /* @var ResultInterface $result */
59
        $result = $event->getParam('result');
60
        $path = $result->getPath();
61
62
        if (!isset($_SESSION['authed']) && '/user/' === substr($path, 0, 6)) {
63
            $result->setStatus(Status::UNAUTHORIZED);
64
            return false;
65
        }
66
        return true;
67
    }
68
69
    /**
70
     * Return event handlers of this extension handling
71
     *
72
     * ```php
73
     * protected function extensionHandles()
74
     * {
75
     *     return [
76
     *         ['event' => 'cache.*', 'handler' => ['byPassCache', 100]],
77
     *     ];
78
     * }
79
     * ```
80
     *
81
     * @return array
82
     * @access protected
83
     */
84
    protected function extensionHandles()/*# : array */
85
    {
86
        return [
87
            [
88
                'event' => Dispatcher::EVENT_BEFORE_MATCH,
89
                'handler' => 'doAuth'
90
            ]
91
        ];
92
    }
93
}
94