Completed
Push — master ( 058941...ab4289 )
by Wachter
05:40
created

DoctrineStorage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 15.65 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 87.04%

Importance

Changes 4
Bugs 0 Features 4
Metric Value
wmc 10
c 4
b 0
f 4
lcom 1
cbo 4
dl 18
loc 115
ccs 47
cts 54
cp 0.8704
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B store() 0 24 3
A findScheduled() 0 9 1
A findAll() 9 9 1
A findByKey() 9 9 1
A persist() 0 8 1
A clear() 0 5 1
A setTask() 0 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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