TaskRepository::save()   B
last analyzed

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\Application\Task\Exception\TaskCannotBeRemovedException;
9
use Todo\Application\Task\Exception\TaskCannotBeSavedException;
10
use Todo\Domain\Exception\TaskNotFoundException;
11
use Todo\Domain\Repository\TaskRepositoryInterface;
12
use Todo\Domain\Task;
13
14
/**
15
 * Class TaskRepository
16
 *
17
 * @category None
18
 * @package  Todo\Infrastructure\Persistence\DoctrineORM\Repository
19
 * @author   Martin Pham <[email protected]>
20
 * @license  None http://
21
 * @link     None
22
 */
23
class TaskRepository extends EntityRepository implements TaskRepositoryInterface
24
{
25
    /**
26
     * @inheritDoc
27
     */
28
    public function findAll(): array
29
    {
30
        return parent::findAll();
31
    }/** @noinspection PhpSignatureMismatchDuringInheritanceInspection */
32
33
    /**
34
     * @inheritDoc
35
     */
36
    public function find($id): Task
37
    {
38
        /** @var Task $task */
39
        $task = parent::find($id);
40
41
        if ($task === null) {
42
            throw new TaskNotFoundException("Cannot find task with id $id");
43
        }
44
45
        return $task;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51
    public function findAllByStatus($status): array
52
    {
53
        return $this->findBy(
54
            [
55
            'status' => $status
56
            ]
57
        );
58
    }
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function findByName(string $name): Task
64
    {
65
        /** @var Task $task */
66
        $task = $this->findOneBy(
67
            [
68
                'name' => $name
69
            ]
70
        );
71
72
        if ($task === null) {
73
            throw new TaskNotFoundException("Cannot find task with name $name");
74
        }
75
76
        return $task;
77
    }
78
79
    /**
80
     * @inheritDoc
81
     */
82
    public function save(Task $task): bool
83
    {
84
        if ($task->getCreatedAt() === null) {
85
            $task->setCreatedAt(new \DateTime());
86
        }
87
        $task->setUpdatedAt(new \DateTime());
88
89
        try {
90
            $this->getEntityManager()->persist($task);
91
        } catch (ORMInvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\ORMInvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
92
            throw new TaskCannotBeSavedException($e->getMessage());
93
        }
94
95
        try {
96
            $this->getEntityManager()->flush();
97
        } catch (OptimisticLockException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\OptimisticLockException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
            throw new TaskCannotBeSavedException($e->getMessage());
99
        }
100
101
102
        return true;
103
    }
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function remove(Task $task): bool
109
    {
110
        try {
111
            $this->getEntityManager()->remove($task);
112
        } catch (ORMInvalidArgumentException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\ORMInvalidArgumentException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
113
            throw new TaskCannotBeRemovedException($e->getMessage());
114
        }
115
116
        try {
117
            $this->getEntityManager()->flush();
118
        } catch (OptimisticLockException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\ORM\OptimisticLockException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
119
            throw new TaskCannotBeRemovedException($e->getMessage());
120
        }
121
122
        return true;
123
    }
124
125
    /**
126
     * @inheritDoc
127
     */
128
    public function removeByStatus($status): bool
129
    {
130
        $query = $this->getEntityManager()
131
            ->createQuery('DELETE FROM Todo\Domain\Task t WHERE t.status = :status');
132
        $query->setParameter('status', $status);
133
        $query->execute();
134
135
        return true;
136
    }
137
138
}
139