Completed
Push — master ( 975112...14a546 )
by Siro Díaz
02:05
created

Order::isValidMiddleware()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 17
rs 8.8571
cc 5
eloc 9
nc 5
nop 1
1
<?php
2
3
namespace Routify;
4
5
use Routify\Exceptions\InvalidMiddlewareException;
6
7
class Order {
8
9
    /**
10
     * @var string The path pattern.
11
     */
12
    private $uri;
13
14
    /**
15
     * @var callable The callback response.
16
     */
17
    private $response;
18
19
    /**
20
     * @var string The request method for the path pattern.
21
     */
22
    private $method;
23
24
    /**
25
     * @var array Contains all middlewares associate to the action.
26
     */
27
    private $middlewares = [];
28
29
    /**
30
     * @var array The types that are supported(for middlewares).
31
     */
32
    private $middlewareTypes = ['before', 'after'];
33
34
    public function __construct($uri, $method, $response, array $middlewares = []) {
35
        $this->uri = $uri;
36
        $this->method = $method;
37
        $this->response = $response;
38
        if($this->isValidMiddleware($middlewares)) {
39
            $this->middlewares = $middlewares;
40
        }
41
    }
42
43
    /**
44
     * Returns the uri(the pattern path).
45
     *
46
     * @return mixed
47
     */
48
49
    public function getUri() {
50
        return $this->uri;
51
    }
52
53
    /**
54
     * Returns the request method registered.
55
     *
56
     * @return mixed
57
     */
58
59
    public function getMethod() {
60
        return $this->method;
61
    }
62
63
    /**
64
     * Returns the callback associated to the pattern.
65
     *
66
     * @return mixed
67
     */
68
69
    public function getResponse() {
70
        return $this->response;
71
    }
72
73
    /**
74
     * Returns a list of middlewares registered.
75
     *
76
     * @return array
77
     */
78
79
    public function getMiddlewares() {
80
        return $this->middlewares;
81
    }
82
83
    /**
84
     * Checks if the middleware contains before callback.
85
     *
86
     * @return bool
87
     */
88
89
    public function hasBefore() {
90
        return array_key_exists('before', $this->middlewares);
91
    }
92
93
    /**
94
     * Checks if the middleware contains after callback.
95
     *
96
     * @return bool
97
     */
98
99
    public function hasAfter() {
100
        return array_key_exists('after', $this->middlewares);
101
    }
102
103
    /**
104
     * Checks if middlewares passed as an associative array
105
     * don't contain keys different to "before" and "after".
106
     *
107
     * @param array $middleware The associative array containing
108
     *          middleware callbacks.
109
     * @return bool
110
     * @throws InvalidMiddlewareException
111
     */
112
113
    private function isValidMiddleware($middleware) {
114
        // if there is not middlewares then it is valid
115
        if(count($middleware) === 0) {
116
            return true;
117
        }
118
119
        foreach($middleware as $key => $value) {
120
            if(!in_array($key, $this->middlewareTypes)) {
121
                throw new InvalidMiddlewareException("Only before and after middleware types are valid");
122
            }
123
            if(!is_callable($value)) {
124
                throw new InvalidMiddlewareException("The middleware must be callable");
125
            }
126
        }
127
128
        return true;
129
    }
130
}