1
|
|
|
<?php |
2
|
|
|
namespace POData\BatchProcessor; |
3
|
|
|
|
4
|
|
|
use POData\BaseService; |
5
|
|
|
use POData\UriProcessor\RequestDescription; |
6
|
|
|
|
7
|
|
|
class BatchProcessor |
8
|
|
|
{ |
9
|
|
|
protected $service; |
10
|
|
|
protected $data; |
11
|
|
|
protected $batchBoundary = ''; |
12
|
|
|
protected $request; |
13
|
|
|
protected $changeSetProcessors = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param BaseService $service |
17
|
|
|
* @param RequestDescription $request |
18
|
|
|
*/ |
19
|
|
|
public function __construct(BaseService $service, RequestDescription $request) |
20
|
|
|
{ |
21
|
|
|
$this->service = $service; |
22
|
|
|
$this->request = $request; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getBoundary() |
26
|
|
|
{ |
27
|
|
|
return $this->batchBoundary; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function handleBatch() |
31
|
|
|
{ |
32
|
|
|
$host = $this->getService()->getHost(); |
33
|
|
|
$contentType = $host->getRequestContentType(); |
34
|
|
|
assert('multipart/mixed;' === substr($contentType, 0, 16)); |
|
|
|
|
35
|
|
|
$this->data = trim($this->getRequest()->getData()); |
|
|
|
|
36
|
|
|
$this->data = preg_replace('~\r\n?~', "\n", $this->data); |
37
|
|
|
$this->batchBoundary = substr($contentType, 26); |
38
|
|
|
|
39
|
|
|
$matches = explode('--' . $this->batchBoundary, $this->data); |
|
|
|
|
40
|
|
|
foreach ($matches as $match) { |
41
|
|
|
$match = trim($match); |
42
|
|
|
if ('' === $match || '--' === $match) { |
43
|
|
|
continue; |
44
|
|
|
} |
45
|
|
|
$header = explode("\n\n", $match)[0]; |
46
|
|
|
$isChangeset = false === strpos($header, 'Content-Type: application/http'); |
47
|
|
|
$this->changeSetProcessors[] = $this->getParser($this->getService(), $match, $isChangeset); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
foreach ($this->changeSetProcessors as $csp) { |
51
|
|
|
$csp->handleData(); |
52
|
|
|
$csp->process(); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getResponse() |
57
|
|
|
{ |
58
|
|
|
$response = ''; |
59
|
|
|
$splitter = '--' . $this->batchBoundary . "\r\n"; |
60
|
|
|
$raw = $this->changeSetProcessors; |
61
|
|
|
foreach ($raw as $contentID => &$workingObject) { |
62
|
|
|
$response .= $splitter; |
63
|
|
|
$response .= $workingObject->getResponse() . "\r\n"; |
64
|
|
|
} |
65
|
|
|
$response .= trim($splitter) . "--\r\n"; |
66
|
|
|
return $response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
protected function getParser(BaseService $service, $match, $isChangeset) |
71
|
|
|
{ |
72
|
|
|
if ($isChangeset) { |
73
|
|
|
return new ChangeSetParser($service, $match); |
74
|
|
|
} |
75
|
|
|
return new QueryParser($service, $match); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return BaseService |
80
|
|
|
*/ |
81
|
|
|
public function getService() |
82
|
|
|
{ |
83
|
|
|
return $this->service; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return RequestDescription |
88
|
|
|
*/ |
89
|
|
|
public function getRequest() |
90
|
|
|
{ |
91
|
|
|
return $this->request; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.