Skip to content

Commit 82ddd23

Browse files
authored
Enhancement/doctrine compatibility (#57)
* Fix doctrine compatibility * Update DateTime to DateTimeImmutable
1 parent 0b1797b commit 82ddd23

15 files changed

+67
-69
lines changed

src/Command/DebugTasksCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6565
$page = $input->getOption('page');
6666
$pageSize = $input->getOption('page-size');
6767

68-
$executions = $this->taskExecutionRepository->findAll($page, $pageSize);
68+
$executions = $this->taskExecutionRepository->findAllPaginated($page, $pageSize);
6969

7070
$table = new Table($output);
7171
$table->setHeaders(['uuid', 'status', 'handler', 'schedule time', 'end time', 'duration']);
@@ -76,8 +76,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7676
$execution->getUuid(),
7777
$execution->getStatus(),
7878
$execution->getHandlerClass(),
79-
$execution->getScheduleTime()->format(\DateTime::RFC3339),
80-
!$execution->getEndTime() ? '' : $execution->getEndTime()->format(\DateTime::RFC3339),
79+
$execution->getScheduleTime()->format(\DateTimeImmutable::RFC3339),
80+
!$execution->getEndTime() ? '' : $execution->getEndTime()->format(\DateTimeImmutable::RFC3339),
8181
(round($execution->getDuration(), 6) * 1000000) . 'ms',
8282
]
8383
);

src/Command/ScheduleSystemTasksCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ private function disableSystemTask($systemKey)
177177
*/
178178
public function disableTask(TaskInterface $task)
179179
{
180-
$task->setInterval($task->getInterval(), $task->getFirstExecution(), new \DateTime());
180+
$task->setInterval($task->getInterval(), $task->getFirstExecution(), new \DateTimeImmutable());
181181

182182
return $this->abortPending($task);
183183
}

src/Command/ScheduleTaskCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8383
if (null !== $cronExpression) {
8484
$endDate = null;
8585
if (null !== $endDateString) {
86-
$endDate = new \DateTime($endDateString);
86+
$endDate = new \DateTimeImmutable($endDateString);
8787
}
8888

89-
$taskBuilder->cron($cronExpression, new \DateTime(), $endDate);
89+
$taskBuilder->cron($cronExpression, new \DateTimeImmutable(), $endDate);
9090
}
9191

9292
if (null !== $executionDateString) {
93-
$taskBuilder->executeAt(new \DateTime($executionDateString));
93+
$taskBuilder->executeAt(new \DateTimeImmutable($executionDateString));
9494
}
9595

9696
$taskBuilder->schedule();

