Completed
Push — master ( 454108...fbd2bc )
by Alexander
09:20 queued 03:33
created

TaskExecutionRepository   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 131
Duplicated Lines 9.92 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 95.24%

Importance

Changes 2
Bugs 1 Features 1
Metric Value
wmc 15
c 2
b 1
f 1
lcom 2
cbo 5
dl 13
loc 131
ccs 60
cts 63
cp 0.9524
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A save() 0 7 1
A findAll() 13 13 2
A findPending() 0 15 2
A findByTask() 0 4 1
A findByTaskUuid() 0 9 1
A remove() 0 7 1
A findByUuid() 0 12 2
B findNextScheduled() 0 22 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 10
    public function create(TaskInterface $task, \DateTime $scheduleTime)
30
    {
31 10
        return new TaskExecution($task, $task->getHandlerClass(), $scheduleTime, $task->getWorkload());
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 21
    public function save(TaskExecutionInterface $execution)
38
    {
39 21
        $this->_em->persist($execution);
40 21
        $this->_em->flush($execution);
41
42 21
        return $this;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function remove(TaskExecutionInterface $execution)
49
    {
50 1
        $this->_em->remove($execution);
51 1
        $this->_em->flush($execution);
52
53 1
        return $this;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 8 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...
60
    {
61 8
        $query = $this->createQueryBuilder('e')
62 8
            ->innerJoin('e.task', 't')
63 8
            ->getQuery();
64
65 8
        if ($pageSize) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $pageSize of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
66 4
            $query->setMaxResults($pageSize);
67 4
            $query->setFirstResult(($page - 1) * $pageSize);
68 4
        }
69
70 8
        return $query->getResult();
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 9
    public function findPending(TaskInterface $task)
77
    {
78
        try {
79 9
            return $this->createQueryBuilder('e')
80 9
                ->innerJoin('e.task', 't')
81 9
                ->where('t.uuid = :uuid')
82 9
                ->andWhere('e.status in (:status)')
83 9
                ->setParameter('uuid', $task->getUuid())
84 9
                ->setParameter('status', [TaskStatus::PLANNED, TaskStatus::RUNNING])
85 9
                ->getQuery()
86 9
                ->getSingleResult();
87 9
        } catch (NoResultException $e) {
88 9
            return;
89
        }
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 5
    public function findByUuid($uuid)
96
    {
97
        try {
98 5
            return $this->createQueryBuilder('e')
99 5
                ->where('e.uuid = :uuid')
100 5
                ->setParameter('uuid', $uuid)
101 5
                ->getQuery()
102 5
                ->getSingleResult();
103 1
        } catch (NoResultException $e) {
104 1
            return;
105
        }
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 4
    public function findByTask(TaskInterface $task)
112
    {
113 4
        return $this->findByTaskUuid($task->getUuid());
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 5
    public function findByTaskUuid($taskUuid)
120
    {
121 5
        return $this->createQueryBuilder('e')
122 5
            ->innerJoin('e.task', 't')
123 5
            ->where('t.uuid = :uuid')
124 5
            ->setParameter('uuid', $taskUuid)
125 5
            ->getQuery()
126 5
            ->getResult();
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 5
    public function findNextScheduled(\DateTime $dateTime = null, array $skippedExecutions = [])
133
    {
134 5
        $queryBuilder = $this->createQueryBuilder('e')
135 5
            ->innerJoin('e.task', 't')
136 5
            ->where('e.status = :status')
137 5
            ->andWhere('e.scheduleTime < :date')
138 5
            ->setParameter('date', $dateTime ?: new \DateTime())
139 5
            ->setParameter('status', TaskStatus::PLANNED)
140 5
            ->setMaxResults(1);
141
142 5
        $expr = $queryBuilder->expr();
143 5
        if (!empty($skippedExecutions)) {
144
            $queryBuilder->andWhere($expr->not($expr->in('e.uuid', ':skipped')))
145
                ->setParameter('skipped', $skippedExecutions);
146
        }
147
148
        try {
149 5
            return $queryBuilder->getQuery()->getSingleResult();
150 4
        } catch (NoResultException $exception) {
151 4
            return null;
152
        }
153
    }
154
}
155