GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

BaseMiddleware   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 70
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A restrict() 0 4 1
B shouldRun() 0 22 6
A preprocess() 0 1 1
A preflight() 0 1 1
A preroute() 0 1 1
A prerender() 0 1 1
1
<?php
2
3
namespace Zaphpa;
4
5
abstract class BaseMiddleware {
6
7
    const ALL_METHODS = '*';
8
9
    public $scope = array();
10
    public static $context = array();
11
    public static $routes = array();
12
13
14
    /**
15
     *
16
     *  Restrict a Middleware hook to certain paths and HTTP methods.
17
     *
18
     *  No actual restriction takes place in this method.
19
     *  We simply place the $methods array into $this->scope, keyed by its $hook.
20
     *
21
     * @param $methods
22
     *  An array of HTTP methods that are allowed, or an '*' to match all methods.
23
     * @param $route
24
     *  Route to restrict the middleware to. Please note: this must be a route you added with addRoute(), not
25
     *  just any path.
26
     * @return $this
27
     */
28
    public function restrict($methods, $route) {
29
        $this->scope[$route] = $methods;
30
        return $this;
31
    }
32
33
    /**
34
     *  Determine whether the current route has any route restrictions for this BaseMiddleware.
35
     *
36
     *  BaseMiddleware must have self::$context['pattern'] and self::$context['http_method'] set.
37
     *  Furthermore $context['http_method'] can be an array (preflight uses that).
38
     *
39
     *  @return bool
40
     *    Whether the current route should run $hook.
41
     */
42
    public function shouldRun() {
43
        if (empty($this->scope)) return true; // no restrictions
44
45
        if (array_key_exists(self::$context['pattern'], $this->scope)) {
46
            $methods = $this->scope[self::$context['pattern']];
47
48
            if ($methods == self::ALL_METHODS) {
49
                return true;
50
            }
51
52
            if (!is_array($methods)) {
53
                return false;
54
            }
55
56
            if (!in_array(strtolower(self::$context['http_method']), array_map('strtolower', $methods))) {
57
                return false;
58
            }
59
        } else {
60
            return false;
61
        }
62
        return true;
63
    }
64
65
    /** Preprocess. This is where you'd add new routes **/
66
    public function preprocess(&$router) {}
67
    /** Preflight. This is where do things after routes are finalized but before processing starts **/
68
    public function preflight() {}
69
    /** Preroute. This is where you would alter request, or implement things like: security etc. **/
70
    public function preroute(&$req, &$res) {}
71
    /** This is your chance to override output. It can be called multiple times for each ->flush() invocation! **/
72
    public function prerender(&$buffer) {}
73
74
} // end Middleware