Completed
Pull Request — master (#15)
by Wachter
10:44
created

TaskRepository::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of php-task library.
5
 *
6
 * (c) php-task
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Task\TaskBundle\DoctrineStorage;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Task\Storage\TaskRepositoryInterface;
16
use Task\TaskBundle\Entity\TaskRepository as ORMTaskRepository;
17
use Task\TaskInterface;
18
19
/**
20
 * Task storage which uses doctrine.
21
 */
22
class TaskRepository implements TaskRepositoryInterface
23
{
24
    /**
25
     * @var ObjectManager
26
     */
27
    private $objectManager;
28
29
    /**
30
     * @var ORMTaskRepository
31
     */
32
    private $taskRepository;
33
34
    /**
35
     * @param ObjectManager $objectManager
36
     * @param ORMTaskRepository $taskRepository
37
     */
38
    public function __construct(ObjectManager $objectManager, ORMTaskRepository $taskRepository)
39
    {
40
        $this->objectManager = $objectManager;
41
        $this->taskRepository = $taskRepository;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function store(TaskInterface $task)
48
    {
49
        $this->objectManager->persist($task);
50
51
        // FIXME move this flush to somewhere else (:
52
        $this->objectManager->flush();
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function findAll($limit = null)
59
    {
60
        return $this->taskRepository->findBy([], null, $limit);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function findEndBeforeNow()
67
    {
68
        return $this->taskRepository->findEndBefore(new \DateTime());
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function clear()
75
    {
76
        foreach ($this->taskRepository->findAll() as $task) {
77
            $this->objectManager->remove($task);
78
        }
79
80
        // FIXME move this flush to somewhere else (:
81
        $this->objectManager->flush();
82
    }
83
}
84