TaskManager::mapEntities()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
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\Commander;
9
10
use Doctrine\ORM\EntityManagerInterface;
11
use GravityMedia\Commander\ORM\TaskEntity;
12
use GravityMedia\Commander\ORM\TaskEntityRepository;
13
14
/**
15
 * Task manager class.
16
 *
17
 * @package GravityMedia\Commander\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 TaskEntityRepository
32
     */
33
    protected $repository;
34
35
    /**
36
     * Create task manager object.
37
     *
38
     * @param EntityManagerInterface $entityManager
39
     */
40 14
    public function __construct(EntityManagerInterface $entityManager)
41
    {
42 14
        $this->entityManager = $entityManager;
43 14
    }
44
45
    /**
46
     * Get repository.
47
     *
48
     * @return TaskEntityRepository
49
     */
50 10
    public function getRepository()
51
    {
52 10
        if (null === $this->repository) {
53 10
            $this->repository = $this->entityManager->getRepository(TaskEntity::class);
54 5
        }
55
56 10
        return $this->repository;
57
    }
58
59
    /**
60
     * Map entities.
61
     *
62
     * @param TaskEntity[] $entities
63
     *
64
     * @return Task[]
65
     */
66
    protected function mapEntities(array $entities)
67
    {
68 4
        return array_map(function (TaskEntity $entity) {
69 4
            return new Task($this->entityManager, $entity);
70 4
        }, $entities);
71
    }
72
73
    /**
74
     * Find all tasks.
75
     *
76
     * @return Task[]
77
     */
78 2
    public function findAllTasks()
79
    {
80 2
        return $this->mapEntities($this->getRepository()->findAll());
81
    }
82
83
    /**
84
     * Find all terminated tasks.
85
     *
86
     * @return Task[]
87
     */
88 2
    public function findAllTerminatedTasks()
89
    {
90 2
        return $this->mapEntities($this->getRepository()->findAllWithNoExitCode());
91
    }
92
93
    /**
94
     * Find next task.
95
     *
96
     * @param array $criteria
97
     *
98
     * @return Task|null
99
     */
100 4
    public function findNextTask(array $criteria = [])
101
    {
102 4
        $entity = $this->getRepository()->findNext($criteria);
103 4
        if (null === $entity) {
104 2
            return null;
105
        }
106
107 2
        return new Task($this->entityManager, $entity);
108
    }
109
110
    /**
111
     * Create and persist new task.
112
     *
113
     * @param string   $commandline
114
     * @param int|null $priority
115
     *
116
     * @return Task
117
     */
118 4
    public function newTask($commandline, $priority = null)
119
    {
120 4
        if (null === $priority) {
121 2
            $priority = TaskEntity::DEFAULT_PRIORITY;
122 1
        }
123
124 4
        $entity = new TaskEntity();
125 4
        $entity->setCommandline($commandline);
126 4
        $entity->setPriority($priority);
127
128 4
        $this->entityManager->persist($entity);
129 4
        $this->entityManager->flush();
130
131 4
        return new Task($this->entityManager, $entity);
132
    }
133
}
134