Resource::setHttpMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Psecio\Invoke;
4
5
class Resource
6
{
7
	/**
8
	 * Curernt URI location
9
	 * @var string
10
	 */
11
	protected $uri;
12
13
	/**
14
	 * Current HTTP method/verb
15
	 * @var string
16
	 */
17
	protected $method;
18
19
	/**
20
	 * Create the resource, set the URL and HTTP method manually if desired
21
	 * 	URI will default to REQUEST_URI
22
	 *  HTTP method will default to REQUEST_METHOD
23
	 *
24
	 * @param string $uri URI for the resource
25
	 * @param string $httpMethod HTTP method for the resource
26
	 */
27
	public function __construct($uri = null, $httpMethod = null)
0 ignored issues
show
Coding Style introduced by
__construct uses the super-global variable $_SERVER 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...
28
	{
29
		$this->setUri(
30
			($uri !== null) ? $uri : $_SERVER['REQUEST_URI']
31
		);
32
		$this->setHttpMethod(
33
			($httpMethod !== null) ? $httpMethod : $_SERVER['REQUEST_METHOD']
34
		);
35
	}
36
37
	/**
38
	 * Set the curren tHTTP method
39
	 *
40
	 * @param string $method HTTP method (ex: GET, POST)
41
	 */
42
	public function setHttpMethod($method)
43
	{
44
		$this->method = strtoupper($method);
45
	}
46
47
	/**
48
	 * Get the current HTTP method
49
	 *
50
	 * @return string HTTP method currently set
51
	 */
52
	public function getHttpMethod()
53
	{
54
		return $this->method;
55
	}
56
57
	/**
58
	 * Set the current URI for the resource
59
	 *
60
	 * @param string $uri URI path
61
	 */
62
	public function setUri($uri)
63
	{
64
		$this->uri = $uri;
65
	}
66
67
	/**
68
	 * Get the curent URI setting
69
	 *
70
	 * @return string Current URI setting
71
	 */
72
	public function getUri($parsed = false)
73
	{
74
		return ($parsed === true) ? parse_url($this->uri) : $this->uri;
75
	}
76
77
	public function getParams()
78
	{
79
		$result = [];
80
		$parse = parse_url($this->uri);
81
82
		if (isset($parse['query'])) {
83
			$params = explode('&', $parse['query']);
84
			foreach ($params as $param) {
85
				list($key, $value) = explode('=', $param);
86
				$result[$key] = $value;
87
			}
88
		}
89
90
		return $result;
91
	}
92
}