Completed
Push — 2.0 ( 21f326...dd3689 )
by Marco
17:36
created

Runner::createTask()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 9
nc 2
nop 3
1
<?php namespace Comodojo\Extender\Tasks;
2
3
use \Comodojo\Extender\Tasks\Table as TasksTable;
4
use \Comodojo\Extender\Components\Database;
5
use \Comodojo\Extender\Events\TaskEvent;
6
use \Comodojo\Extender\Events\TaskStatusEvent;
7
use \Comodojo\Dispatcher\Components\Configuration;
8
use \Comodojo\Dispatcher\Components\EventsManager;
9
use \Psr\Log\LoggerInterface;
10
use \Doctrine\DBAL\Connection;
11
use \Exception;
12
13
/**
14
 * Job runner
15
 *
16
 * @package     Comodojo extender
17
 * @author      Marco Giovinazzi <[email protected]>
18
 * @license     GPL-3.0+
19
 *
20
 * LICENSE:
21
 *
22
 * This program is free software: you can redistribute it and/or modify
23
 * it under the terms of the GNU Affero General Public License as
24
 * published by the Free Software Foundation, either version 3 of the
25
 * License, or (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU Affero General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU Affero General Public License
33
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34
 */
35
36
class Runner {
37
38
    protected $configuration;
39
40
    protected $logger;
41
42
    protected $events;
43
44
    protected $dbh;
45
46
    protected $worklog;
47
48
    protected $tasks;
49
50
    public function __construct(
51
        Configuration $configuration,
52
        LoggerInterface $logger,
53
        TasksTable $tasks,
54
        EventsManager $events,
55
        Connection $dbh = null
56
    ) {
57
58
        // init components
59
        $this->configuration = $configuration;
60
        $this->logger = $logger;
61
        $this->events = $events;
62
        $this->tasks = $tasks;
63
64
        // init database
65
        $this->dbh = is_null($dbh) ? Database::init($configuration) : $dbh;
66
67
        // init worklog manager
68
        $this->worklog = new Worklog($this->dbh, $this->configuration->get('database-worklogs-table'));
69
70
    }
71
72
    public function run($name, $task, $jid = null, $parameters = array()) {
73
74
        try {
75
76
            $this->logger->info("Starting new task $task ($name)");
77
78
            $start = microtime(true);
79
80
            $thetask = $this->createTask($name, $task, $parameters);
81
82
            $this->events->emit( new TaskEvent('start', $thetask) );
83
84
            $pid = $thetask->pid;
0 ignored issues
show
Bug introduced by
Accessing pid on the interface Comodojo\Extender\Tasks\TaskInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
85
86
            $wid = $this->worklog->open($pid, $name, $jid, $task, $parameters, $start);
87
88
            $status = true;
89
90
            $this->events->emit( new TaskStatusEvent('start', $thetask) );
91
92
            try {
93
94
                // run task
95
                $result = $thetask->run();
96
97
            } catch (TaskException $te) {
0 ignored issues
show
Bug introduced by
The class Comodojo\Extender\Tasks\TaskException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
99
                $status = false;
100
101
                $result = $te->getMessage();
102
103
            } catch (Exception $e) {
104
105
                $status = false;
106
107
                $result = $e->getMessage();
108
109
            }
110
111
            $this->events->emit( new TaskStatusEvent($status ? 'success' : 'error', $thetask) );
112
113
            $this->events->emit( new TaskStatusEvent('stop', $thetask) );
114
115
            $this->events->emit( new TaskEvent('stop', $thetask) );
116
117
            $end = microtime(true);
118
119
            $this->worklog->close($wid, $status, $result, $end);
120
121
            $this->logger->notice("Task $name ($task) with pid $pid ends in ".$status ? 'success' : 'error');
122
123
        } catch (Exception $e) {
124
125
            throw $e;
126
127
        }
128
129
        return new Result(
130
            array (
131
                $pid,
132
                $name,
133
                $status,
134
                $start,
135
                $end,
136
                $result,
137
                intval($wid)
138
            )
139
        );
140
141
    }
142
143
    private function createTask($name, $task, $parameters) {
144
145
        // retrieve the task class
146
        $class = $this->tasks->get($task)->class;
147
148
        // create a task instance
149
        $thetask = new $class(
150
            $this->logger,
151
            $name,
152
            $parameters
153
        );
154
155
        if ( !($thetask instanceof \Comodojo\Extender\Tasks\TaskInterface) ) {
156
            throw new Exception("Invalid task object $class");
157
        }
158
159
        return $thetask;
160
161
    }
162
163
}
164