Passed
Push — master ( 84f46e...e41bcf )
by Dan Michael O.
02:23
created

Report::readColumnHeaders()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
1
<?php
2
3
namespace Scriptotek\Alma\Analytics;
4
5
use Danmichaelo\QuiteSimpleXMLElement\QuiteSimpleXMLElement;
6
use Scriptotek\Alma\Client;
7
8
/**
9
 * @property \Generator|Row[] rows
10
 */
11
class Report
12
{
13
    public $path;
14
    public $chunkSize = 1000;
15
16
    /** @var Client */
17
    protected $client;
18
19
    protected $headers = [];
20
21
    public function __construct(Client $client = null, $path = null, $headers = [], $filter = null)
22
    {
23
        $this->path = $path;
24
        $this->client = $client;
25
26
        $this->headers = $headers;
27
        $this->filter = $filter;
0 ignored issues
show
Bug introduced by
The property filter does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
28
        // $this->rows = new Rows($this->path, $this->client);
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
    }
30
31
    public function __get($key)
32
    {
33
        if ($key == 'rows') {
34
            return $this->getRows();
35
        }
36
        if ($key == 'headers') {
37
            return $this->headers;
38
        }
39
    }
40
41
    protected function fetchRows($resumptionToken = null)
42
    {
43
        $results = $this->client->getXML('/analytics/reports', [
44
            'path'   => $this->path,
45
            'limit'  => $this->chunkSize,
46
            'token'  => $resumptionToken,
47
            'filter' => $this->filter ? str_replace(['\''], ['&apos;'], $this->filter) : null,
48
        ]);
49
        $results->registerXPathNamespaces([
50
            'rowset' => 'urn:schemas-microsoft-com:xml-analysis:rowset',
51
            'xsd'    => 'http://www.w3.org/2001/XMLSchema',
52
        ]);
53
54
        $this->readColumnHeaders($results);
55
56
        return $results;
57
    }
58
59
    /**
60
     * Read column headers from response, and check that we got the right number of columns back.
61
     *
62
     * @param QuiteSimpleXMLElement $results
63
     */
64
    protected function readColumnHeaders(QuiteSimpleXMLElement $results)
65
    {
66
        $headers = array_map(function (QuiteSimpleXMLElement $node) {
67
            return $node->attr('name');
68
        }, $results->all('//xsd:complexType[@name="Row"]/xsd:sequence/xsd:element[position()>1]'));
69
70
        if (!count($headers)) {
71
            // No column headers included in this response. They're only
72
            // included in the first response, so that's probably fine.
73
            return;
74
        }
75
76
        if (!count($this->headers)) {
77
            $this->headers = $headers;
78
            return;
79
        }
80
81
        if (count($headers) != count($this->headers)) {
82
            throw new \RuntimeException(sprintf('The number of returned columns (%d) does not match the number of assigned headers (%d).',
83
                count($headers), count($this->headers)));
84
        }
85
    }
86
87
    /**
88
     * @return \Generator|Row[]
89
     */
90
    public function getRows()
91
    {
92
        $isFinished = false;
93
        $resumptionToken = null;
94
95
        while (!$isFinished) {
96
            $results = $this->fetchRows($resumptionToken);
97
98
            foreach ($results->all('//rowset:Row') as $row) {
99
                yield new Row($row, $this->headers);
100
            }
101
102
            $resumptionToken = $results->text('/report/QueryResult/ResumptionToken') ?: $resumptionToken;
103
            $isFinished = ($results->text('/report/QueryResult/IsFinished') == 'true');
104
        }
105
    }
106
}
107