Completed
Push — fix/backendModuleUrl ( a3b276 )
by Tomas Norre
17:39
created

ProcessListView::getCliPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace AOE\Crawler\Backend\View;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2017 AOE GmbH <[email protected]>
8
 *
9
 *  All rights reserved
10
 *
11
 *  This script is part of the TYPO3 project. The TYPO3 project is
12
 *  free software; you can redistribute it and/or modify
13
 *  it under the terms of the GNU General Public License as published by
14
 *  the Free Software Foundation; either version 3 of the License, or
15
 *  (at your option) any later version.
16
 *
17
 *  The GNU General Public License can be found at
18
 *  http://www.gnu.org/copyleft/gpl.html.
19
 *
20
 *  This script is distributed in the hope that it will be useful,
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 *  GNU General Public License for more details.
24
 *
25
 *  This copyright notice MUST APPEAR in all copies of the script!
26
 ***************************************************************/
27
28
use AOE\Crawler\Domain\Model\ProcessCollection;
29
use AOE\Crawler\Utility\BackendModuleUtility;
30
use AOE\Crawler\Utility\ButtonUtility;
31
use TYPO3\CMS\Backend\Utility\BackendUtility;
32
use TYPO3\CMS\Core\Utility\GeneralUtility;
33
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
34
35
/**
36
 * Class ProcessListView
37
 *
38
 * @package AOE\Crawler\Backend\View
39
 */
