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\TaskBundle\Entity; |
13
|
|
|
|
14
|
|
|
use Doctrine\ORM\EntityRepository; |
15
|
|
|
use Task\Storage\TaskRepositoryInterface; |
16
|
|
|
use Task\TaskInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Repository for task. |
20
|
|
|
*/ |
21
|
|
|
class TaskRepository extends EntityRepository implements TaskRepositoryInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* {@inheritdoc} |
25
|
|
|
*/ |
26
|
9 |
|
public function create($handlerClass, $workload = null) |
27
|
|
|
{ |
28
|
9 |
|
return new Task($handlerClass, $workload); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
|
|
public function findByUuid($uuid) |
35
|
|
|
{ |
36
|
|
|
return $this->find($uuid); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
9 |
|
public function persist(TaskInterface $task) |
43
|
|
|
{ |
44
|
9 |
|
$this->_em->persist($task); |
45
|
|
|
|
46
|
9 |
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
|
|
public function remove(TaskInterface $task) |
53
|
|
|
{ |
54
|
|
|
$this->_em->remove($task); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
9 |
|
public function flush() |
63
|
|
|
{ |
64
|
9 |
|
$this->_em->flush(); |
65
|
|
|
|
66
|
9 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
5 |
View Code Duplication |
public function findAll($page = 1, $pageSize = null) |
|
|
|
|
73
|
|
|
{ |
74
|
5 |
|
$query = $this->createQueryBuilder('t') |
75
|
5 |
|
->getQuery(); |
76
|
|
|
|
77
|
5 |
|
if ($pageSize) { |
|
|
|
|
78
|
|
|
$query->setMaxResults($pageSize); |
79
|
|
|
$query->setFirstResult(($page - 1) * $pageSize); |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
return $query->getResult(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
2 |
|
public function findEndBeforeNow() |
89
|
|
|
{ |
90
|
2 |
|
return $this->findEndBefore(new \DateTime()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns task where last-execution is before given date-time. |
95
|
|
|
* |
96
|
|
|
* @param \DateTime $dateTime |
97
|
|
|
* |
98
|
|
|
* @return TaskInterface[] |
99
|
|
|
*/ |
100
|
2 |
|
public function findEndBefore(\DateTime $dateTime) |
101
|
|
|
{ |
102
|
2 |
|
return $this->createQueryBuilder('t') |
103
|
2 |
|
->where('t.lastExecution IS NULL OR t.lastExecution > :dateTime') |
104
|
2 |
|
->setParameter('dateTime', $dateTime) |
105
|
2 |
|
->getQuery() |
106
|
2 |
|
->getResult(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.