Completed
Pull Request — master (#37)
by
unknown
03:22
created

MoipResource::createConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Moip\Resource;
4
5
use JsonSerializable;
6
use Moip\Http\HTTPConnection;
7
use Moip\Http\HTTPRequest;
8
use Moip\Moip;
9
use RuntimeException;
10
use stdClass;
11
12
abstract class MoipResource implements JsonSerializable
13
{
14
    /**
15
     * Version of API.
16
     *
17
     * @const string
18
     */
19
    const VERSION = 'v2';
20
21
    /**
22
     * @var \Moip\Moip
23
     */
24
    protected $moip;
25
26
    /**
27
     * @var \stdClass
28
     */
29
    protected $data;
30
31
    /**
32
     * Initialize a new instance.
33
     */
34
    abstract protected function initialize();
35
36
    /**
37
     * Mount information of a determined object.
38
     *
39
     * @param \stdClass $response
40
     *
41
     * @return mixed
42
     */
43
    abstract protected function populate(stdClass $response);
44
45
    /**
46
     * Create a new instance.
47
     *
48
     * @param \Moip\Moip $moip
49
     */
50
    public function __construct(Moip $moip)
51
    {
52
        $this->moip = $moip;
53
        $this->data = new stdClass();
54
        $this->initialize();
55
    }
56
57
    /**
58
     * Create a new connecttion.
59
     *
60
     * @return \Moip\Http\HTTPConnection
61
     */
62
    protected function createConnection()
63
    {
64
        return $this->moip->createConnection(new HTTPConnection());
65
    }
66
67
    /**
68
     * Get a key of an object if he exist.
69
     *
70
     * @param string         $key
71
     * @param \stdClass|null $data
72
     *
73
     * @return mixed
74
     */
75
    protected function getIfSet($key, stdClass $data = null)
76
    {
77
        if ($data == null) {
78
            $data = $this->data;
79
        }
80
81
        if (isset($data->$key)) {
82
            return $data->$key;
83
        }
84
    }
85
86
    /**
87
     * Get a key, representing a date (Y-m-d), of an object if it exists.
88
     * @param string $key
89
     * @param stdClass|null $data
90
     * @return \DateTime|null
91
     */
92 View Code Duplication
    protected function getIfSetDate($key, stdClass $data = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
94
        $val = $this->getIfSet($key, $data);
95
        if(!empty($val)){
96
            return \DateTime::createFromFormat('Y-m-d', $val);
97
        }
98
        return null;
99
100
    }
101
102
    /**
103
     * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists.
104
     * @param string $key
105
     * @param stdClass|null $data
106
     * @return \DateTime|null
107
     */
108
109 View Code Duplication
    protected function getIfSetDateTime($key, stdClass $data = null){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
        $val = $this->getIfSet($key, $data);
111
        if(!empty($val)){
112
            return \DateTime::createFromFormat(\DateTime::ATOM, $val);
113
        }
114
        return null;
115
    }
116
117
    /**
118
     * Specify data which should be serialized to JSON.
119
     *
120
     * @return \stdClass
121
     */
122
    public function jsonSerialize()
123
    {
124
        return $this->data;
125
    }
126
127
    /**
128
     * Find by path.
129
     *
130
     * @param string $path
131
     *
132
     * @return stdClass
133
     */
134
    public function getByPath($path)
135
    {
136
        $httpConnection = $this->createConnection();
137
        $httpConnection->addHeader('Content-Type', 'application/json');
138
139
        $httpResponse = $httpConnection->execute($path, HTTPRequest::GET);
140
141
        if ($httpResponse->getStatusCode() != 200) {
142
            throw new RuntimeException($httpResponse->getStatusMessage(), $httpResponse->getStatusCode());
143
        }
144
145
        return $this->populate(json_decode($httpResponse->getContent()));
146
    }
147
148
    /**
149
     * Create a new item in Moip.
150
     *
151
     * @param string $path
152
     *
153
     * @return stdClass
154
     */
155
    public function createResource($path)
156
    {
157
        $body = json_encode($this, JSON_UNESCAPED_SLASHES);
158
159
        $httpConnection = $this->createConnection();
160
        $httpConnection->addHeader('Content-Type', 'application/json');
161
        $httpConnection->addHeader('Content-Length', strlen($body));
162
        $httpConnection->setRequestBody($body);
163
164
        $httpResponse = $httpConnection->execute($path, HTTPRequest::POST);
165
166
        if ($httpResponse->getStatusCode() != 201) {
167
            throw new RuntimeException($httpResponse->getStatusMessage(), $httpResponse->getStatusCode());
168
        }
169
170
        return $this->populate(json_decode($httpResponse->getContent()));
171
    }
172
}
173