40
class ProcessListView
41
{
42
43
    /**
44
     * @var string template path
45
     */
46
    protected $template = 'EXT:crawler/template/process/list.php';
47
48
    /**
49
     * @var string icon path
50
     */
51
    protected $iconPath;
52
53
    /**
54
     * @var string Holds the path to start a cli process via command line
55
     */
56
    protected $cliPath;
57
58
    /**
59
     * @var int Holds the total number of items pending in the queue to be processed
60
     */
61
    protected $totalItemCount;
62
63
    /**
64
     * @var boolean Holds the enable state of the crawler
65
     */
66
    protected $isCrawlerEnabled;
67
68
    /**
69
     * @var int Holds the number of active processes
70
     */
71
    protected $activeProcessCount;
72
73
    /**
74
     * @var int Holds the number of maximum active processes
75
     */
76
    protected $maxActiveProcessCount;
77
78
    /**
79
     * @var string Holds the mode state, can be simple or detail
80
     */
81
    protected $mode;
82
83
    /**
84
     * @var int Holds the current page id
85
     */
86
    protected $pageId;
87
88
    /**
89
     * @var int $totalItemCount number of total item
90
     */
91
    protected $totalUnprocessedItemCount;
92
93
    /**
94
     * @var int Holds the number of assigned unprocessed items
95
     */
96
    protected $assignedUnprocessedItemCount;
97
98
    /**
99
     * @return int
100
     */
101
    public function getAssignedUnprocessedItemCount()
102
    {
103
        return $this->assignedUnprocessedItemCount;
104
    }
105
106
    /**
107
     * @return int
108
     */
109
    public function getTotalUnprocessedItemCount()
110
    {
111
        return $this->totalUnprocessedItemCount;
112
    }
113
114
    /**
115
     * @param int $assignedUnprocessedItemCount
116
     */
117
    public function setAssignedUnprocessedItemCount($assignedUnprocessedItemCount)
118
    {
119
        $this->assignedUnprocessedItemCount = $assignedUnprocessedItemCount;
120
    }
121
122
    /**
123
     * @param int $totalUnprocessedItemCount
124
     */
125
    public function setTotalUnprocessedItemCount($totalUnprocessedItemCount)
126
    {
127
        $this->totalUnprocessedItemCount = $totalUnprocessedItemCount;
128
    }
129
130
    /**
131
     * Set the page id
132
     *
133
     * @param int $pageId page id
134
     */
135
    public function setPageId($pageId)
136
    {
137
        $this->pageId = $pageId;
138
    }
139
140
    /**
141
     * Get the page id
142
     *
143
     * @return int page id
144
     */
145
    public function getPageId()
146
    {
147
        return $this->pageId;
148
    }
149
150
    /**
151
     * @return string
152
     */
153
    public function getMode()
154
    {
155
        return $this->mode;
156
    }
157
158
    /**
159
     * @param string $mode
160
     */
161
    public function setMode($mode)
162
    {
163
        $this->mode = $mode;
164
    }
165
166
    /**
167
     * @return int
168
     */
169
    public function getMaxActiveProcessCount()
170
    {
171
        return $this->maxActiveProcessCount;
172
    }
173
174
    /**
175
     * @param int $maxActiveProcessCount
176
     */
177
    public function setMaxActiveProcessCount($maxActiveProcessCount)
178
    {
179
        $this->maxActiveProcessCount = $maxActiveProcessCount;
180
    }
181
182
    /**
183
     * @return int
184
     */
185
    public function getActiveProcessCount()
186
    {
187
        return $this->activeProcessCount;
188
    }
189
190
    /**
191
     * @param int $activeProcessCount
192
     */
193
    public function setActiveProcessCount($activeProcessCount)
194
    {
195
        $this->activeProcessCount = $activeProcessCount;
196
    }
197
198
    /**
199
     * @return boolean
200
     */
201
    public function getIsCrawlerEnabled()
202
    {
203
        return $this->isCrawlerEnabled;
204
    }
205
206
    /**
207
     * @param boolean $isCrawlerEnabled
208
     */
209
    public function setIsCrawlerEnabled($isCrawlerEnabled)
210
    {
211
        $this->isCrawlerEnabled = $isCrawlerEnabled;
212
    }
213
214
    /**
215
     * Returns the path to start a cli process from the shell
216
     *
217
     * @return string
218
     */
219
    public function getCliPath()
220
    {
221
        return $this->cliPath;
222
    }
223
224
    /**
225
     * @param string $cliPath
226
     */
227
    public function setCliPath($cliPath)
228
    {
229
        $this->cliPath = $cliPath;
230
    }
231
232
    /**
233
     * @return int
234
     */
235
    public function getTotalItemCount()
236
    {
237
        return $this->totalItemCount;
238
    }
239
240
    /**
241
     * @param int $totalItemCount
242
     */
243
    public function setTotalItemCount($totalItemCount)
244
    {
245
        $this->totalItemCount = $totalItemCount;
246
    }
247
248
    /**
249
     * Method to set the path to the icon from outside
250
     *
251
     * @param string $iconPath
252
     */
253
    public function setIconPath($iconPath)
254
    {
255
        $this->iconPath = $iconPath;
256
    }
257
258
    /**
259
     * Method to read the configured icon path
260
     *
261
     * @return string
262
     */
263
    protected function getIconPath()
264
    {
265
        return $this->iconPath;
266
    }
267
268
    /**
269
     * Method to set a collection of process objects to be displayed in
270
     * the list view.
271
     *
272
     * @param ProcessCollection $processCollection
273
     */
274
    public function setProcessCollection($processCollection)
275
    {
276
        $this->processCollection = $processCollection;
0 ignored issues
show
Bug introduced by
The property processCollection 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...
277
    }
278
279
    /**
280
     * Returns a collection of processObjects.
281
     *
282
     * @return ProcessCollection
283
     */
284
    protected function getProcessCollection()
285
    {
286
        return $this->processCollection;
287
    }
288
289
    /**
290
     * Formats a timestamp as date
291
     *
292
     * @param int $timestamp
293
     *
294
     * @return string
295
     */
296
    protected function asDate($timestamp)
297
    {
298
        if ($timestamp > 0) {
299
            return date($this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:time.detailed'), $timestamp);
300
        } else {
301
            return '';
302
        }
303
    }
304
305
    /**
306
     * Converts seconds into minutes
307
     *
308
     * @param int $seconds
309
     *
310
     * @return float
311
     */
312
    protected function asMinutes($seconds)
313
    {
314
        return round($seconds / 60);
315
    }
316
317
    /**
318
     * Returns the state icon for the current job
319
     *
320
     * @param string $state
321
     * @return string icon
322
     */
323
    protected function getIconForState($state)
324
    {
325
        switch ($state) {
326
            case 'running':
327
                $icon = 'bullet_orange';
328
                $title = $this->getLLLabel('LLL:EXT:crawler/modfunc1/locallang.xml:labels.process.running');
329
                break;
330
            case 'completed':
331
                $icon = 'bullet_green';
332
                $title = $this->getLLLabel('LLL:EXT:crawler/modfunc1/locallang.xml:labels.process.success');
333
                break;
334
            case 'cancelled':
335
                $icon = 'bullet_red';
336
                $title = $this->getLLLabel('LLL:EXT:crawler/modfunc1/locallang.xml:labels.process.cancelled');
337
                break;
338
        }
339
340
        return $this->getIcon($icon, $title);
0 ignored issues
show
Bug introduced by
The variable $icon does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
341
    }
342
343
    /**
344
     * Returns an imagetag for an icon
345
     *
346
     * @param string $icon
347
     * @param string $title
348
     *
349
     * @return string html tag for icon
350
     */
351
    protected function getIcon($icon, $title = '')
352
    {
353
        if (!empty($title)) {
354
            $title = ' title="' . $title . '"';
355
        }
356
        return '<img src="' . $this->getIconPath() . $icon . '.png" ' . $title . ' />';
357
    }
358
359
    /**
360
     * Returns a tag for the refresh icon
361
     *
362
     * @return string
363
     */
364
    protected function getRefreshLink()
365
    {
366
        return ButtonUtility::getLinkButton(
367
            'actions-refresh',
368
            $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.refresh'),
369
            'window.location=\'' . BackendModuleUtility::getInfoModuleUrl(['SET[\'crawleraction\']' => 'crawleraction', 'id' => $this->pageId]) . '\';'
370
        );
371
    }
372
373
    /**
374
     * Returns a link for the panel to enable or disable the crawler
375
     *
376
     * @return string
377
     */
378
    protected function getEnableDisableLink()
379
    {
380
        if ($this->getIsCrawlerEnabled()) {
381
            // TODO: Icon Should be bigger + Perhaps better icon
382
            return ButtonUtility::getLinkButton(
383
                'tx-crawler-stop',
384
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.disablecrawling'),
385
                'window.location=\'' . BackendModuleUtility::getInfoModuleUrl(['action' => 'stopCrawling']) . '\';'
386
            );
387
        } else {
388
            // TODO: Icon Should be bigger
389
            return ButtonUtility::getLinkButton(
390
                'tx-crawler-start',
391
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.enablecrawling'),
392
                'window.location=\'' . BackendModuleUtility::getInfoModuleUrl(['action' => 'resumeCrawling']) . '\';'
393
            );
394
        }
395
    }
396
397
    /**
398
     * Get mode link
399
     *
400
     * @param void
401
     *
402
     * @return string a-tag
403
     */
404
    protected function getModeLink()
405
    {
406
        if ($this->getMode() == 'detail') {
407
            return ButtonUtility::getLinkButton(
408
                'actions-document-view',
409
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.show.running'),
410
                'window.location=\'' . BackendModuleUtility::getInfoModuleUrl(['SET[\'processListMode\']' => 'simple']) . '\';'
411
            );
412
        } elseif ($this->getMode() == 'simple') {
413
            return ButtonUtility::getLinkButton(
414
                'actions-document-view',
415
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.show.all'),
416
                'window.location=\'' . BackendModuleUtility::getInfoModuleUrl(['SET[\'processListMode\']' => 'detail']) . '\';'
417
            );
418
        }
419
    }
420
421
    /**
422
     * Get add link
423
     *
424
     * @param void
425
     *
426
     * @return string a-tag
427
     */
428
    protected function getAddLink()
429
    {
430
        if ($this->getActiveProcessCount() < $this->getMaxActiveProcessCount() && $this->getIsCrawlerEnabled()) {
431
            return ButtonUtility::getLinkButton(
432
                'actions-add',
433
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.process.add'),
434
                'window.location=\'' . BackendModuleUtility::getInfoModuleUrl(['action' => 'addProcess']) . '\';'
435
            );
436
        } else {
437
            return '';
438
        }
439
    }
440
441
    /**
442
     * Method to render the view.
443
     *
444
     * @return string html content
445
     */
446
    public function render()
447
    {
448
        ob_start();
449
        $this->template = GeneralUtility::getFileAbsFileName($this->template);
450
        include($this->template);
451
        $content = ob_get_contents();
452
        ob_end_clean();
453
454
        return $content;
455
    }
456
457
    /**
458
     * retrieve locallanglabel from environment
459
     * just a wrapper should be done in a cleaner way
460
     * later on
461
     *
462
     * @param string $label
463
     *
464
     * @return string
465
     */
466
    protected function getLLLabel($label)
467
    {
468
        return $GLOBALS['LANG']->sL($label);
469
    }
470
}
471