1
|
|
|
<?php |
2
|
|
|
namespace agoalofalife\bpm\Actions; |
3
|
|
|
|
4
|
|
|
use agoalofalife\bpm\Assistants\ConstructorUrl; |
5
|
|
|
use agoalofalife\bpm\Assistants\VerifyValues; |
6
|
|
|
use agoalofalife\bpm\Contracts\Action; |
7
|
|
|
use agoalofalife\bpm\Contracts\ActionGet; |
8
|
|
|
use agoalofalife\bpm\Contracts\Authentication; |
9
|
|
|
use agoalofalife\bpm\KernelBpm; |
10
|
|
|
use Assert\Assert; |
11
|
|
|
use Assert\Assertion; |
12
|
|
|
use Assert\AssertionFailedException; |
13
|
|
|
use GuzzleHttp\ClientInterface; |
14
|
|
|
use GuzzleHttp\Exception\ClientException; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
class Read implements Action, ActionGet |
18
|
|
|
{ |
19
|
|
|
use ConstructorUrl; |
20
|
|
|
|
21
|
|
|
protected $kernel; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Type HTTP_TYPE select |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $HTTP_TYPE = 'GET'; |
28
|
|
|
|
29
|
|
|
protected $url = '?'; |
30
|
|
|
|
31
|
|
|
public function injectionKernel(KernelBpm $bpm) |
32
|
|
|
{ |
33
|
|
|
$this->kernel = $bpm; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getUrl() |
37
|
|
|
{ |
38
|
|
|
return $this->url; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return array url -> string , http_type -> string |
43
|
|
|
*/ |
44
|
|
|
public function processData() |
45
|
|
|
{ |
46
|
|
|
$this->getData(); |
47
|
|
|
return $this->kernel->getHandler(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getData() |
51
|
|
|
{ |
52
|
|
|
$this->query(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Request the type of filter |
57
|
|
|
* Design filterConstructor allows you to build logical expressions the conditions selecting the desired object . |
58
|
|
|
* Expressions filterConstructor can be used to reference the properties and literals , |
59
|
|
|
* as well as strings, numbers and Boolean expressions (true, false). |
60
|
|
|
* Expressions $ filter supports arithmetic , logical operations , and operations groups , |
61
|
|
|
* strings , date and time of the operation. |
62
|
|
|
* @documentation |
63
|
|
|
* @param $strRequest |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function filterConstructor($strRequest) |
67
|
|
|
{ |
68
|
|
|
$ParameterQuery = '$filter='; |
69
|
|
|
$ParameterQuery.= $strRequest; |
70
|
|
|
$this->concatenationUrlCurl($ParameterQuery); |
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Service resources can be obtained in the form of sort . |
76
|
|
|
* asc ascending |
77
|
|
|
* desc descending |
78
|
|
|
* @param string $whatSort |
79
|
|
|
* @param string $param asc | desc |
80
|
|
|
* @return $this |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
public function orderBy($whatSort, $param = 'asc') |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$ParameterQuery = '$orderby='; |
86
|
|
|
$ParameterQuery.= ucfirst($whatSort); |
87
|
|
|
|
88
|
|
|
if ( empty($param) === false ) { |
89
|
|
|
if ($param != 'desc' && $param != 'asc') { |
90
|
|
|
throw new \Exception('no valid orderby parameters'); |
91
|
|
|
} |
92
|
|
|
$ParameterQuery.= " ".$param; |
93
|
|
|
} |
94
|
|
|
$this->concatenationUrlCurl($ParameterQuery); |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* In bpm'online support the use of parameter $ the skip , |
101
|
|
|
* which allows you to query the service resources , |
102
|
|
|
* skipping the specified number of entries. |
103
|
|
|
* @param $skip |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
|
|
public function skip($skip) |
107
|
|
|
{ |
108
|
|
|
Assert::that($skip, 'You must specify a numeric parameter for the amount of the method')->integer(); |
109
|
|
|
$ParameterQuery = '$skip='.$skip; |
110
|
|
|
$this->concatenationUrlCurl($ParameterQuery); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Restrictions in the sample query |
117
|
|
|
* If you want the request to return more than 40 records at a time, it can be implemented using the parameter $ top |
118
|
|
|
* @param $amountMax |
119
|
|
|
* @return $this |
120
|
|
|
*/ |
121
|
|
|
public function amount($amountMax = null) |
122
|
|
|
{ |
123
|
|
|
Assert::that($amountMax,'You must specify a numeric parameter for the amount of the method')->integer(); |
124
|
|
|
$ParameterQuery = '$top='.$amountMax; |
125
|
|
|
$this->concatenationUrlCurl($ParameterQuery); |
126
|
|
|
return $this; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return void |
131
|
|
|
*/ |
132
|
|
View Code Duplication |
private function query() |
|
|
|
|
133
|
|
|
{ |
134
|
|
|
$parameters = str_replace(' ', '%20', $this->url); |
135
|
|
|
$url = $this->kernel->getCollection() . $parameters; |
136
|
|
|
$client = app()->make(ClientInterface::class); |
137
|
|
|
$urlHome = config($this->kernel->getPrefixConfig() . '.UrlHome'); |
138
|
|
|
|
139
|
|
|
try { |
140
|
|
|
$response = $client->request($this->HTTP_TYPE, $urlHome . $url, |
141
|
|
|
[ |
142
|
|
|
'headers' => [ |
143
|
|
|
'HTTP/1.0', |
144
|
|
|
'Accept' => $this->kernel->getHandler()->getContentType(), |
145
|
|
|
'Content-type' => $this->kernel->getHandler()->getAccept() |
146
|
|
|
], |
147
|
|
|
'curl' => [ |
148
|
|
|
CURLOPT_COOKIEFILE => app()->make(Authentication::class)->getPathCookieFile() |
149
|
|
|
] |
150
|
|
|
]); |
151
|
|
|
|
152
|
|
|
$body = $response->getBody(); |
153
|
|
|
|
154
|
|
|
$this->kernel->getHandler()->parse($body->getContents()); |
155
|
|
|
} catch (ClientException $e) { |
156
|
|
|
|
157
|
|
|
if ($e->getResponse()->getStatusCode() == 401 && $e->getResponse()->getReasonPhrase() == 'Unauthorized') |
158
|
|
|
{ |
159
|
|
|
$this->kernel->authentication(); |
160
|
|
|
return $this->query(); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
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.