Completed
Pull Request — master (#20)
by Wachter
08:56
created

TaskExecutionRepository::findByScheduledTime()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 12
nc 2
nop 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A TaskExecutionRepository::persist() 0 6 1
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 Doctrine\ORM\NoResultException;
16
use Task\Execution\TaskExecutionInterface;
17
use Task\Storage\TaskExecutionRepositoryInterface;
18
use Task\TaskInterface;
19
use Task\TaskStatus;
20
21
/**
22
 * Repository for task-execution.
23
 */
24
class TaskExecutionRepository extends EntityRepository implements TaskExecutionRepositoryInterface
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function create(TaskInterface $task, \DateTime $scheduleTime)
30
    {
31
        return new TaskExecution($task, $task->getHandlerClass(), $scheduleTime, $task->getWorkload());
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function persist(TaskExecutionInterface $execution)
38
    {
39
        $this->_em->persist($execution);
40
41
        return $this;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function flush()
48
    {
49
        $this->_em->flush();
50
51
        return $this;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
58
    {
59
        try {
60
            return $this->createQueryBuilder('e')
61
                ->innerJoin('e.task', 't')
62
                ->where('t.uuid = :uuid')
63
                ->andWhere('e.scheduleTime = :scheduleTime')
64
                ->setParameter('uuid', $task->getUuid())
65
                ->setParameter('scheduleTime', $scheduleTime)
66
                ->getQuery()
67
                ->getSingleResult();
68
        } catch (NoResultException $e) {
69
            return;
70
        }
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function findScheduled(\DateTime $dateTime = null)
77
    {
78
        if ($dateTime === null) {
79
            $dateTime = new \DateTime();
80
        }
81
82
        return $this->createQueryBuilder('e')
83
            ->innerJoin('e.task', 't')
84
            ->where('e.status = :status AND e.scheduleTime < :dateTime')
85
            ->setParameter('status', TaskStatus::PLANNED)
86
            ->setParameter('dateTime', $dateTime)
87
            ->getQuery()
88
            ->getResult();
89
    }
90
}
91