MethodHandler   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
c 2
b 0
f 0
lcom 1
cbo 2
dl 0
loc 91
ccs 25
cts 25
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 11 3
A isOverridable() 0 4 1
A getMethod() 0 14 3
A withMethod() 0 13 2
1
<?php
2
/**
3
 * Vperyod Method Override Handler
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Middleware
13
 * @package   Vperyod\MethodHandler
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      http://github.com/vperyod/vperyod.method-handler
18
 */
19
20
namespace Vperyod\MethodHandler;
21
22
use Psr\Http\Message\ResponseInterface as Response;
23
use Psr\Http\Message\ServerRequestInterface as Request;
24
25
/**
26
 * MethodHandler
27
 *
28
 * Parses request body and request headers and overrides request method for
29
 * allowed request types
30
 *
31
 * @category Middleware
32
 * @package  Vperyod\MethodHandler
33
 * @author   Jake Johns <[email protected]>
34
 * @license  http://jnj.mit-license.org/ MIT License
35
 * @link     http://github.com/vperyod/vperyod.method-handler
36
 */
37
class MethodHandler
38
{
39
    use MethodOverrideTrait;
40
41
    /**
42
     * __invoke
43
     *
44
     * @param Request  $request  PSR7 HTTP Request
45
     * @param Response $response PSR7 HTTP Response
46
     * @param callable $next     Next callable middleware
47
     *
48
     * @return Response
49
     *
50
     * @access public
51
     */
52 6
    public function __invoke(Request $request, Response $response, callable $next)
53
    {
54 6
        if (! $this->isOverridable($request)
55 6
            || ! $method = $this->getMethod($request)
56
        ) {
57 2
            return $next($request, $response);
58
        }
59
60 4
        $request = $this->withMethod($request, $method);
61 4
        return $next($request, $response);
62
    }
63
64
    /**
65
     * Is Request Overidable?
66
     *
67
     * Method is considered overrideable if it is a post request
68
     *
69
     * @param Request $request PSR7 HTTP Request
70
     *
71
     * @return bool
72
     *
73
     * @access protected
74
     */
75 6
    protected function isOverridable(Request $request)
76
    {
77 6
        return 'POST' === $request->getMethod();
78
    }
79
80
    /**
81
     * Get value with which to override method
82
     *
83
     * @param Request $request PSR7 HTTP Request
84
     *
85
     * @return string|null
86
     *
87
     * @access protected
88
     */
89 5
    protected function getMethod(Request $request)
90
    {
91 5
        $param = $this->getMethodOverrideParam();
92 5
        $params = $request->getParsedBody();
93
94 5
        if (isset($params[$param])) {
95 2
            return $params[$param];
96
        }
97
98 3
        $header = $this->getMethodOverrideHeader();
99 3
        if ($method = $request->getHeaderLine($header)) {
100 2
            return $method;
101
        }
102 1
    }
103
104
    /**
105
     * WithMethod
106
     *
107
     * @param Request $request DESCRIPTION
108
     * @param mixed   $method  DESCRIPTION
109
     *
110
     * @return Request
111
     *
112
     * @access protected
113
     */
114 4
    protected function withMethod(Request $request, $method)
115
    {
116 4
        $request = $request->withMethod($method);
117 4
        $body = $request->getParsedBody();
118 4
        $param = $this->getMethodOverrideParam();
119
120 4
        if (isset($body[$param])) {
121 2
            unset($body[$param]);
122 2
            $request = $request->withParsedBody($body);
123
        }
124
125 4
        return $request;
126
    }
127
}
128