src/Entity/Task.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getInterval()
5252
/**
5353
* {@inheritdoc}
5454
*/
55-
public function setInterval(CronExpression $interval, \DateTime $firstExecution = null, \DateTime $lastExecution = null)
55+
public function setInterval(CronExpression $interval, \DateTimeImmutable $firstExecution = null, \DateTimeImmutable $lastExecution = null)
5656
{
5757
parent::setInterval($interval, $firstExecution, $lastExecution);
5858

src/Entity/TaskExecutionRepository.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class TaskExecutionRepository extends EntityRepository implements TaskExecutionR
2626
/**
2727
* {@inheritdoc}
2828
*/
29-
public function create(TaskInterface $task, \DateTime $scheduleTime)
29+
public function create(TaskInterface $task, \DateTimeImmutable $scheduleTime)
3030
{
3131
return new TaskExecution($task, $task->getHandlerClass(), $scheduleTime, $task->getWorkload());
3232
}
@@ -36,8 +36,8 @@ public function create(TaskInterface $task, \DateTime $scheduleTime)
3636
*/
3737
public function save(TaskExecutionInterface $execution)
3838
{
39-
$this->_em->persist($execution);
40-
$this->_em->flush($execution);
39+
$this->getEntityManager()->persist($execution);
40+
$this->getEntityManager()->flush($execution);
4141

4242
return $this;
4343
}
@@ -47,16 +47,16 @@ public function save(TaskExecutionInterface $execution)
4747
*/
4848
public function remove(TaskExecutionInterface $execution)
4949
{
50-
$this->_em->remove($execution);
51-
$this->_em->flush($execution);
50+
$this->getEntityManager()->remove($execution);
51+
$this->getEntityManager()->flush($execution);
5252

5353
return $this;
5454
}
5555

5656
/**
5757
* {@inheritdoc}
5858
*/
59-
public function findAll($page = 1, $pageSize = null)
59+
public function findAllPaginated(int $page = 1, ?int $pageSize = null): array
6060
{
6161
$query = $this->createQueryBuilder('e')
6262
->innerJoin('e.task', 't')
@@ -129,13 +129,13 @@ public function findByTaskUuid($taskUuid)
129129
/**
130130
* {@inheritdoc}
131131
*/
132-
public function findNextScheduled(\DateTime $dateTime = null, array $skippedExecutions = [])
132+
public function findNextScheduled(\DateTimeImmutable $dateTime = null, array $skippedExecutions = [])
133133
{
134134
$queryBuilder = $this->createQueryBuilder('e')
135135
->innerJoin('e.task', 't')
136136
->where('e.status = :status')
137137
->andWhere('e.scheduleTime < :date')
138-
->setParameter('date', $dateTime ?: new \DateTime())
138+
->setParameter('date', $dateTime ?: new \DateTimeImmutable())
139139
->setParameter('status', TaskStatus::PLANNED)
140140
->setMaxResults(1);
141141

src/Entity/TaskRepository.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Task\TaskBundle\Entity;
1313

14-
use DateTime;
1514
use Doctrine\ORM\EntityRepository;
1615
use Doctrine\ORM\NoResultException;
1716
use Task\TaskInterface;
@@ -44,8 +43,8 @@ public function findByUuid($uuid)
4443
*/
4544
public function save(TaskInterface $task)
4645
{
47-
$this->_em->persist($task);
48-
$this->_em->flush($task);
46+
$this->getEntityManager()->persist($task);
47+
$this->getEntityManager()->flush($task);
4948

5049
return $this;
5150
}
@@ -55,16 +54,16 @@ public function save(TaskInterface $task)
5554
*/
5655
public function remove(TaskInterface $task)
5756
{
58-
$this->_em->remove($task);
59-
$this->_em->flush($task);
57+
$this->getEntityManager()->remove($task);
58+
$this->getEntityManager()->flush($task);
6059

6160
return $this;
6261
}
6362

6463
/**
6564
* {@inheritdoc}
6665
*/
67-
public function findAll($page = 1, $pageSize = null): array
66+
public function findAllPaginated(int $page = 1, ?int $pageSize = null): array
6867
{
6968
$query = $this->createQueryBuilder('t')
7069
->getQuery();
@@ -82,17 +81,17 @@ public function findAll($page = 1, $pageSize = null): array
8281
*/
8382
public function findEndBeforeNow()
8483
{
85-
return $this->findEndBefore(new \DateTime());
84+
return $this->findEndBefore(new \DateTimeImmutable());
8685
}
8786

8887
/**
8988
* Returns task where last-execution is before given date-time.
9089
*
91-
* @param \DateTime $dateTime
90+
* @param \DateTimeImmutable $dateTime
9291
*
9392
* @return TaskInterface[]
9493
*/
95-
public function findEndBefore(\DateTime $dateTime)
94+
public function findEndBefore(\DateTimeImmutable $dateTime)
9695
{
9796
return $this->createQueryBuilder('t')
9897
->where('t.lastExecution IS NULL OR t.lastExecution > :dateTime')

src/Resources/config/doctrine/Task.orm.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
<field name="handlerClass" type="string" length="255"/>
1717
<field name="intervalExpression" type="string" length="255" nullable="true"/>
18-
<field name="firstExecution" type="datetime" nullable="true"/>
19-
<field name="lastExecution" type="datetime" nullable="true"/>
18+
<field name="firstExecution" type="datetime_immutable" nullable="true"/>
19+
<field name="lastExecution" type="datetime_immutable" nullable="true"/>
2020
<field name="systemKey" type="string" nullable="true" unique="true" length="191"/>
21-
<field name="workload" type="object"/>
21+
<field name="workload" type="json"/>
2222

2323
</entity>
2424
</doctrine-mapping>

src/Resources/config/doctrine/TaskExecution.orm.xml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@
1414
</id>
1515

1616
<field name="handlerClass" type="string" length="255"/>
17-
<field name="workload" type="object"/>
17+
<field name="workload" type="json"/>
1818
<field name="duration" type="float" nullable="true"/>
19-
<field name="startTime" type="datetime" nullable="true"/>
20-
<field name="endTime" type="datetime" nullable="true"/>
21-
<field name="scheduleTime" column="schedule_time" type="datetime"/>
19+
<field name="startTime" type="datetime_immutable" nullable="true"/>
20+
<field name="endTime" type="datetime_immutable" nullable="true"/>
21+
<field name="scheduleTime" column="schedule_time" type="datetime_immutable"/>
2222
<field name="exception" type="text" nullable="true"/>
23-
<field name="result" type="object" nullable="true"/>
23+
<field name="result" type="json" nullable="true"/>
2424
<field name="status" type="string" length="20"/>
2525
<field name="attempts" type="integer"/>
2626

2727
<many-to-one target-entity="Task\TaskBundle\Entity\Task" field="task">
2828
<join-column name="task_id" referenced-column-name="uuid" on-delete="CASCADE"/>
2929
</many-to-one>
30-
3130
</entity>
3231
</doctrine-mapping>

tests/Functional/BaseCommandTestCase.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected function createTask($workload, CronExpression $cronExpression = null,
102102
{
103103
$task = $this->taskRepository->create($handlerClass, $workload);
104104
if ($cronExpression) {
105-
$task->setInterval($cronExpression, new \DateTime(), new \DateTime('+1 year'));
105+
$task->setInterval($cronExpression, new \DateTimeImmutable(), new \DateTimeImmutable('+1 year'));
106106
}
107107
$this->taskRepository->save($task);
108108

@@ -113,12 +113,12 @@ protected function createTask($workload, CronExpression $cronExpression = null,
113113
* Create task-execution.
114114
*
115115
* @param TaskInterface $task
116-
* @param \DateTime $scheduleTime
116+
* @param \DateTimeImmutable $scheduleTime
117117
* @param string $status
118118
*
119119
* @return TaskExecutionInterface
120120
*/
121-
protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
121+
protected function createTaskExecution(TaskInterface $task, \DateTimeImmutable $scheduleTime, $status = TaskStatus::PLANNED)
122122
{
123123
$execution = $this->taskExecutionRepository->create($task, $scheduleTime);
124124
$execution->setStatus($status);

tests/Functional/BaseDatabaseTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ protected function createTask($handlerClass = TestHandler::class)
7373
* Create a new task-execution.
7474
*
7575
* @param TaskInterface $task
76-
* @param \DateTime $scheduleTime
76+
* @param \DateTimeImmutable $scheduleTime
7777
* @param string $status
7878
*
7979
* @return TaskExecution
8080
*/
81-
protected function createTaskExecution(TaskInterface $task, \DateTime $scheduleTime, $status = TaskStatus::PLANNED)
81+
protected function createTaskExecution(TaskInterface $task, \DateTimeImmutable $scheduleTime, $status = TaskStatus::PLANNED)
8282
{
8383
$execution = new TaskExecution($task, $task->getHandlerClass(), $scheduleTime);
8484
$execution->setStatus($status);

0 commit comments

Comments
 (0)