Completed
Push — master ( 4035f4...400e5d )
by Daniel
02:15
created

TaskManager::updatePid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 2
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\ORM\EntityManagerInterface;
11
use GravityMedia\Commander\Entity\TaskEntity;
12
13
/**
14
 * Task manager class.
15
 *
16
 * @package GravityMedia\Commander
17
 */
18
class TaskManager
19
{
20
    /**
21
     * The entity manager.
22
     *
23
     * @var EntityManagerInterface
24
     */
25
    protected $entityManager;
26
27
    /**
28
     * Create task manager object.
29
     *
30
     * @param EntityManagerInterface $entityManager
31
     */
32
    public function __construct(EntityManagerInterface $entityManager)
33
    {
34
        $this->entityManager = $entityManager;
35
    }
36
37
    /**
38
     * Get all tasks.
39
     *
40
     * @param array $criteria
41
     *
42
     * @return Task[]
43
     */
44
    public function getTasks(array $criteria = [])
45
    {
46
        return array_map(function (TaskEntity $entity) {
47
            return new Task($this->entityManager, $entity);
48
        }, $this->entityManager->getRepository(TaskEntity::class)->findBy($criteria));
49
    }
50
51
    /**
52
     * Get single task by criteria.
53
     *
54
     * @param array $criteria
55
     *
56
     * @return null|Task
57
     */
58
    public function getTask(array $criteria)
59
    {
60
        /** @var TaskEntity $entity */
61
        $entity = $this->entityManager->getRepository(TaskEntity::class)->findOneBy($criteria);
62
        if (null === $entity) {
63
            return null;
64
        }
65
66
        return new Task($this->entityManager, $entity);
67
    }
68
69
    /**
70
     * Add task.
71
     *
72
     * @param string $script
73
     * @param int    $priority
74
     *
75
     * @return Task
76
     */
77
    public function addTask($script, $priority = TaskEntity::DEFAULT_PRIORITY)
78
    {
79
        $entity = new TaskEntity();
80
        $entity->setScript($script);
81
        $entity->setPriority($priority);
82
83
        $this->entityManager->persist($entity);
84
        $this->entityManager->flush();
85
86
        return new Task($this->entityManager, $entity);
87
    }
88
}
89