1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LegalThings\DataEnricher\Processor; |
4
|
|
|
|
5
|
|
|
use LegalThings\DataEnricher; |
6
|
|
|
use LegalThings\DataEnricher\Node; |
7
|
|
|
use LegalThings\DataEnricher\Processor; |
8
|
|
|
use GuzzleHttp\Client; |
9
|
|
|
use GuzzleHttp\Promise; |
10
|
|
|
use GuzzleHttp\Psr7\Response; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Load an external source |
14
|
|
|
*/ |
15
|
|
|
class Http implements Processor |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Property key which should trigger the processor |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $property; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class constructor |
25
|
|
|
* |
26
|
|
|
* @param DataEnricher $invoker |
27
|
|
|
* @param string $property Property key with the processing instruction |
28
|
|
|
*/ |
29
|
|
|
public function __construct(DataEnricher $invoker, $property) |
30
|
|
|
{ |
31
|
|
|
$this->property = $property; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get the property key that holds the processing instruction |
36
|
|
|
* |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
|
|
public function getProperty() |
40
|
|
|
{ |
41
|
|
|
return $this->property; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Apply processing to nodes |
46
|
|
|
* |
47
|
|
|
* @param Node[] $nodes |
48
|
|
|
*/ |
49
|
|
|
public function applyTo(array $nodes) |
50
|
|
|
{ |
51
|
|
|
$promises = $this->request($nodes); |
52
|
|
|
if (empty($promises)) { |
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->applyResults($promises); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Do async requests for each node |
61
|
|
|
* |
62
|
|
|
* @param Node[] $nodes |
63
|
|
|
* @return \SplObjectStorage|Promise\PromiseInterface[] |
64
|
|
|
*/ |
65
|
|
|
protected function request($nodes) |
66
|
|
|
{ |
67
|
|
|
$client = new Client(); |
|
|
|
|
68
|
|
|
$promises = new \SplObjectStorage(); |
69
|
|
|
|
70
|
|
|
foreach ($nodes as $node) { |
71
|
|
|
if ($node->hasInstruction($this)) { |
72
|
|
|
$url = $node->getInstruction($this); |
|
|
|
|
73
|
|
|
$promises[$node] = $client->getAsync($url); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $promises; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Apply results to nodes |
82
|
|
|
* |
83
|
|
|
* @param \SplObjectStorage|Promise\PromiseInterface[] $promises |
84
|
|
|
*/ |
85
|
|
|
protected function applyResults($promises) |
86
|
|
|
{ |
87
|
|
|
$results = new \SplObjectStorage(); |
88
|
|
|
foreach ($promises as $node => $promise) { |
89
|
|
|
$results[$node] = $promise->wait(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
foreach ($results as $node => $response) { |
93
|
|
|
$result = null; |
94
|
|
|
|
95
|
|
|
if ($this->hasExpectedResponse($response)) { |
96
|
|
|
$result = json_decode($response->body()); |
97
|
|
|
|
98
|
|
|
if (!$result) { |
99
|
|
|
$url = $node->getInstruction($this); |
|
|
|
|
100
|
|
|
trigger_error("Failed to fetch '$url': Corrupt JSON response", E_USER_WARNING); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$node->setResult($result); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Check if we got an expected response |
110
|
|
|
* |
111
|
|
|
* @param Response $response |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
protected function hasExpectedResponse(Response $response) |
115
|
|
|
{ |
116
|
|
|
$status = $response->getStatusCode(); |
|
|
|
|
117
|
|
|
$contentType = preg_replace('/\s*;.*$/', '', $response->getHeader('content-type')); |
118
|
|
|
|
119
|
|
|
if ($status >= 300 || !in_array($contentType, ['application/json', 'text/plain'])) { |
120
|
|
|
$url = $node->getInstruction($this); |
|
|
|
|
121
|
|
|
|
122
|
|
|
if ($contentType === 'text/plain') { |
123
|
|
|
$message = $response->getBody(); |
124
|
|
|
} else { |
125
|
|
|
$message = "Server responded with a $status status and $contentType"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
trigger_error("Failed to fetch '$url': $message", E_USER_WARNING); |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.