Completed
Push — master ( e481c7...bcdb1b )
by Marco
35:35 queued 20:00
created

Manager   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 282
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 282
rs 10
c 0
b 0
f 0
wmc 28

13 Methods

Rating   Name   Duplication   Size   Complexity  
A enableByName() 0 17 2
A getByName() 0 9 2
A enable() 0 17 2
A get() 0 7 1
A add() 0 15 1
B getAll() 0 36 4
A edit() 0 21 3
A disable() 0 17 2
A remove() 0 15 2
B addBulk() 0 31 3
A disableByName() 0 17 2
A __construct() 0 13 2
A removeByName() 0 15 2
1
<?php namespace Comodojo\Extender\Schedule;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Comodojo\Foundation\Events\Manager as EventsManager;
5
use \Comodojo\Foundation\Logging\LoggerTrait;
6
use \Comodojo\Foundation\Events\EventsTrait;
7
use \Comodojo\Extender\Components\Database;
8
use \Comodojo\Foundation\Base\ConfigurationTrait;
9
use \Comodojo\Extender\Traits\EntityManagerTrait;
10
use \Comodojo\Extender\Orm\Entities\Schedule;
11
use \Comodojo\Extender\Events\ScheduleEvent;
12
use \Doctrine\ORM\EntityManager;
13
use \Psr\Log\LoggerInterface;
14
use \DateTime;
15
use \Exception;
16
17
/**
18
* @package     Comodojo Extender
19
* @author      Marco Giovinazzi <[email protected]>
20
* @license     MIT
21
*
22
* LICENSE:
23
*
24
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
* THE SOFTWARE.
31
 */
