Completed
Pull Request — master (#14)
by Wachter
06:14
created

DoctrineStorage::__construct()   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 2
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 3
            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 3 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 3
        return array_map(
89 3
            function (TaskEntity $entity) {
90
                return $entity->getTask();
91 3
            },
92 3
            $this->taskRepository->findBy(['key' => $key], null, $limit)
93 3
        );
94
    }
95
96
    /**
97
     * {@inheritdoc}
98 6
     */
99
    public function persist(TaskInterface $task)
100 6
    {
101 6
        $entity = $this->taskRepository->findByUuid($task->getUuid());
102 6
        $this->setTask($entity, $task);
103
104 78
        $this->entityManager->persist($entity);
105
        $this->entityManager->flush();
106 78
    }
107 78
108 78
    /**
109 78
     * {@inheritdoc}
110 78
     */
111 78
    public function clear()
112
    {
113
        $this->taskRepository->deleteAll();
114
        $this->entityManager->flush();
115
    }
116
117
    private function setTask(TaskEntity $entity, TaskInterface $task)
118
    {
119
        $entity->setTask($task);
120
        $entity->setUuid($task->getUuid());
121
        $entity->setKey($task->getKey());
122
        $entity->setCompleted($task->isCompleted());
123
        $entity->setExecutionDate($task->getExecutionDate());
124
    }
125
}
126