Completed
Push — master ( 400e5d...38df09 )
by Daniel
02:19
created

TaskManager::getRepository()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 6
1
<?php
2
/**
3
 * This file is part of the Commander project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Commander;
9
10
use Doctrine\Common\Persistence\ObjectRepository;
11
use Doctrine\ORM\EntityManagerInterface;
12
use GravityMedia\Commander\Entity\TaskEntity;
13
14
/**
15
 * Task manager class.
16
 *
17
 * @package GravityMedia\Commander
18
 */
19
class TaskManager
20
{
21
    /**
22
     * The entity manager.
23
     *
24
     * @var EntityManagerInterface
25
     */
26
    protected $entityManager;
27
28
    /**
29
     * The repository.
30
     *
31
     * @var ObjectRepository
32
     */
33
    protected $repository;
34
35
    /**
36
     * Create task manager object.
37
     *
38
     * @param EntityManagerInterface $entityManager
39
     */
40
    public function __construct(EntityManagerInterface $entityManager)
41
    {
42
        $this->entityManager = $entityManager;
43
    }
44
45
    /**
46
     * Get repository.
47
     *
48
     * @return ObjectRepository
49
     */
50
    public function getRepository()
51
    {
52
        if (null === $this->repository) {
53
            $this->repository = $this->entityManager->getRepository(TaskEntity::class);
54
        }
55
56
        return $this->repository;
57
    }
58
59
    /**
60
     * Get all tasks.
61
     *
62
     * @param array $criteria
63
     *
64
     * @return Task[]
65
     */
66
    public function getTasks(array $criteria = [])
67
    {
68
        return array_map(function (TaskEntity $entity) {
69
            return new Task($this->entityManager, $entity);
70
        }, $this->getRepository()->findBy($criteria));
71
    }
72
73
    /**
74
     * Get single task by criteria.
75
     *
76
     * @param array $criteria
77
     *
78
     * @return null|Task
79
     */
80
    public function getTask(array $criteria)
81
    {
82
        /** @var TaskEntity $entity */
83
        $entity = $this->getRepository()->findOneBy($criteria);
84
        if (null === $entity) {
85
            return null;
86
        }
87
88
        return new Task($this->entityManager, $entity);
89
    }
90
91
    /**
92
     * Add task.
93
     *
94
     * @param string $script
95
     * @param int    $priority
96
     *
97
     * @return Task
98
     */
99
    public function addTask($script, $priority = TaskEntity::DEFAULT_PRIORITY)
100
    {
101
        $entity = new TaskEntity();
102
        $entity->setScript($script);
103
        $entity->setPriority($priority);
104
105
        $this->entityManager->persist($entity);
106
        $this->entityManager->flush();
107
108
        return new Task($this->entityManager, $entity);
109
    }
110
}
111