Completed
Pull Request — master (#20)
by Wachter
05:26
created

TaskExecutionRepository::flush()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 2
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 View Code Duplication
    public function findAll($page = 1, $pageSize = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
58
    {
59
        $query = $this->createQueryBuilder('e')
60
            ->innerJoin('e.task', 't')
61
            ->getQuery();
62
63
        if ($pageSize) {
64
            $query->setMaxResults($pageSize);
65
            $query->setFirstResult(($page - 1) * $pageSize);
66
        }
67
68
        return $query->getResult();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function findByStartTime(TaskInterface $task, \DateTime $scheduleTime)
75
    {
76
        try {
77
            return $this->createQueryBuilder('e')
78
                ->innerJoin('e.task', 't')
79
                ->where('t.uuid = :uuid')
80
                ->andWhere('e.scheduleTime = :scheduleTime')
81
                ->setParameter('uuid', $task->getUuid())
82
                ->setParameter('scheduleTime', $scheduleTime)
83
                ->getQuery()
84
                ->getSingleResult();
85
        } catch (NoResultException $e) {
86
            return;
87
        }
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function findScheduled(\DateTime $dateTime = null)
94
    {
95
        if ($dateTime === null) {
96
            $dateTime = new \DateTime();
97
        }
98
99
        return $this->createQueryBuilder('e')
100
            ->innerJoin('e.task', 't')
101
            ->where('e.status = :status AND e.scheduleTime < :dateTime')
102
            ->setParameter('status', TaskStatus::PLANNED)
103
            ->setParameter('dateTime', $dateTime)
104
            ->getQuery()
105
            ->getResult();
106
    }
107
}
108