Skip to content

Commit d057083

Browse files
refactored bundle to fit new structure of php-task
1 parent e228e05 commit d057083

File tree

11 files changed

+246
-261
lines changed

11 files changed

+246
-261
lines changed

src/Command/DebugTasksCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
5353
}
5454

5555
$table = new Table($output);
56-
$table->setHeaders(array('uuid', 'key', 'task-name', 'execution-date', 'completed', 'start', 'duration'));
56+
$table->setHeaders(['uuid', 'key', 'task-name', 'execution-date', 'completed', 'start', 'duration']);
5757

5858
foreach ($tasks as $task) {
5959
$start = null;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Task\TaskBundle\DoctrineStorage;
4+
5+
use Doctrine\Common\Persistence\ObjectManager;
6+
use Task\Execution\TaskExecutionInterface;
7+
use Task\Storage\TaskExecutionRepositoryInterface;
8+
use Task\TaskBundle\Entity\TaskExecutionRepository as ORMTaskExecutionRepository;
9+
use Task\TaskInterface;
10+
11+
class TaskExecutionRepository implements TaskExecutionRepositoryInterface
12+
{
13+
/**
14+
* @var ObjectManager
15+
*/
16+
private $objectManager;
17+
18+
/**
19+
* @var ORMTaskExecutionRepository
20+
*/
21+
private $taskExecutionRepository;
22+
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function save(TaskExecutionInterface $execution)
27+
{
28+
$this->objectManager->persist($execution);
29+
$this->objectManager->flush();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function add(TaskExecutionInterface $execution)
36+
{
37+
$this->objectManager->flush();
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function findScheduled()
44+
{
45+
return $this->taskExecutionRepository->findScheduled(new \DateTime());
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
52+
{
53+
return $this->taskExecutionRepository->findByStartTime($task, $scheduleTime);
54+
}
55+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Task\TaskBundle\DoctrineStorage;
4+
5+
use Doctrine\Common\Persistence\ObjectManager;
6+
use Task\Storage\TaskRepositoryInterface;
7+
use Task\TaskBundle\Entity\TaskRepository as ORMTaskRepository;
8+
use Task\TaskInterface;
9+
10+
class TaskRepository implements TaskRepositoryInterface
11+
{
12+
/**
13+
* @var ObjectManager
14+
*/
15+
private $objectManager;
16+
17+
/**
18+
* @var ORMTaskRepository
19+
*/
20+
private $taskRepository;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function add(TaskInterface $task)
26+
{
27+
$this->objectManager->persist($task);
28+
$this->objectManager->flush();
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function findAll()
35+
{
36+
return $this->taskRepository->findAll();
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function findEndBeforeNow()
43+
{
44+
return $this->taskRepository->findEndBefore(new \DateTime());
45+
}
46+
}

src/Entity/Task.php

Lines changed: 11 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22

33
namespace Task\TaskBundle\Entity;
44

5-
use Task\TaskInterface;
5+
use Cron\CronExpression;
6+
use Task\Task as BaseTask;
67

7-
class Task
8+
class Task extends BaseTask
89
{
910
/**
1011
* @var int
@@ -14,27 +15,7 @@ class Task
1415
/**
1516
* @var string
1617
*/
17-
private $uuid;
18-
19-
/**
20-
* @var string
21-
*/
22-
private $key;
23-
24-
/**
25-
* @var TaskInterface
26-
*/
27-
private $task;
28-
29-
/**
30-
* @var \DateTime
31-
*/
32-
private $executionDate;
33-
34-
/**
35-
* @var bool
36-
*/
37-
private $completed;
18+
private $intervalExpression;
3819

3920
/**
4021
* @return int
@@ -45,84 +26,20 @@ public function getId()
4526
}
4627

4728
/**
48-
* @return string
49-
*/
50-
public function getUuid()
51-
{
52-
return $this->uuid;
53-
}
54-
55-
/**
56-
* @param string $uuid
57-
*/
58-
public function setUuid($uuid)
59-
{
60-
$this->uuid = $uuid;
61-
}
62-
63-
/**
64-
* @return TaskInterface
65-
*/
66-
public function getTask()
67-
{
68-
return $this->task;
69-
}
70-
71-
/**
72-
* @param TaskInterface $task
73-
*/
74-
public function setTask($task)
75-
{
76-
$this->task = $task;
77-
}
78-
79-
/**
80-
* @return \DateTime
81-
*/
82-
public function getExecutionDate()
83-
{
84-
return $this->executionDate;
85-
}
86-
87-
/**
88-
* @param \DateTime $executionDate
89-
*/
90-
public function setExecutionDate($executionDate)
91-
{
92-
$this->executionDate = $executionDate;
93-
}
94-
95-
/**
96-
* @return bool
29+
* @return mixed
9730
*/
98-
public function isCompleted()
31+
public function getIntervalExpression()
9932
{
100-
return $this->completed;
33+
return $this->intervalExpression;
10134
}
10235

10336
/**
104-
* @param bool $completed
37+
* {@inheritdoc}
10538
*/
106-
public function setCompleted($completed)
39+
public function setInterval(CronExpression $interval, \DateTime $firstExecution = null, \DateTime $lastExecution = null)
10740
{
108-
$this->completed = $completed;
109-
}
41+
parent::setInterval($interval, $firstExecution, $lastExecution);
11042

111-
/**
112-
* @return string
113-
*/
114-
public function getKey()
115-
{
116-
return $this->key;
117-
}
118-
119-
/**
120-
* @param string $key
121-
*
122-
* @return $this
123-
*/
124-
public function setKey($key)
125-
{
126-
$this->key = $key;
43+
$this->intervalExpression = $interval->getExpression();
12744
}
12845
}

src/Entity/TaskExecution.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Task\TaskBundle\Entity;
4+
5+
use Task\Execution\TaskExecution as BaseTaskExecution;
6+
7+
class TaskExecution extends BaseTaskExecution
8+
{
9+
/**
10+
* @var int
11+
*/
12+
private $id;
13+
14+
/**
15+
* @return int
16+
*/
17+
public function getId()
18+
{
19+
return $this->id;
20+
}
21+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Task\TaskBundle\Entity;
4+
5+
use Doctrine\ORM\EntityRepository;
6+
use Task\TaskInterface;
7+
use Task\TaskStatus;
8+
9+
class TaskExecutionRepository extends EntityRepository
10+
{
11+
public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
12+
{
13+
return $this->createQueryBuilder('e')
14+
->innerJoin('e.task', 't')
15+
->where('t.uuid = :uuid')
16+
->andWhere('e.scheduleTime = :scheduleTime')
17+
->setParameter('uuid', $task->getUuid())
18+
->setParameter('scheduleTime', $scheduleTime)
19+
->getQuery()
20+
->getResult();
21+
}
22+
23+
public function findScheduled(\DateTime $dateTime)
24+
{
25+
return $this->createQueryBuilder('e')
26+
->innerJoin('e.task', 't')
27+
->where('e.status = :status AND e.scheduleTime < :dateTime')
28+
->setParameter('status', TaskStatus::PLANNED)
29+
->setParameter('dateTime', $dateTime)
30+
->getQuery()
31+
->getResult();
32+
}
33+
}

src/Entity/TaskRepository.php

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,11 @@
66

77
class TaskRepository extends EntityRepository
88
{
9-
public function findScheduled()
9+
public function findEndBefore(\DateTime $dateTime)
1010
{
11-
$query = $this->createQueryBuilder('task')
12-
->where('task.completed = :completed')
13-
->andWhere('task.executionDate <= :date')
14-
->setParameter('completed', false)
15-
->setParameter('date', new \DateTime())
16-
->getQuery();
17-
18-
return $query->getResult();
19-
}
20-
21-
/**
22-
* @param string $uuid
23-
*
24-
* @return Task
25-
*/
26-
public function findByUuid($uuid)
27-
{
28-
$query = $this->createQueryBuilder('task')
29-
->where('task.uuid = :uuid')
30-
->setParameter('uuid', $uuid)
31-
->getQuery();
32-
33-
return $query->getSingleResult();
34-
}
35-
36-
public function deleteAll()
37-
{
38-
$query = $this->_em->createQueryBuilder()
39-
->delete($this->_entityName, 'task')
40-
->getQuery();
41-
42-
$query->execute();
11+
$this->createQueryBuilder('t')
12+
->where('t.lastExecution IS NOT NULL OR t.lastExecution > :dateTime')
13+
->setParameter('dateTime', $dateTime)
14+
->getQuery()->getResult();
4315
}
4416
}

src/Handler/TaskHandlerFactory.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Task\TaskBundle\Handler;
4+
5+
use Symfony\Component\CssSelector\Parser\Handler\HandlerInterface;
6+
use Task\Handler\TaskHandlerFactoryInterface;
7+
use Task\Handler\TaskHandlerNotExistsException;
8+
9+
class TaskHandlerFactory implements TaskHandlerFactoryInterface
10+
{
11+
/**
12+
* @var HandlerInterface[]
13+
*/
14+
private $handler = [];
15+
16+
public function __construct(array $handler)
17+
{
18+
$this->handler = $handler;
19+
}
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function create($className)
25+
{
26+
if (!array_key_exists($className, $this->handler)) {
27+
throw new TaskHandlerNotExistsException($className);
28+
}
29+
30+
return $this->handler[$className];
31+
}
32+
}

0 commit comments

Comments
 (0)