32
33
class Manager {
34
35
    use ConfigurationTrait;
36
    use LoggerTrait;
37
    use EventsTrait;
38
    use EntityManagerTrait;
39
40
    /**
41
     * Class constructor
42
     *
43
     * @param Configuration $configuration
44
     * @param LoggerInterface $logger
45
     * @param TasksTable $tasks
0 ignored issues
show
Bug introduced by
The type Comodojo\Extender\Schedule\TasksTable was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
     * @param EventsManager $events
47
     * @param EntityManager $em
48
     */
49
    public function __construct(
50
        Configuration $configuration,
51
        LoggerInterface $logger,
52
        EventsManager $events,
53
        EntityManager $em = null
54
    ) {
55
56
        $this->setConfiguration($configuration);
57
        $this->setLogger($logger);
58
        $this->setEvents($events);
59
60
        $em = is_null($em) ? Database::init($configuration)->getEntityManager() : $em;
61
        $this->setEntityManager($em);
62
63
    }
64
65
    public function get($id) {
66
67
        $em = $this->getEntityManager();
68
69
        $data = $em->find('Comodojo\Extender\Orm\Entities\Schedule', $id);
70
71
        return $data;
72
73
    }
74
75
    public function getByName($name) {
76
77
        $em = $this->getEntityManager();
78
79
        $data = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findBy(["name" => $name]);
80
81
        if ( empty($data) ) return null;
82
83
        return $data[0];
84
85
    }
86
87
    public function getAll($ready = false) {
88
89
        $logger = $this->getLogger();
90
91
        $time = new DateTime();
92
93
        $em = $this->getEntityManager();
94
95
        $standby = $em->getRepository('Comodojo\Extender\Orm\Entities\Schedule')->findAll();
96
97
        return $ready ? array_filter($standby, function($job) use ($time, $logger) {
98
99
            $name = $job->getName();
100
            $id = $job->getId();
101
            $enabled = $job->getEnabled();
102
            $firstrun = $job->getFirstrun();
103
            $lastrun = $job->getLastrun();
104
            $expression = $job->getExpression();
105
106
            if ( $lastrun !== null ) {
107
                $nextrun = $expression->getNextRunDate($lastrun);
108
            } else {
109
                $nextrun = $firstrun;
110
            }
111
112
            $shouldrun = $nextrun <= $time;
113
114
            $logger->debug("Job $name (id $id) will ".($shouldrun ? "" : "NOT ")."be executed", [
115
                'ENABLED' => $enabled,
116
                'NEXTRUN' => $nextrun->format('r'),
117
                'SHOULDRUN' => $shouldrun
118
            ]);
119
120
            return $shouldrun;
121
122
        }) : $standby;
123
124
    }
125
126
    public function add(Schedule $schedule) {
127
128
        $time = new DateTime();
129
130
        $schedule->setFirstrun($schedule->getNextPlannedRun($time));
131
132
        $this->getEvents()->emit( new ScheduleEvent('add', $schedule) );
133
134
        $em = $this->getEntityManager();
135
136
        $em->persist($schedule);
137
138
        $em->flush();
139
140
        return $schedule->getId();
141
142
    }
143
144
    public function addBulk(array $schedules) {
145
146
        $time = new DateTime();
147
        $records = [];
148
        $em = $this->getEntityManager();
149
150
        foreach ($schedules as $key => $schedule) {
151
152
            try {
153
154
                $schedule->setFirstrun($schedule->getNextPlannedRun($time));
155
156
                $this->getEvents()->emit( new ScheduleEvent('add', $schedule) );
157
158
                $em->persist($schedule);
159
160
                $em->flush();
161
162
            } catch (Exception $e) {
163
164
                $records[$key] = false;
165
166
                continue;
167
168
            }
169
170
            $records[$key] = $schedule->getId();
171
172
        }
173
174
        return $records;
175
176
    }
177
178
    public function edit(Schedule $schedule) {
179
180
        $em = $this->getEntityManager();
181
182
        $id = $schedule->getId();
183
184
        if ( empty($id) ) throw new Exception("Cannot edit scheule without id");
185
186
        $old_schedule = $this->get($schedule->getId());
187
188
        if ( empty($old_schedule) ) throw new Exception("Cannot find schedule with id $id");
189
190
        $this->getEvents()->emit( new ScheduleEvent('edit', $schedule, $old_schedule) );
191
192
        $old_schedule->merge($schedule);
193
194
        $em->persist($old_schedule);
195
196
        $em->flush();
197
198
        return true;
199
200
    }
201
202
    public function remove($id) {
203
204
        $em = $this->getEntityManager();
205
206
        $schedule = $this->get($id);
207
208
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $id");
209
210
        $this->getEvents()->emit( new ScheduleEvent('remove', $schedule) );
211
212
        $em->remove($schedule);
213
214
        $em->flush();
215
216
        return true;
217
218
    }
219
220
    public function removeByName($name) {
221
222
        $em = $this->getEntityManager();
223
224
        $schedule = $this->getByName($name);
225
226
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name");
227
228
        $this->getEvents()->emit( new ScheduleEvent('remove', $schedule) );
229
230
        $em->remove($schedule);
231
232
        $em->flush();
233
234
        return true;
235
236
    }
237
238
    public function enable($id) {
239
240
        $em = $this->getEntityManager();
241
242
        $schedule = $this->get($id);
243
244
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $id");
245
246
        $this->getEvents()->emit( new ScheduleEvent('enable', $schedule) );
247
248
        $schedule->setEnabled(true);
249
250
        $em->persist($schedule);
251
252
        $em->flush();
253
254
        return true;
255
256
    }
257
258
    public function enableByName($name) {
259
260
        $em = $this->getEntityManager();
261
262
        $schedule = $this->getByName($name);
263
264
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name");
265
266
        $this->getEvents()->emit( new ScheduleEvent('enable', $schedule) );
267
268
        $schedule->setEnabled(true);
269
270
        $em->persist($schedule);
271
272
        $em->flush();
273
274
        return true;
275
276
    }
277
278
    public function disable($id) {
279
280
        $em = $this->getEntityManager();
281
282
        $schedule = $this->get($id);
283
284
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $id");
285
286
        $this->getEvents()->emit( new ScheduleEvent('disable', $schedule) );
287
288
        $schedule->setEnabled(false);
289
290
        $em->persist($schedule);
291
292
        $em->flush();
293
294
        return true;
295
296
    }
297
298
    public function disableByName($name) {
299
300
        $em = $this->getEntityManager();
301
302
        $schedule = $this->getByName($name);
303
304
        if ( is_null($schedule) ) throw new Exception("Cannot find scheule $name");
305
306
        $this->getEvents()->emit( new ScheduleEvent('disable', $schedule) );
307
308
        $schedule->setEnabled(false);
309
310
        $em->persist($schedule);
311
312
        $em->flush();
313
314
        return true;
315
316
    }
317
318
}
319