Completed
Pull Request — master (#2)
by Wachter
05:57
created

DoctrineStorage::store()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3

Importance

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