Completed
Push — master ( 058941...ab4289 )
by Wachter
05:40
created

DoctrineStorage::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace Task\TaskBundle\Storage;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Task\Storage\StorageInterface;
7
use Task\TaskBundle\Entity\Task as TaskEntity;
8
use Task\TaskBundle\Entity\TaskRepository;
9
use Task\TaskInterface;
10
11
class DoctrineStorage implements StorageInterface
12
{
13
    /**
14
     * @var EntityManagerInterface
15
     */
16
    private $entityManager;
17
18
    /**
19
     * @var TaskRepository
20
     */
21
    private $taskRepository;
22
23 105
    public function __construct(EntityManagerInterface $entityManager, TaskRepository $taskRepository)
24
    {
25 105
        $this->entityManager = $entityManager;
26 105
        $this->taskRepository = $taskRepository;
27 105
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 102
    public function store(TaskInterface $task)
33
    {
34 102
        $entity = new TaskEntity();
35
36 102
        if ($task->getKey()) {
37 48
            $oldEntity = $this->taskRepository->findOneBy(
38
                [
39 48
                    'key' => $task->getKey(),
40 32
                    'completed' => false,
41
                ]
42 32
            );
43
44 48
            if ($oldEntity) {
45
                // TODO update task (warning execution date should not be changed)
46
47 24
                return;
48
            }
49 16
        }
50
51 78
        $this->setTask($entity, $task);
52
53 78
        $this->entityManager->persist($entity);
54 78
        $this->entityManager->flush();
55 78
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 6
    public function findScheduled()
61
    {
62 6
        return array_map(
63
            function (TaskEntity $entity) {
64 6
                return $entity->getTask();
65 6
            },
66 6
            $this->taskRepository->findScheduled()
67 4
        );
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 3 View Code Duplication
    public function findAll($limit = 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...
74
    {
75 3
        return array_map(
76
            function (TaskEntity $entity) {
77 3
                return $entity->getTask();
78 3
            },
79 3
            $this->taskRepository->findBy([], null, $limit)
80 2
        );
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 View Code Duplication
    public function findByKey($key, $limit = 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...
87
    {
88
        return array_map(
89
            function (TaskEntity $entity) {
90
                return $entity->getTask();
91
            },
92
            $this->taskRepository->findBy(['key' => $key], null, $limit)
93
        );
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 3
    public function persist(TaskInterface $task)
100
    {
101 3
        $entity = $this->taskRepository->findByUuid($task->getUuid());
102 3
        $this->setTask($entity, $task);
103
104 3
        $this->entityManager->persist($entity);
105 3
        $this->entityManager->flush();
106 3
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 6
    public function clear()
112
    {
113 6
        $this->taskRepository->deleteAll();
114 6
        $this->entityManager->flush();
115 6
    }
116
117 78
    private function setTask(TaskEntity $entity, TaskInterface $task)
118
    {
119 78
        $entity->setTask(clone $task);
120 78
        $entity->setUuid($task->getUuid());
121 78
        $entity->setKey($task->getKey());
122 78
        $entity->setCompleted($task->isCompleted());
123 78
        $entity->setExecutionDate($task->getExecutionDate());
124 78
    }
125
}
126