Completed
Pull Request — master (#2)
by Wachter
03:01
created

DoctrineStorage::store()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 24
ccs 12
cts 12
cp 1
rs 8.9714
cc 3
eloc 11
nc 3
nop 1
crap 3
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 33
    public function __construct(EntityManagerInterface $entityManager, TaskRepository $taskRepository)
24
    {
25 33
        $this->entityManager = $entityManager;
26 33
        $this->taskRepository = $taskRepository;
27 33
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 30
    public function store(TaskInterface $task)
33
    {
34 30
        $entity = new TaskEntity();
35 30
36
        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 30
            $oldEntity = $this->taskRepository->findOneBy(
38 30
                [
39 30
                    '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
                    'completed' => false,
41
                ]
42
            );
43
44 6
            if ($oldEntity) {
45
                // TODO update task (warning execution date should not be changed)
46 6
47
                return;
48 6
            }
49 6
        }
50 6
51 4
        $this->setTask($entity, $task);
52
53
        $this->entityManager->persist($entity);
54
        $this->entityManager->flush();
55
    }
56
57 3
    /**
58
     * {@inheritdoc}
59 3
     */
60 3
    public function findScheduled()
61 3
    {
62 3
        return array_map(
63 3
            function (TaskEntity $entity) {
64 2
                return $entity->getTask();
65
            },
66
            $this->taskRepository->findScheduled()
67
        );
68
    }
69
70 3
    /**
71
     * {@inheritdoc}
72 3
     */
73 3
    public function findAll()
74
    {
75 3
        return array_map(
76 3
            function (TaskEntity $entity) {
77 3
                return $entity->getTask();
78
            },
79
            $this->taskRepository->findAll()
80
        );
81
    }
82 6
83
    /**
84 6
     * {@inheritdoc}
85 6
     */
86 6
    public function persist(TaskInterface $task)
87
    {
88 30
        $entity = $this->taskRepository->findByUuid($task->getUuid());
89
        $this->setTask($entity, $task);
90 30
91 30
        $this->entityManager->persist($entity);
92 30
        $this->entityManager->flush();
93 30
    }
94 30
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function clear()
99
    {
100
        $this->taskRepository->deleteAll();
101
        $this->entityManager->flush();
102
    }
103
104
    private function setTask(TaskEntity $entity, TaskInterface $task)
105
    {
106
        $entity->setTask($task);
107
        $entity->setUuid($task->getUuid());
108
        $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
        $entity->setCompleted($task->isCompleted());
110
        $entity->setExecutionDate($task->getExecutionDate());
111
    }
112
}
113