Completed
Push — master ( baf356...7843c7 )
by Alex
07:30 queued 03:24
created

BatchProcessor   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 85
rs 10
c 1
b 1
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getResponse() 0 11 2
A getService() 0 3 1
B handleBatch() 0 23 5
A getBoundary() 0 3 1
A getParser() 0 6 2
A __construct() 0 4 1
A getRequest() 0 3 1
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));
0 ignored issues
show
Bug introduced by
The call to assert() has too few arguments starting with description. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        /** @scrutinizer ignore-call */ 
35
        assert('multipart/mixed;' === substr($contentType, 0, 16));

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.

Loading history...
35
        $this->data = trim($this->getRequest()->getData());
0 ignored issues
show
Bug introduced by
It seems like $this->getRequest()->getData() can also be of type array; however, parameter $str of trim() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

35
        $this->data = trim(/** @scrutinizer ignore-type */ $this->getRequest()->getData());
Loading history...
36
        $this->data = preg_replace('~\r\n?~', "\n", $this->data);
37
        $this->batchBoundary = substr($contentType, 26);
38
39
        $matches = explode('--' . $this->batchBoundary, $this->data);
0 ignored issues
show
Bug introduced by
Are you sure $this->batchBoundary of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
        $matches = explode('--' . /** @scrutinizer ignore-type */ $this->batchBoundary, $this->data);
Loading history...
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