Test Failed
Push — master ( faf208...51c6fd )
by Martin
14:25
created

TaskRepository   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 110
Duplicated Lines 29.09 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 32
loc 110
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findAll() 0 4 1
A find() 0 11 2
A findAllByStatus() 0 8 1
A findByName() 0 15 2
A save() 16 16 3
A remove() 16 16 3
A removeByStatus() 0 9 1

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
namespace Todo\Infrastructure\Persistence\DoctrineORM\Repository;
4
5
use Doctrine\ORM\EntityRepository;
6
use Doctrine\ORM\OptimisticLockException;
7
use Doctrine\ORM\ORMInvalidArgumentException;
8
use Todo\Domain\Exception\TaskNotFoundException;
9
use Todo\Domain\Repository\TaskRepositoryInterface;
10
use Todo\Domain\Task;
11
12
/**
13
 * Class TaskRepository
14
 *
15
 * @category None
16
 * @package  Infrastructure\Persistence\DoctrineORM\Repository
17
 * @author   Martin Pham <[email protected]>
18
 * @license  None http://
19
 * @link     None
20
 */
21
class TaskRepository extends EntityRepository implements TaskRepositoryInterface
22
{
23
    /**
24
     * @inheritDoc
25
     */
26
    public function findAll(): array
27
    {
28
        return parent::findAll();
29
    }/** @noinspection PhpSignatureMismatchDuringInheritanceInspection */
30
31
    /**
32
     * @inheritDoc
33
     */
34
    public function find($id): Task
35
    {
36
        /** @var Task $task */
37
        $task = parent::find($id);
38
39
        if ($task === null) {
40
            throw new TaskNotFoundException("Cannot find task with id $id");
41
        }
42
43
        return $task;
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function findAllByStatus($status): array
50
    {
51
        return $this->findBy(
52
            [
53
            'status' => $status
54
            ]
55
        );
56
    }
57
58
    /**
59
     * @inheritDoc
60
     */
61
    public function findByName(string $name): Task
62
    {
63
        /** @var Task $task */
64
        $task = $this->findOneBy(
65
            [
66
                'name' => $name
67
            ]
68
        );
69
70
        if ($task === null) {
71
            throw new TaskNotFoundException("Cannot find task with name $name");
72
        }
73
74
        return $task;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80 View Code Duplication
    public function save(Task $task): bool
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...
81
    {
82
        try {
83
            $this->getEntityManager()->persist($task);
84
        } catch (ORMInvalidArgumentException $e) {
85
            return false;
86
        }
87
88
        try {
89
            $this->getEntityManager()->flush();
90
        } catch (OptimisticLockException $e) {
91
            return false;
92
        }
93
94
        return true;
95
    }
96
97
    /**
98
     * @inheritDoc
99
     */
100 View Code Duplication
    public function remove(Task $task): bool
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...
101
    {
102
        try {
103
            $this->getEntityManager()->remove($task);
104
        } catch (ORMInvalidArgumentException $e) {
105
            return false;
106
        }
107
108
        try {
109
            $this->getEntityManager()->flush();
110
        } catch (OptimisticLockException $e) {
111
            return false;
112
        }
113
114
        return true;
115
    }
116
117
    /**
118
     * @inheritDoc
119
     */
120
    public function removeByStatus($status): bool
121
    {
122
        $query = $this->getEntityManager()
123
            ->createQuery('DELETE FROM Domain\Task t WHERE t.status = :status');
124
        $query->setParameter('status', $status);
125
        $query->execute();
126
127
        return true;
128
    }
129
130
}
131