1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of php-task library. |
5
|
|
|
* |
6
|
|
|
* (c) php-task |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Task\Storage\ArrayStorage; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
15
|
|
|
use Doctrine\Common\Collections\Collection; |
16
|
|
|
use Task\Storage\TaskRepositoryInterface; |
17
|
|
|
use Task\Task; |
18
|
|
|
use Task\TaskInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Storage task in an array. |
22
|
|
|
*/ |
23
|
|
|
class ArrayTaskRepository implements TaskRepositoryInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Collection |
27
|
|
|
*/ |
28
|
|
|
private $taskCollection; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Collection $tasks |
32
|
|
|
*/ |
33
|
8 |
|
public function __construct(Collection $tasks = null) |
34
|
|
|
{ |
35
|
8 |
|
$this->taskCollection = $tasks ?: new ArrayCollection(); |
36
|
8 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
2 |
|
public function findByUuid($uuid) |
42
|
|
|
{ |
43
|
|
|
/** @var TaskInterface $task */ |
44
|
2 |
|
foreach ($this->taskCollection as $task) { |
45
|
2 |
|
if ($task->getUuid() === $uuid) { |
46
|
1 |
|
return $task; |
47
|
|
|
} |
48
|
1 |
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function create($handlerClass, $workload = null) |
55
|
|
|
{ |
56
|
|
|
return new Task($handlerClass, $workload); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
1 |
|
public function persist(TaskInterface $task) |
63
|
|
|
{ |
64
|
1 |
|
$this->taskCollection->add($task); |
65
|
|
|
|
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function remove(TaskInterface $task) |
73
|
|
|
{ |
74
|
1 |
|
$this->taskCollection->removeElement($task); |
75
|
|
|
|
76
|
1 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
1 |
|
public function flush() |
83
|
|
|
{ |
84
|
1 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
2 |
|
public function findAll($page = 1, $pageSize = null) |
91
|
|
|
{ |
92
|
2 |
|
return array_values($this->taskCollection->slice(($page - 1) * $pageSize, $pageSize)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
1 |
|
public function findEndBeforeNow() |
99
|
|
|
{ |
100
|
1 |
|
$now = new \DateTime(); |
101
|
|
|
|
102
|
1 |
|
return array_values( |
103
|
1 |
|
$this->taskCollection->filter( |
104
|
1 |
|
function (TaskInterface $task) use ($now) { |
105
|
1 |
|
return $task->getLastExecution() === null || $task->getLastExecution() > $now; |
106
|
|
|
} |
107
|
1 |
|
)->toArray() |
108
|
1 |
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|