Completed
Push — master ( 3df6d0...513e0e )
by Hans
06:54 queued 05:39
created

Request   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 82.61%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 2
cbo 0
dl 0
loc 125
ccs 19
cts 23
cp 0.8261
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A setFields() 0 9 1
A getMethod() 0 4 1
A getEndpoint() 0 4 1
A getParams() 0 4 1
A getHeaders() 0 4 1
A isPost() 0 4 1
A isGet() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Pinterest PHP library.
5
 *
6
 * (c) Hans Ott <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.md.
10
 *
11
 * Source: https://github.com/hansott/pinterest-php
12
 */
13
14
namespace Pinterest\Http;
15
16
/**
17
 * The request class.
18
 *
19
 * @author Hans Ott <[email protected]>
20
 * @author Toon Daelman <[email protected]>
21
 */
22
final class Request
23
{
24
    /**
25
     * The http method.
26
     *
27
     * @var string
28
     */
29
    private $method;
30
31
    /**
32
     * The endpoint to call.
33
     *
34
     * Relative url.
35
     *
36
     * @var string
37
     */
38
    private $endpoint;
39
40
    /**
41
     * The parameters to pass.
42
     *
43
     * @var array
44
     */
45
    private $params;
46
47
    /**
48
     * The headers to pass.
49
     *
50
     * @var array
51
     */
52
    private $headers;
53
54
    /**
55
     * The constructor.
56
     *
57
     * @param string $method   The http method.
58
     * @param string $endpoint The relative url to call.
59
     * @param array  $params   The parameters.
60
     * @param array  $headers  The headers.
61
     */
62 34
    public function __construct($method, $endpoint, array $params = array(), array $headers = array())
63
    {
64 34
        $this->method = strtoupper((string) $method);
65 34
        $this->endpoint = (string) $endpoint;
66 34
        $this->params = $params;
67 34
        $this->headers = $headers;
68 34
    }
69
70
    /**
71
     * Sets the fields.
72
     *
73
     * @param array $fields The fields to return.
74
     *
75
     * @return Request The current Request instance.
76
     */
77 30
    public function setFields(array $fields)
78
    {
79
        $merge = array(
80 30
            'fields' => implode(',', $fields),
81 15
        );
82 30
        $this->params = array_replace($this->params, $merge);
83
84 30
        return $this;
85
    }
86
87
    /**
88
     * Get the http (lowercase) method.
89
     *
90
     * @return string
91
     */
92 34
    public function getMethod()
93
    {
94 34
        return $this->method;
95
    }
96
97
    /**
98
     * Get the http endpoint.
99
     *
100
     * @return string
101
     */
102 34
    public function getEndpoint()
103
    {
104 34
        return $this->endpoint;
105
    }
106
107
    /**
108
     * Get the http parameters.
109
     *
110
     * @return array
111
     */
112 34
    public function getParams()
113
    {
114 34
        return $this->params;
115
    }
116
117
    /**
118
     * Get the http headers.
119
     *
120
     * @return array
121
     */
122 34
    public function getHeaders()
123
    {
124 34
        return $this->headers;
125
    }
126
127
    /**
128
     * Is this a POST request?
129
     *
130
     * @return bool
131
     */
132
    public function isPost()
133
    {
134
        return $this->getMethod() === 'POST';
135
    }
136
137
    /**
138
     * Is this a GET request?
139
     *
140
     * @return bool
141
     */
142
    public function isGet()
143
    {
144
        return $this->getMethod() === 'GET';
145
    }
146
}
147