Completed
Push — master ( 1b04e6...82e336 )
by Tomas Norre
08:07
created

Classes/Backend/View/ProcessListView.php (3 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\ButtonUtility;
30
use TYPO3\CMS\Backend\Utility\BackendUtility;
31
use TYPO3\CMS\Core\Utility\GeneralUtility;
32
33
/**
34
 * Class ProcessListView
35
 *
36
 * @package AOE\Crawler\Backend\View
37
 */
38
class ProcessListView
39
{
40
41
    /**
42
     * @var string template path
43
     */
44
    protected $template = 'EXT:crawler/template/process/list.php';
45
46
    /**
47
     * @var string icon path
48
     */
49
    protected $iconPath;
50
51
    /**
52
     * @var string Holds the path to start a cli process via command line
53
     */
54
    protected $cliPath;
55
56
    /**
57
     * @var int Holds the total number of items pending in the queue to be processed
58
     */
59
    protected $totalItemCount;
60
61
    /**
62
     * @var boolean Holds the enable state of the crawler
63
     */
64
    protected $isCrawlerEnabled;
65
66
    /**
67
     * @var int Holds the number of active processes
68
     */
69
    protected $activeProcessCount;
70
71
    /**
72
     * @var int Holds the number of maximum active processes
73
     */
74
    protected $maxActiveProcessCount;
75
76
    /**
77
     * @var string Holds the mode state, can be simple or detail
78
     */
79
    protected $mode;
80
81
    /**
82
     * @var int Holds the current page id
83
     */
84
    protected $pageId;
85
86
    /**
87
     * @var int $totalItemCount number of total item
88
     */
89
    protected $totalUnprocessedItemCount;
90
91
    /**
92
     * @var int Holds the number of assigned unprocessed items
93
     */
94
    protected $assignedUnprocessedItemCount;
95
96
    /**
97
     * @return int
98
     */
99
    public function getAssignedUnprocessedItemCount()
100
    {
101
        return $this->assignedUnprocessedItemCount;
102
    }
103
104
    /**
105
     * @return int
106
     */
107
    public function getTotalUnprocessedItemCount()
108
    {
109
        return $this->totalUnprocessedItemCount;
110
    }
111
112
    /**
113
     * @param int $assignedUnprocessedItemCount
114
     */
115
    public function setAssignedUnprocessedItemCount($assignedUnprocessedItemCount)
116
    {
117
        $this->assignedUnprocessedItemCount = $assignedUnprocessedItemCount;
118
    }
119
120
    /**
121
     * @param int $totalUnprocessedItemCount
122
     */
123
    public function setTotalUnprocessedItemCount($totalUnprocessedItemCount)
124
    {
125
        $this->totalUnprocessedItemCount = $totalUnprocessedItemCount;
126
    }
127
128
    /**
129
     * Set the page id
130
     *
131
     * @param int $pageId page id
132
     */
133
    public function setPageId($pageId)
134
    {
135
        $this->pageId = $pageId;
136
    }
137
138
    /**
139
     * Get the page id
140
     *
141
     * @return int page id
142
     */
143
    public function getPageId()
144
    {
145
        return $this->pageId;
146
    }
147
148
    /**
149
     * @return string
150
     */
151
    public function getMode()
152
    {
153
        return $this->mode;
154
    }
155
156
    /**
157
     * @param string $mode
158
     */
159
    public function setMode($mode)
160
    {
161
        $this->mode = $mode;
162
    }
163
164
    /**
165
     * @return int
166
     */
167
    public function getMaxActiveProcessCount()
168
    {
169
        return $this->maxActiveProcessCount;
170
    }
171
172
    /**
173
     * @param int $maxActiveProcessCount
174
     */
175
    public function setMaxActiveProcessCount($maxActiveProcessCount)
176
    {
177
        $this->maxActiveProcessCount = $maxActiveProcessCount;
178
    }
179
180
    /**
181
     * @return int
182
     */
183
    public function getActiveProcessCount()
184
    {
185
        return $this->activeProcessCount;
186
    }
187
188
    /**
189
     * @param int $activeProcessCount
190
     */
191
    public function setActiveProcessCount($activeProcessCount)
192
    {
193
        $this->activeProcessCount = $activeProcessCount;
194
    }
195
196
    /**
197
     * @return boolean
198
     */
199
    public function getIsCrawlerEnabled()
200
    {
201
        return $this->isCrawlerEnabled;
202
    }
203
204
    /**
205
     * @param boolean $isCrawlerEnabled
206
     */
207
    public function setIsCrawlerEnabled($isCrawlerEnabled)
208
    {
209
        $this->isCrawlerEnabled = $isCrawlerEnabled;
210
    }
211
212
    /**
213
     * Returns the path to start a cli process from the shell
214
     *
215
     * @return string
216
     */
217
    public function getCliPath()
218
    {
219
        return $this->cliPath;
220
    }
221
222
    /**
223
     * @param string $cliPath
224
     */
225
    public function setCliPath($cliPath)
226
    {
227
        $this->cliPath = $cliPath;
228
    }
229
230
    /**
231
     * @return int
232
     */
233
    public function getTotalItemCount()
234
    {
235
        return $this->totalItemCount;
236
    }
237
238
    /**
239
     * @param int $totalItemCount
240
     */
241
    public function setTotalItemCount($totalItemCount)
242
    {
243
        $this->totalItemCount = $totalItemCount;
244
    }
245
246
    /**
247
     * Method to set the path to the icon from outside
248
     *
249
     * @param string $iconPath
250
     */
251
    public function setIconPath($iconPath)
252
    {
253
        $this->iconPath = $iconPath;
254
    }
255
256
    /**
257
     * Method to read the configured icon path
258
     *
259
     * @return string
260
     */
261
    protected function getIconPath()
262
    {
263
        return $this->iconPath;
264
    }
265
266
    /**
267
     * Method to set a collection of process objects to be displayed in
268
     * the list view.
269
     *
270
     * @param ProcessCollection $processCollection
271
     */
272
    public function setProcessCollection($processCollection)
273
    {
274
        $this->processCollection = $processCollection;
0 ignored issues
show
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...
275
    }
276
277
    /**
278
     * Returns a collection of processObjects.
279
     *
280
     * @return ProcessCollection
281
     */
282
    protected function getProcessCollection()
283
    {
284
        return $this->processCollection;
285
    }
286
287
    /**
288
     * Formats a timestamp as date
289
     *
290
     * @param int $timestamp
291
     *
292
     * @return string
293
     */
294
    protected function asDate($timestamp)
295
    {
296
        if ($timestamp > 0) {
297
            return date($this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:time.detailed'), $timestamp);
298
        } else {
299
            return '';
300
        }
301
    }
302
303
    /**
304
     * Converts seconds into minutes
305
     *
306
     * @param int $seconds
307
     *
308
     * @return float
309
     */
310
    protected function asMinutes($seconds)
311
    {
312
        return round($seconds / 60);
313
    }
314
315
    /**
316
     * Returns the state icon for the current job
317
     *
318
     * @param string $state
319
     * @return string icon
320
     */
321
    protected function getIconForState($state)
322
    {
323
        switch ($state) {
324
            case 'running':
325
                $icon = 'bullet_orange';
326
                $title = $this->getLLLabel('LLL:EXT:crawler/modfunc1/locallang.xml:labels.process.running');
327
                break;
328
            case 'completed':
329
                $icon = 'bullet_green';
330
                $title = $this->getLLLabel('LLL:EXT:crawler/modfunc1/locallang.xml:labels.process.success');
331
                break;
332
            case 'cancelled':
333
                $icon = 'bullet_red';
334
                $title = $this->getLLLabel('LLL:EXT:crawler/modfunc1/locallang.xml:labels.process.cancelled');
335
                break;
336
        }
337
338
        return $this->getIcon($icon, $title);
0 ignored issues
show
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...
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...
339
    }
340
341
    /**
342
     * Returns an imagetag for an icon
343
     *
344
     * @param string $icon
345
     * @param string $title
346
     *
347
     * @return string html tag for icon
348
     */
349
    protected function getIcon($icon, $title = '')
350
    {
351
        if (!empty($title)) {
352
            $title = ' title="' . $title . '"';
353
        }
354
        return '<img src="' . $this->getIconPath() . $icon . '.png" ' . $title . ' />';
355
    }
356
357
    /**
358
     * Returns a tag for the refresh icon
359
     *
360
     * @return string
361
     */
362
    protected function getRefreshLink()
363
    {
364
        return ButtonUtility::getLinkButton(
365
            'actions-refresh',
366
            $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.refresh'),
367
            'window.location=\'' . BackendUtility::getModuleUrl('web_info') . '&SET[crawlaction]=multiprocess&id=' . $this->pageId . '\';'
368
        );
369
    }
370
371
    /**
372
     * Returns a link for the panel to enable or disable the crawler
373
     *
374
     * @return string
375
     */
376
    protected function getEnableDisableLink()
377
    {
378
        if ($this->getIsCrawlerEnabled()) {
379
            // TODO: Icon Should be bigger + Perhaps better icon
380
            return ButtonUtility::getLinkButton(
381
                'tx-crawler-stop',
382
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.disablecrawling'),
383
                'window.location=\'' . BackendUtility::getModuleUrl('web_info') . '&action=stopCrawling\';'
384
            );
385
        } else {
386
            // TODO: Icon Should be bigger
387
            return ButtonUtility::getLinkButton(
388
                'tx-crawler-start',
389
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.enablecrawling'),
390
                'window.location=\'' . BackendUtility::getModuleUrl('web_info') . '&action=resumeCrawling\';'
391
            );
392
        }
393
    }
394
395
    /**
396
     * Get mode link
397
     *
398
     * @param void
399
     *
400
     * @return string a-tag
401
     */
402
    protected function getModeLink()
403
    {
404
        if ($this->getMode() == 'detail') {
405
            return ButtonUtility::getLinkButton(
406
                'actions-document-view',
407
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.show.running'),
408
                'window.location=\'' . BackendUtility::getModuleUrl('web_info') . '&SET[processListMode]=simple\';'
409
            );
410
        } elseif ($this->getMode() == 'simple') {
411
            return ButtonUtility::getLinkButton(
412
                'actions-document-view',
413
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.show.all'),
414
                'window.location=\'' . BackendUtility::getModuleUrl('web_info') . '&SET[processListMode]=detail\';'
415
            );
416
        }
417
    }
418
419
    /**
420
     * Get add link
421
     *
422
     * @param void
423
     *
424
     * @return string a-tag
425
     */
426
    protected function getAddLink()
427
    {
428
        if ($this->getActiveProcessCount() < $this->getMaxActiveProcessCount() && $this->getIsCrawlerEnabled()) {
429
            return ButtonUtility::getLinkButton(
430
                'actions-add',
431
                $this->getLLLabel('LLL:EXT:crawler/Resources/Private/Language/locallang.xml:labels.process.add'),
432
                'window.location=\'' . BackendUtility::getModuleUrl('web_info') . '&action=addProcess\';'
433
            );
434
        } else {
435
            return '';
436
        }
437
    }
438
439
    /**
440
     * Method to render the view.
441
     *
442
     * @return string html content
443
     */
444
    public function render()
445
    {
446
        ob_start();
447
        $this->template = GeneralUtility::getFileAbsFileName($this->template);
448
        include($this->template);
449
        $content = ob_get_contents();
450
        ob_end_clean();
451
452
        return $content;
453
    }
454
455
    /**
456
     * retrieve locallanglabel from environment
457
     * just a wrapper should be done in a cleaner way
458
     * later on
459
     *
460
     * @param string $label
461
     *
462
     * @return string
463
     */
464
    protected function getLLLabel($label)
465
    {
466
        return $GLOBALS['LANG']->sL($label);
467
    }
468
}
469