ReportBuilder::status()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the ReportBundle package
5
 *
6
 * (c) symball <http://simonball.me>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace Symball\ReportBundle\Service;
13
14
use Symball\ReportBundle\Service\ReportPattern;
15
use Symball\ReportBundle\Service\ReportStyle;
16
use Symball\ReportBundle\Service\Meta;
17
use Symball\ReportBundle\Interfaces\NavInterface;
18
use Symball\ReportBundle\Interfaces\QueryInterface;
19
20
/**
21
 * The report builder service acts as a facade bringing together the various
22
 * functionality that will be present when creating reports and some shortcut
23
 * functions
24
 *
25
 * @author Simon Ball <simonball at simonball dot me>
26
 */
27
class ReportBuilder
28
{
29
30
    private $meta;
31
    private $excelService;
32
    private $sheet;
33
    private $nav;
34
    private $query;
35
    private $status;
36
    private $style;
37
38
    /**
39
     * @param object       $excelService
40
     * @param Meta         $meta
41
     * @param NavInterface $nav
42
     */
43
    public function __construct($excelService, Meta $meta, NavInterface $nav, ReportStyle $style)
44
    {
45
        $this->excelService = $excelService;
46
        $this->meta = $meta;
47
        $this->nav = $nav;
48
        $this->style = $style;
49
    }
50
51
    /**
52
     * Set the database query handler
53
     *
54
     * @param QueryInterface $query
55
     * @return $this
56
     */
57
    public function setQuery(QueryInterface $query)
58
    {
59
        $this->query = $query;
60
61
        return $this;
62
    }
63
64
    /**
65
     * Set the navigation handler
66
     *
67
     * @param NavInterface $nav
68
     * @return $this
69
     */
70
    public function setNav(NavInterface $nav)
71
    {
72
        $this->nav = $nav;
73
74
        return $this;
75
    }
76
77
    /**
78
     * Fill in a literal value in the cell at current nav pointer
79
     *
80
     * @param mixed $value
81
     */
82
    public function write($value)
83
    {
84
        $this->sheet->setCellValue((string) $this->nav, $value);
85
    }
86
87
    /**
88
     * Fill in a cell with a custom or ready made formula at current nav pointer
89
     *
90
     * @param type $value
91
     * @param type $options
92
     */
93
    public function formula($value, $options)
94
    {
95
96
        // TODO Refactor in to a service which detects shortcuts or paste raw
97
        switch ($value) {
98
            case 'sum_up_data':
99
                $start = $this->nav('current', 'initial');
0 ignored issues
show
Unused Code introduced by
The call to ReportBuilder::nav() has too many arguments starting with 'current'.

This check compares calls to functions or methods with their respective definitions. If the call has more 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
100
                $end = $this->nav('current', ($this->nav->row() + 1));
0 ignored issues
show
Unused Code introduced by
The call to ReportBuilder::nav() has too many arguments starting with 'current'.

This check compares calls to functions or methods with their respective definitions. If the call has more 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
101
                $formula = '=SUM(' . $start . ':' . $end . ')';
102
                break;
103
104
            default:
105
                $formula = $value;
106
                break;
107
        }
108
        $this->write($formula, $options);
0 ignored issues
show
Unused Code introduced by
The call to ReportBuilder::write() has too many arguments starting with $options.

This check compares calls to functions or methods with their respective definitions. If the call has more 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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
109
    }
110
111
    /**
112
     * Set or return the current run status of the report builder
113
     *
114
     * @param type $newStatus
115
     * @return $this
116
     */
117
    public function status($newStatus = false)
118
    {
119
        if ($newStatus) {
120
            $this->status = $newStatus;
121
122
            return $this;
123
        } else {
124
125
            return $this->status;
126
        }
127
    }
128
129
    /**
130
     * Facade function for creating a new spreadsheet within the current excel
131
     * If it is not the first, the query object will also be reset back to
132
     * initial status
133
     *
134
     * @param type $title
135
     * @return $this
136
     */
137
    public function createSheet($title = '')
138
    {
139
        $this->sheet = $this->excelService->newSheet($title);
140
        if ($this->status() == 'running') {
141
            $this->query->reset();
142
        }
143
144
        $this->status('initial');
0 ignored issues
show
Documentation introduced by
'initial' is of type string, but the function expects a false|object<Symball\ReportBundle\Service\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
145
146
        return $this;
147
    }
148
149
    /**
150
     * Used to prepare the service for an iteration of writing. This should be
151
     * called prior to any data manipulation as it will reset the data tracking
152
     * array.
153
     *
154
     * @return boolean outcome of operation
155
     */
156
    public function newSet()
157
    {
158
        $this->meta->clear();
159
160
        // Check whether the report is using query and whether any sets left
161
        if ($this->query && !$this->query->tick()) {
162
            return false;
163
        }
164
165
        // Is there cause to move the poingter
166
        if ($this->status() == 'running') {
167
            $this->nav->movePointerAlong($this->meta->columnCount());
168
        } else {
169
            $this->status('running');
0 ignored issues
show
Documentation introduced by
'running' is of type string, but the function expects a false|object<Symball\ReportBundle\Service\type>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
170
        }
171
172
        return true;
173
    }
174
175
    /**
176
     * Facade function for saving the spreadsheet
177
     *
178
     * @param string $fileName
179
     * @param string $path
180
     * @param string $outputFormat
181
     * @return File
182
     */
183
    public function save($fileName, $path = '', $outputFormat = '')
184
    {
185
        return $this->excelService->save($fileName, $path, $outputFormat);
186
    }
187
188
    /**
189
     * Facade function for applying style to the spreadsheet. Will also merge
190
     * options with user overrides before calling
191
     *
192
     * @param string $reference
193
     * @param string $coordString
194
     * @param array  $options
195
     */
196
    public function style($reference, $coordString, $options = array())
197
    {
198
        // Combine any options from the meta class with function call
199
        $options = array_merge($this->meta->getOptions(), $options);
200
        $this->style->run($reference, $this, $coordString, $options);
201
    }
202
    /**
203
     * Return the navigation service object
204
     *
205
     * @return NavInterface
206
     */
207
    public function nav()
208
    {
209
        return $this->nav;
210
    }
211
212
    /**
213
     * Return the meta data broker service object
214
     *
215
     * @return Meta
216
     */
217
    public function meta()
218
    {
219
        return $this->meta;
220
    }
221
222
    /**
223
     * Return the current sheet object. This is for low level control
224
     *
225
     * @return object
226
     */
227
    public function sheet()
228
    {
229
        return $this->sheet;
230
    }
231
232
    /**
233
     * Return the Excel service directly
234
     *
235
     * @return Excel
236
     */
237
    public function excel()
238
    {
239
        return $this->excelService;
240
    }
241
242
    /**
243
     * Return the data handling query service
244
     *
245
     * @return QueryInterface
246
     */
247
    public function query()
248
    {
249
        return $this->query;
250
    }
251
}
252