Test Failed
Push — master ( 810670...0a801e )
by Martin
02:32
created

TaskRepository::save()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 8.9197
c 0
b 0
f 0
cc 4
eloc 13
nc 6
nop 1
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  Todo\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
    public function save(Task $task): bool
81
    {
82
        if ($task->getCreatedAt() === null) {
83
            $task->setCreatedAt(new \DateTime());
84
        }
85
        $task->setUpdatedAt(new \DateTime());
86
87
        try {
88
            $this->getEntityManager()->persist($task);
89
        } catch (ORMInvalidArgumentException $e) {
90
            return false;
91
        }
92
93
        try {
94
            $this->getEntityManager()->flush();
95
        } catch (OptimisticLockException $e) {
96
            return false;
97
        }
98
99
100
        return true;
101
    }
102
103
    /**
104
     * @inheritDoc
105
     */
106
    public function remove(Task $task): bool
107
    {
108
        try {
109
            $this->getEntityManager()->remove($task);
110
        } catch (ORMInvalidArgumentException $e) {
111
            return false;
112
        }
113
114
        try {
115
            $this->getEntityManager()->flush();
116
        } catch (OptimisticLockException $e) {
117
            return false;
118
        }
119
120
        return true;
121
    }
122
123
    /**
124
     * @inheritDoc
125
     */
126
    public function removeByStatus($status): bool
127
    {
128
        $query = $this->getEntityManager()
129
            ->createQuery('DELETE FROM Domain\Task t WHERE t.status = :status');
130
        $query->setParameter('status', $status);
131
        $query->execute();
132
133
        return true;
134
    }
135
136
}
137