Completed
Pull Request — master (#14)
by Wachter
06:14
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 100%

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
rs 10
ccs 48
cts 48
cp 1

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