1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Commander project. |
4
|
|
|
* |
5
|
|
|
* @author Daniel Schröder <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace GravityMedia\Commander; |
9
|
|
|
|
10
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
11
|
|
|
use GravityMedia\Commander\Entity\TaskEntity; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Task manager class. |
15
|
|
|
* |
16
|
|
|
* @package GravityMedia\Commander |
17
|
|
|
*/ |
18
|
|
|
class TaskManager |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The entity manager. |
22
|
|
|
* |
23
|
|
|
* @var EntityManagerInterface |
24
|
|
|
*/ |
25
|
|
|
protected $entityManager; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create task manager object. |
29
|
|
|
* |
30
|
|
|
* @param EntityManagerInterface $entityManager |
31
|
|
|
*/ |
32
|
|
|
public function __construct(EntityManagerInterface $entityManager) |
33
|
|
|
{ |
34
|
|
|
$this->entityManager = $entityManager; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get all tasks. |
39
|
|
|
* |
40
|
|
|
* @param array $criteria |
41
|
|
|
* |
42
|
|
|
* @return Task[] |
43
|
|
|
*/ |
44
|
|
|
public function getTasks(array $criteria = []) |
45
|
|
|
{ |
46
|
|
|
return array_map(function (TaskEntity $entity) { |
47
|
|
|
return new Task($this->entityManager, $entity); |
48
|
|
|
}, $this->entityManager->getRepository(TaskEntity::class)->findBy($criteria)); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get single task by criteria. |
53
|
|
|
* |
54
|
|
|
* @param array $criteria |
55
|
|
|
* |
56
|
|
|
* @return null|Task |
57
|
|
|
*/ |
58
|
|
|
public function getTask(array $criteria) |
59
|
|
|
{ |
60
|
|
|
/** @var TaskEntity $entity */ |
61
|
|
|
$entity = $this->entityManager->getRepository(TaskEntity::class)->findOneBy($criteria); |
62
|
|
|
if (null === $entity) { |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return new Task($this->entityManager, $entity); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Add task. |
71
|
|
|
* |
72
|
|
|
* @param string $script |
73
|
|
|
* @param int $priority |
74
|
|
|
* |
75
|
|
|
* @return Task |
76
|
|
|
*/ |
77
|
|
|
public function addTask($script, $priority = TaskEntity::DEFAULT_PRIORITY) |
78
|
|
|
{ |
79
|
|
|
$entity = new TaskEntity(); |
80
|
|
|
$entity->setScript($script); |
81
|
|
|
$entity->setPriority($priority); |
82
|
|
|
|
83
|
|
|
$this->entityManager->persist($entity); |
84
|
|
|
$this->entityManager->flush(); |
85
|
|
|
|
86
|
|
|
return new Task($this->entityManager, $entity); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|