MailxpertRequest::setAccessToken()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 4
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * sources.
4
 * Date: 14/08/15
5
 */
6
7
namespace Mailxpert;
8
9
use Mailxpert\Authentication\AccessToken;
10
11
/**
12
 * Class MailxpertRequest
13
 * @package Mailxpert
14
 */
15
class MailxpertRequest
16
{
17
    /**
18
     * @var MailxpertApp
19
     */
20
    private $app;
21
    private $accessToken;
22
    private $method;
23
    private $endpoint;
24
    private $params;
25
    protected $headers = [];
26
    private $body;
27
28
    /**
29
     * MailxpertRequest constructor.
30
     *
31
     * @param MailxpertApp            $app
32
     * @param AccessToken|string|null $accessToken
33
     * @param string                  $method
34
     * @param string                  $endpoint
35
     * @param array                   $params
36
     * @param string                  $body
37
     */
38
    public function __construct(MailxpertApp $app, $accessToken, $method, $endpoint, $params, $body)
39
    {
40
        $this->app = $app;
41
        $this->setAccessToken($accessToken);
42
        $this->method = $method;
43
        $this->endpoint = $endpoint;
44
        $this->params = $params;
45
        $this->body = $body;
46
    }
47
48
    /**
49
     * @return MailxpertApp
50
     */
51
    public function getApp()
52
    {
53
        return $this->app;
54
    }
55
56
    /**
57
     * @param AccessToken|string|null $accessToken
58
     *
59
     * @return MailxpertRequest
60
     */
61
    public function setAccessToken($accessToken)
62
    {
63
        $this->accessToken = $accessToken;
64
        if ($accessToken instanceof AccessToken) {
65
            $this->accessToken = $accessToken->getAccessToken();
66
        }
67
68
        if (!is_null($this->accessToken)) {
69
            $this->headers['Authorization'] = 'Bearer '.$this->accessToken;
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public function getAccessToken()
79
    {
80
        return $this->accessToken;
81
    }
82
83
    /**
84
     * @return mixed
85
     */
86
    public function getMethod()
87
    {
88
        return $this->method;
89
    }
90
91
    /**
92
     * @return mixed
93
     */
94
    public function getEndpoint()
95
    {
96
        return $this->endpoint;
97
    }
98
99
    /**
100
     * @return mixed
101
     */
102
    public function getParams()
103
    {
104
        return $this->params;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getUrl()
111
    {
112
        $url = $this->endpoint;
113
114
        if ($this->getMethod() !== 'POST') {
115
            $url .= '?'.http_build_query($this->getParams(), null, '&');
116
        }
117
118
        return $url;
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    public function getHeaders()
125
    {
126
        return $this->headers;
127
    }
128
129
    /**
130
     * @return string
131
     */
132
    public function getBody()
133
    {
134
        return $this->body;
135
    }
136
}
137