Completed
Push — master ( 33e149...329666 )
by Wachter
09:05
created

DoctrineStorage   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
c 2
b 0
f 2
lcom 1
cbo 4
dl 0
loc 85
ccs 39
cts 39
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A store() 0 8 1
A findScheduled() 0 9 1
A findAll() 0 9 1
A persist() 0 8 1
A clear() 0 5 1
A setTask() 0 7 1
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
        $this->setTask($entity, $task);
36
37 30
        $this->entityManager->persist($entity);
38 30
        $this->entityManager->flush();
39 30
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 6
    public function findScheduled()
45
    {
46 6
        return array_map(
47
            function (TaskEntity $entity) {
48 6
                return $entity->getTask();
49 6
            },
50 6
            $this->taskRepository->findScheduled()
51 4
        );
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57 3
    public function findAll()
58
    {
59 3
        return array_map(
60 3
            function (TaskEntity $entity) {
61 3
                return $entity->getTask();
62 3
            },
63 3
            $this->taskRepository->findAll()
64 2
        );
65
    }
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 3
    public function persist(TaskInterface $task)
71
    {
72 3
        $entity = $this->taskRepository->findByUuid($task->getUuid());
73 3
        $this->setTask($entity, $task);
74
75 3
        $this->entityManager->persist($entity);
76 3
        $this->entityManager->flush();
77 3
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82 6
    public function clear()
83
    {
84 6
        $this->taskRepository->deleteAll();
85 6
        $this->entityManager->flush();
86 6
    }
87
88 30
    private function setTask(TaskEntity $entity, TaskInterface $task)
89
    {
90 30
        $entity->setTask($task);
91 30
        $entity->setUuid($task->getUuid());
92 30
        $entity->setCompleted($task->isCompleted());
93 30
        $entity->setExecutionDate($task->getExecutionDate());
94 30
    }
95
}
96