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

TaskManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 7
c 5
b 0
f 3
lcom 1
cbo 4
dl 0
loc 92
ccs 0
cts 23
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRepository() 0 8 2
A getTasks() 0 6 1
A getTask() 0 10 2
A addTask() 0 11 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;
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