Completed
Push — master ( b6df4e...3ad32d )
by Raphaël
02:17
created

CertainRessourceAbstract::postWithEventCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 7
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 6
1
<?php
2
3
namespace Wabel\CertainAPI;
4
5
use Wabel\CertainAPI\Interfaces\CertainRessourceInterface;
6
use Wabel\CertainAPI\Interfaces\CertainResponseInterface;
7
/**
8
 * CertainRessourceAbstracct for common action about Ressource
9
 *
10
 * @author rbergina
11
 */
12
use Wabel\CertainAPI\CertainApiService;
13
14
abstract class CertainRessourceAbstract implements CertainRessourceInterface, CertainResponseInterface
15
{
16
    const NOT_FOUND = 404;
17
18
    /**
19
     * CertainApiService
20
     * @var CertainApiService
21
     */
22
    protected $certainApiService;
23
24
    /**
25
     * array of results with information about the request
26
     * @var array
27
     */
28
    protected $results;
29
30
    /**
31
     *
32
     * @var string
33
     */
34
    protected $ressourceCalled;
35
36
    /**
37
     * @param CertainApiService $certainApiService
38
     */
39
    public function __construct(CertainApiService $certainApiService)
40
    {
41
        $this->certainApiService = $certainApiService;
42
    }
43
44
    /**
45
     * Get information about ressource
46
     * @param string $ressourceId
47
     * @param array $params
48
     * @param boolean $assoc
49
     * @param string $contentType
50
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
51
     * @throws Exceptions\RessourceException
52
     */
53 View Code Duplication
    public function get($ressourceId = null, $params = array(), $assoc = false,
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...
54
                        $contentType = 'json')
55
    {
56
        $ressourceName = $this->getRessourceName();
57
        ;
58
        if ($ressourceName == '' || is_null($ressourceName)) {
59
            throw new Exceptions\RessourceException('No ressource name provided.');
60
        }
61
        $this->results = $this->certainApiService->get($ressourceName,
62
            $this->ressourceCalled, $ressourceId, $params, $assoc, $contentType);
63
        return $this;
64
    }
65
66
    /**
67
     * Add/Update information to certain
68
     * @param array $bodyData
69
     * @param array $query
70
     * @param string $ressourceId
71
     * @param boolean $assoc
72
     * @param string $contentType
73
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
74
     * @throws Exceptions\RessourceException
75
     * @throws Exceptions\RessourceMandatoryFieldException
76
     */
77
    public function post($bodyData, $query = array(), $ressourceId = null,
78
                         $assoc = false, $contentType = 'json')
79
    {
80
        $ressourceName = $this->getRessourceName();
81
        ;
82
        if ($ressourceName == '' || is_null($ressourceName)) {
83
            throw new Exceptions\RessourceException('No ressource name provided.');
84
        }
85
        if ($ressourceId === null && count($this->getMandatoryFields()) > 0) {
86 View Code Duplication
            foreach ($this->getMandatoryFields() as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
                if (!in_array($field, array_keys($bodyData))) {
88
                    throw new Exceptions\RessourceMandatoryFieldException(sprintf('The field %s is required',
89
                        $field));
90
                }
91
            }
92
        }
93
        $this->results = $this->certainApiService->post($ressourceName,
94
            $this->ressourceCalled, $ressourceId, $bodyData, $query, $assoc,
95
            $contentType);
96
        return $this;
97
    }
98
99
    /**
100
     * Update information to certain
101
     * @param array $bodyData
102
     * @param array $query
103
     * @param string $ressourceId
104
     * @param boolean $assoc
105
     * @param string $contentType
106
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
107
     * @throws Exceptions\RessourceException
108
     * @throws Exceptions\RessourceMandatoryFieldException
109
     */
110
    public function put($bodyData, $query = array(), $ressourceId = null,
111
                        $assoc = false, $contentType = 'json')
112
    {
113
        $ressourceName = $this->getRessourceName();
114
        ;
115
        if ($ressourceName == '' || is_null($ressourceName)) {
116
            throw new Exceptions\RessourceException('No ressource name provided.');
117
        }
118
        if (!is_null($ressourceId) && count($this->getMandatoryFields()) > 0) {
119 View Code Duplication
            foreach ($this->getMandatoryFields() as $field) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
120
                if (!in_array($field, array_keys($bodyData))) {
121
                    throw new Exceptions\RessourceMandatoryFieldException(sprintf('The field %s is required',
122
                        $field));
123
                }
124
            }
125
        } else {
126
            throw new Exceptions\RessourceMandatoryFieldException(sprintf('The id field is required'));
127
        }
128
        $this->results = $this->certainApiService->put($ressourceName,
129
            $this->ressourceCalled, $ressourceId, $bodyData, $query, $assoc,
130
            $contentType);
131
        return $this;
132
    }
133
134
    /**
135
     * Delete information from certain
136
     * @param string $ressourceId
137
     * @param boolean $assoc
138
     * @param string $contentType
139
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
140
     * @throws Exceptions\RessourceException
141
     */
