Completed
Push — master ( 08d36e...676503 )
by Raphaël
02:11
created

CertainRessourceAbstract::post()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 5
Ratio 33.33 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 5
loc 15
rs 8.2222
cc 7
eloc 10
nc 4
nop 5
1
<?php
2
namespace Wabel\CertainAPI;
3
4
use Wabel\CertainAPI\Interfaces\CertainRessourceInterface;
5
use Wabel\CertainAPI\Interfaces\CertainResponseInterface;
6
7
/**
8
 * CertainRessourceAbstracct for common action about Ressource
9
 *
10
 * @author rbergina
11
 */
12
13
use Wabel\CertainAPI\CertainApiService;
14
15
abstract class CertainRessourceAbstract implements CertainRessourceInterface, CertainResponseInterface
16
{
17
    const NOT_FOUND = 404;
18
    /**
19
     * CertainApiService
20
     * @var CertainApiService
21
     */
22
    protected $certainApiService;
23
    /**
24
     * array of results with information about the request
25
     * @var array
26
     */
27
    protected $results;
28
29
    /**
30
     *
31
     * @var string
32
     */
33
    protected $ressourceCalled;
34
35
36
    /**
37
     * @param CertainApiService $certainApiService
38
     */
39
    public function __construct(CertainApiService $certainApiService)
40
    {
41
        $this->certainApiService = $certainApiService;
42
    }
43
44
45
    /**
46
     * Get information about ressource
47
     * @param string $ressourceId
48
     * @param array $params
49
     * @param boolean $assoc
50
     * @param string $contentType
51
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
52
     * @throws Exceptions\RessourceException
53
     */
54 View Code Duplication
    public function get($ressourceId= null,$params= array(), $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...
55
        $ressourceName = $this->getRessourceName();;
56
        if($ressourceName == '' || is_null($ressourceName)){
57
            throw new Exceptions\RessourceException('No ressource name provided.');
58
        }
59
        $this->results = $this->certainApiService->get($ressourceName,$this->ressourceCalled, $ressourceId,$params, $assoc, $contentType);
60
        return $this;
61
    }
62
63
    /**
64
     * Add/Update information to certain
65
     * @param array $bodyData
66
     * @param array $query
67
     * @param string $ressourceId
68
     * @param boolean $assoc
69
     * @param string $contentType
70
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
71
     * @throws Exceptions\RessourceException
72
     * @throws Exceptions\RessourceMandatoryFieldException
73
     */
74
    public function post($bodyData, $query=array(), $ressourceId= null, $assoc = false, $contentType='json'){
75
        $ressourceName = $this->getRessourceName();;
76
        if($ressourceName == '' || is_null($ressourceName)){
77
            throw new Exceptions\RessourceException('No ressource name provided.');
78
        }
79
        if($ressourceId === null && count($this->getMandatoryFields())>0){
80 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...
81
                if(!in_array($field,  array_keys($bodyData))){
82
                    throw new Exceptions\RessourceMandatoryFieldException(sprintf('The field %s is required',$field));
83
                }
84
            }
85
        }
86
        $this->results = $this->certainApiService->post($ressourceName, $this->ressourceCalled, $ressourceId, $bodyData, $query, $assoc, $contentType);
87
        return $this;
88
    }
89
90
   /**
91
     * Update information to certain
92
     * @param array $bodyData
93
     * @param array $query
94
     * @param string $ressourceId
95
     * @param boolean $assoc
96
     * @param string $contentType
97
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
98
     * @throws Exceptions\RessourceException
99
     * @throws Exceptions\RessourceMandatoryFieldException
100
     */
101
    public function put($bodyData, $query=array(), $ressourceId= null, $assoc = false, $contentType='json'){
102
        $ressourceName = $this->getRessourceName();;
103
        if($ressourceName == '' || is_null($ressourceName)){
104
            throw new Exceptions\RessourceException('No ressource name provided.');
105
        }
106
        if(!is_null($ressourceId) && count($this->getMandatoryFields())>0){
107 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...
108
                if(!in_array($field,  array_keys($bodyData))){
109
                    throw new Exceptions\RessourceMandatoryFieldException(sprintf('The field %s is required',$field));
110
                }
111
            }
112
        }else{
113
            throw new Exceptions\RessourceMandatoryFieldException(sprintf('The id field is required'));
114
        }
115
        $this->results = $this->certainApiService->put($ressourceName, $this->ressourceCalled, $ressourceId, $bodyData, $query, $assoc, $contentType);
116
        return $this;
117
    }
118
119
    /**
120
     * Delete information from certain
121
     * @param string $ressourceId
122
     * @param boolean $assoc
123
     * @param string $contentType
124
     * @return \Wabel\CertainAPI\CertainRessourceAbstract
125
     * @throws Exceptions\RessourceException
126
     */
127 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...
128
        $ressourceName = $this->getRessourceName();;
129
        if($ressourceName == '' || is_null($ressourceName)){
130
            throw new Exceptions\RessourceException('No ressource name provided.');
131
        }
132
        $this->results = $this->certainApiService->delete($ressourceName, $this->ressourceCalled, $ressourceId, $assoc, $contentType);
133
        return $this;
134
    }
135
136
    /**
137
     * Check is a successful request
138
     * @return boolean
139
     */
140
    public function isSuccessFul()
141
    {
142
        return $this->results['success'];
143
    }
144
145
    /**
146
     * Check is not found.
147
     * @return boolean
148
     */
149
    public  function isNotFound(){
150
        if(isset($this->results['statusCode']) && $this->results['statusCode'] == self::NOT_FOUND){
151
            return true;
152
        } elseif (isset($this->results['statusCode']) && $this->results['statusCode'] != self::NOT_FOUND){
153
            return false;
154
        }
155
        return null;
156
    }
157
158
    /**
159
     * Get the results
160
     * @return \stdClass|\stdClass[]|array
161
     */
162
    public function getResults()
163
    {
164
        return $this->results['results'];
165
    }
166
167
    /**
168
     * Get the succes value, results,  success or fail reason
169
     * @return array
170
     */
171
    public function getCompleteResults(){
172
        return $this->results;
173
    }
174
175
    public function getRessourceCalled()
176
    {
177
        return $this->ressourceCalled;
178
    }
179
180
    public function createRessourceCalled($ressourceCalledParameters = null)
181
    {
182
        if(is_array($ressourceCalledParameters) && !empty($ressourceCalledParameters)){
183
            foreach ($ressourceCalledParameters as $segmentKey => $segmentValue) {
184
                if($segmentValue != ''){
185
                    $this->ressourceCalled .= '/'.$segmentKey.'/'.$segmentValue;
186
                }else{
187
                    $this->ressourceCalled .= '/'.$segmentKey;
188
                }
189
190
            }
191
        }
192
193
        return $this;
194
    }
195
196
197
198
}