Completed
Push — master ( 01e874...cfe99f )
by Dan Michael O.
02:16
created

Report   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 87
rs 10
wmc 14
lcom 1
cbo 3

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A __get() 0 9 3
A fetchRows() 0 17 2
A readResponseHeaders() 0 15 4
A getRows() 0 16 4
1
<?php
2
3
namespace Scriptotek\Alma\Analytics;
4
5
use Scriptotek\Alma\Client;
6
7
/**
8
 * @property \Generator|Row[] rows
9
 */
10
class Report
11
{
12
    public $path;
13
    public $chunkSize = 1000;
14
15
    /** @var Client */
16
    protected $client;
17
18
    protected $headers = [];
19
20
    public function __construct(Client $client = null, $path = null, $headers = [], $filter = null)
21
    {
22
        $this->path = $path;
23
        $this->client = $client;
24
25
        $this->headers = $headers;
26
        $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...
27
        // $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...
28
    }
29
30
    public function __get($key)
31
    {
32
        if ($key == 'rows') {
33
            return $this->getRows();
34
        }
35
        if ($key == 'headers') {
36
            return $this->headers;
37
        }
38
    }
39
40
    protected function fetchRows($resumptionToken = null)
41
    {
42
        $results = $this->client->getXML('/analytics/reports', [
43
            'path'   => $this->path,
44
            'limit'  => $this->chunkSize,
45
            'token'  => $resumptionToken,
46
            'filter' => $this->filter ? str_replace(['\''], ['&apos;'], $this->filter) : null,
47
        ]);
48
        $results->registerXPathNamespaces([
49
            'rowset' => 'urn:schemas-microsoft-com:xml-analysis:rowset',
50
            'xsd'    => 'http://www.w3.org/2001/XMLSchema',
51
        ]);
52
53
        $this->readResponseHeaders($results);
54
55
        return $results;
56
    }
57
58
    /**
59
     * @param $results
60
     */
61
    protected function readResponseHeaders($results)
62
    {
63
        $headers = array_map(function ($node) {
64
            return $node->attr('name');
65
        }, $results->all('//xsd:complexType[@name="Row"]/xsd:sequence/xsd:element[position()>1]'));
66
67
        if (count($headers)) {
68
            if (!count($this->headers)) {
69
                $this->headers = $headers;
70
            } elseif (count($headers) != count($this->headers)) {
71
                throw new \RuntimeException(sprintf('Number of returned columns (%d) does not match number of assigned headers (%d).',
72
                    count($headers), count($this->headers)));
73
            }
74
        }
75
    }
76
77
    /**
78
     * @return \Generator|Row[]
79
     */
80
    public function getRows()
81
    {
82
        $isFinished = false;
83
        $resumptionToken = null;
84
85
        while (!$isFinished) {
86
            $results = $this->fetchRows($resumptionToken);
87
88
            foreach ($results->all('//rowset:Row') as $row) {
89
                yield new Row($row, $this->headers);
90
            }
91
92
            $resumptionToken = $results->text('/report/QueryResult/ResumptionToken') ?: $resumptionToken;
93
            $isFinished = ($results->text('/report/QueryResult/IsFinished') == 'true');
94
        }
95
    }
96
}
97