142 View Code Duplication
    public function delete($ressourceId, $assoc = false, $contentType = 'json')
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...
143
    {
144
        $ressourceName = $this->getRessourceName();
145
        ;
146
        if ($ressourceName == '' || is_null($ressourceName)) {
147
            throw new Exceptions\RessourceException('No ressource name provided.');
148
        }
149
        $this->results = $this->certainApiService->delete($ressourceName,
150
            $this->ressourceCalled, $ressourceId, $assoc, $contentType);
151
        return $this;
152
    }
153
154
    /**
155
     * Check is a successful request
156
     * @return boolean
157
     */
158
    public function isSuccessFul()
159
    {
160
        return $this->results['success'];
161
    }
162
163
    /**
164
     * Check is not found.
165
     * @return boolean
166
     */
167
    public function isNotFound()
168
    {
169
        if (isset($this->results['statusCode']) && $this->results['statusCode'] == self::NOT_FOUND) {
170
            return true;
171
        } elseif (isset($this->results['statusCode']) && $this->results['statusCode']
172
            != self::NOT_FOUND) {
173
            return false;
174
        }
175
        return null;
176
    }
177
178
    /**
179
     * Get the results
180
     * @return \stdClass|\stdClass[]|array
181
     */
182
    public function getResults()
183
    {
184
        return $this->results['results'];
185
    }
186
187
    /**
188
     * Get the succes value, results,  success or fail reason
189
     * @return array
190
     */
191
    public function getCompleteResults()
192
    {
193
        return $this->results;
194
    }
195
196
    public function getRessourceCalled()
197
    {
198
        return $this->ressourceCalled;
199
    }
200
201
    public function createRessourceCalled($ressourceCalledParameters = null)
202
    {
203
        $this->ressourceCalled = '';
204
        if (is_array($ressourceCalledParameters) && !empty($ressourceCalledParameters)) {
205
            foreach ($ressourceCalledParameters as $segmentKey => $segmentValue) {
206
                if ($segmentValue != '') {
207
                    $this->ressourceCalled .= '/'.$segmentKey.'/'.$segmentValue;
208
                } else {
209
                    $this->ressourceCalled .= '/'.$segmentKey;
210
                }
211
            }
212
        }
213
214
        return $this;
215
    }
216
217
    /**
218
     *
219
     * @param string $eventCode
220
     * @param string $ressourceId
221
     * @param array $params
222
     * @param boolean $assoc
223
     * @param string $contentType
224
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
225
     */
226
    public function getWithEventCode($eventCode, $ressourceId, $params = array(),
227
                                     $assoc = false, $contentType = 'json')
228
    {
229
        $ressourceId = $eventCode.'/'.$ressourceId;
230
        return $this->get($ressourceId, $params, $assoc, $contentType);
231
    }
232
233
    /**
234
     *
235
     * @param string $eventCode
236
     * @param string $ressourceId
237
     * @param array $bodyData
238
     * @param array $query
239
     * @param boolean $assoc
240
     * @param string $contentType
241
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
242
     */
243 View Code Duplication
    public function postWithEventCode($eventCode, $ressourceId, $bodyData,
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...
244
                                      $query = array(), $assoc = false,
245
                                      $contentType = 'json')
246
    {
247
        $ressourceId = $eventCode.'/'.$ressourceId;
248
        return $this->post($bodyData, $query, $ressourceId, $assoc, $contentType);
249
    }
250
251
    /**
252
     *
253
     * @param string $eventCode
254
     * @param string $ressourceId
255
     * @param array $bodyData
256
     * @param array $query
257
     * @param boolean $assoc
258
     * @param string $contentType
259
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
260
     */
261 View Code Duplication
    public function putWithEventCode($eventCode, $ressourceId, $bodyData,
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...
262
                                     $query = array(), $assoc = false,
263
                                     $contentType = 'json')
264
    {
265
        $ressourceId = $eventCode.'/'.$ressourceId;
266
        return $this->put($bodyData, $query, $ressourceId, $assoc, $contentType);
267
    }
268
269
    /**
270
     *
271
     * @param type $eventCode
272
     * @param string $ressourceId
273
     * @param boolean $assoc
274
     * @param string $contentType
275
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
276
     */
277
    public function deleteWithEventCode($eventCode, $ressourceId,
278
                                        $assoc = false, $contentType = 'json')
279
    {
280
        $ressourceId = $eventCode.'/'.$ressourceId;
281
        return $this->delete($ressourceId, $assoc, $contentType);
282
    }
283
284
    /**
285
     * Return the size.
286
     * @return int
287
     */
288
    public function getSize()
289
    {
290
        $result = $this->getResults();
291
        if (!isset($result->size)) {
292
            return null;
293
        }
294
        return $result->size;
295
    }
296
297
    /**
298
     * Return the completeCollectionSizet.
299
     * @return int
300
     */
301
    public function getCompleteCollectionSize()
302
    {
303
        $result = $this->getResults();
304
        if (!isset($result->size)) {
305
            return null;
306
        }
307
        return $result->completeCollectionSize;
308
    }
309
310
    /**
311
     * Return the maxResults.
312
     * @return int
313
     */
314
    public function getMaxResults()
315
    {
316
        $result = $this->getResults();
317
        if (!isset($result->size)) {
318
            return null;
319
        }
320
        return $result->maxResults;
321
    }
322
}