Completed
Push — develop ( b0afbf...396dfb )
by Bob Olde
02:24
created

TaskManagerCommand   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 45.9 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 7
c 1
b 0
f 1
lcom 0
cbo 0
dl 28
loc 61
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionRun() 15 23 3
B actionWatch() 13 26 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Craft;
4
5
/**
6
 * Task Manager.
7
 *
8
 * @author    Bob Olde Hampsink <[email protected]>
9
 * @copyright Copyright (c) 2015, Bob Olde Hampsink
10
 * @license   MIT
11
 *
12
 * @link      http://github.com/boboldehampsink
13
 */
14
class TaskManagerCommand extends BaseCommand
15
{
16
    /**
17
     * Runs pending tasks.
18
     *
19
     * @return int
20
     */
21
    public function actionRun()
22
    {
23
        Craft::log(Craft::t('Running new tasks.'));
24
25
        // Make sure tasks aren't already running
26 View Code Duplication
        if (!craft()->tasks->isTaskRunning()) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
28
            // Is there a pending task?
29
            if (craft()->tasks->getNextPendingTask()) {
30
31
                // Start running tasks
32
                craft()->tasks->runPendingTasks();
33
34
                return 1;
35
            } else {
36
                Craft::log(Craft::t('No pending tasks found.'));
37
            }
38
        } else {
39
            Craft::log(Craft::t('Tasks are already running.'));
40
        }
41
42
        return 0;
43
    }
44
45
    /**
46
     * Watch for tasks and run them.
47
     */
48
    public function actionWatch()
49
    {
50
        Craft::log(Craft::t('Watching for new tasks.'));
51
52
        // Keep on checking for pending tasks
53
        while (true) {
54
55
            // Make sure tasks aren't already running
56 View Code Duplication
            if (!craft()->tasks->isTaskRunning()) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
58
                // Is there a pending task?
59
                if (craft()->tasks->getNextPendingTask()) {
60
61
                    // Start running tasks
62
                    craft()->tasks->runPendingTasks();
63
                } else {
64
                    Craft::log(Craft::t('No pending tasks found.'));
65
                }
66
            } else {
67
                Craft::log(Craft::t('Tasks are already running, skipping iteration.'));
68
            }
69
70
            // Sleep a little
71
            sleep(10);
72
        }
73
    }
74
}
75