Task::remove()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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
13
/**
14
 * Task class.
15
 *
16
 * @package GravityMedia\Commander\Commander
17
 */
18
class Task
19
{
20
    /**
21
     * The entity manager.
22
     *
23
     * @var EntityManagerInterface
24
     */
25
    protected $entityManager;
26
27
    /**
28
     * The entity.
29
     *
30
     * @var TaskEntity
31
     */
32
    protected $entity;
33
34
    /**
35
     * Create task object.
36
     *
37
     * @param EntityManagerInterface $entityManager
38
     * @param TaskEntity             $entity
39
     */
40 8
    public function __construct(EntityManagerInterface $entityManager, TaskEntity $entity)
41
    {
42 8
        $this->entityManager = $entityManager;
43 8
        $this->entity = $entity;
44 8
    }
45
46
    /**
47
     * Get entity
48
     *
49
     * @return TaskEntity
50
     */
51 2
    public function getEntity()
52
    {
53 2
        return $this->entity;
54
    }
55
56
    /**
57
     * Prioritize task.
58
     *
59
     * @param int $priority
60
     *
61
     * @return $this
62
     */
63 2
    public function prioritize($priority)
64
    {
65 2
        $this->entity->setPriority($priority);
66 2
        $this->entityManager->flush();
67
68 2
        return $this;
69
    }
70
71
    /**
72
     * Defer task.
73
     *
74
     * @param int      $pid
75
     * @param callable $deferrer
76
     *
77
     * @return $this
78
     */
79 2
    public function defer($pid, $deferrer)
80
    {
81 2
        $connection = $this->entityManager->getConnection();
82
83 2
        $this->entity->setPid($pid);
84 2
        $this->entityManager->flush();
85
86 2
        $connection->close();
87 2
        $exitCode = $deferrer();
88 2
        $connection->connect();
89
90 2
        $this->entity->setExitCode($exitCode);
91 2
        $this->entityManager->flush();
92
93 2
        return $this;
94
    }
95
96
    /**
97
     * Remove task.
98
     *
99
     * @return $this
100
     */
101 2
    public function remove()
102
    {
103 2
        $this->entityManager->remove($this->entity);
104 2
        $this->entityManager->flush();
105
106 2
        return $this;
107
    }
108
}
109