Completed
Push — master ( 8a5d0f...f400e4 )
by Wachter
23:12
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()) {
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37 48
            $oldEntity = $this->taskRepository->findOneBy(
38
                [
39 48
                    'key' => $task->getKey(),
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
    public function findAll()
74
    {
75 3
        return array_map(
76 3
            function (TaskEntity $entity) {
77 3
                return $entity->getTask();
78 3
            },
79 3
            $this->taskRepository->findAll()
80 2
        );
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 3
    public function persist(TaskInterface $task)
87
    {
88 3
        $entity = $this->taskRepository->findByUuid($task->getUuid());
89 3
        $this->setTask($entity, $task);
90
91 3
        $this->entityManager->persist($entity);
92 3
        $this->entityManager->flush();
93 3
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 6
    public function clear()
99
    {
100 6
        $this->taskRepository->deleteAll();
101 6
        $this->entityManager->flush();
102 6
    }
103
104 78
    private function setTask(TaskEntity $entity, TaskInterface $task)
105
    {
106 78
        $entity->setTask($task);
107 78
        $entity->setUuid($task->getUuid());
108 78
        $entity->setKey($task->getKey());
0 ignored issues
show
Bug introduced by
The method getKey() does not seem to exist on object<Task\TaskInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109 78
        $entity->setCompleted($task->isCompleted());
110 78
        $entity->setExecutionDate($task->getExecutionDate());
111 78
    }
112
}
113