Completed
Pull Request — master (#35)
by
unknown
02:56
created

QueueTask::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Andy Carter
4
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
5
 */
6
namespace Queue\Shell\Task;
7
8
use Cake\Console\ConsoleIo;
9
use Cake\Console\Shell;
10
use InvalidArgumentException;
11
12
/**
13
 * Queue Task.
14
 *
15
 * Common Queue plugin tasks properties and methods to be extended by custom
16
 * tasks.
17
 */
18
abstract class QueueTask extends Shell implements QueueTaskInterface
19
{
20
21
    /**
22
     *
23
     * @var string
24
     */
25
    public $queueModelClass = 'Queue.QueuedTasks';
26
27
    /**
28
     *
29
     * @var \Queue\Model\Table\QueuedTasksTable
30
     */
31
    public $QueuedTasks;
32
33
    /**
34
     * Timeout for run, after which the Task is reassigned to a new worker.
35
     *
36
     * @var int
37
     */
38
    public $timeout = 120;
39
40
    /**
41
     * Number of times a failed instance of this task should be restarted before giving up.
42
     *
43
     * @var int
44
     */
45
    public $retries = 1;
46
47
    /**
48
     *
49
     * @param \Cake\Console\ConsoleIo|null $io IO
50
     */
51
    public function __construct(ConsoleIo $io = null)
52
    {
53
        parent::__construct($io);
54
55
        $this->loadModel($this->queueModelClass);
56
    }
57
58
    /**
59
     *
60
     * @return string
61
     * @throws \InvalidArgumentException
62
     */
63
    protected function queueTaskName()
64
    {
65
        $class = get_class($this);
66
67
        preg_match('#\\\\Queue(.+)Task$#', $class, $matches);
68
        if (!$matches) {
69
            throw new InvalidArgumentException('Invalid class name: ' . $class);
70
        }
71
72
        return $matches[1];
73
    }
74
}
75