Completed
Push — master ( 32c328...253a84 )
by Valentin
15s queued 11s
created

Job::addTask()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
4
namespace Pheanstalk\Structure;
5
6
use Doctrine\Common\Collections\ArrayCollection;
7
8
class Job
9
{
10
    /** @var ArrayCollection[Task] */
0 ignored issues
show
Documentation Bug introduced by
The doc comment ArrayCollection[Task] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
11
    private $tasks;
12
13
    /**
14
     * Job constructor.
15
     *
16
     * @param ArrayCollection[Task] $tasks
0 ignored issues
show
Documentation Bug introduced by
The doc comment ArrayCollection[Task] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
17
     */
18 15
    public function __construct(ArrayCollection $tasks)
19
    {
20 15
        $this->setTasks($tasks);
21
    }
22
23
    /**
24
     * @return ArrayCollection
25
     */
26 8
    public function getTasks(): ArrayCollection
27
    {
28 8
        return $this->tasks;
29
    }
30
31
    /**
32
     * @param ArrayCollection $tasks
33
     *
34
     * @return Job
35
     */
36 15
    public function setTasks(ArrayCollection $tasks): Job
37
    {
38
        $this->tasks = $tasks->filter(function(Task $task) {
0 ignored issues
show
Unused Code introduced by
The parameter $task is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

38
        $this->tasks = $tasks->filter(function(/** @scrutinizer ignore-unused */ Task $task) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39 15
            return true;
40 15
        });
41 15
        return $this;
42
    }
43
44
    /**
45
     * @param Task $task
46
     *
47
     * @return Job
48
     */
49 1
    public function addTask(Task $task): Job
50
    {
51 1
        $this->tasks[] = $task;
52 1
        return $this;
53
    }
54
55
    /**
56
     * @param Task $task
57
     *
58
     * @return Job
59
     */
60 1
    public function removeTask(Task $task): Job
61
    {
62 1
        $this->tasks->removeElement($task);
63 1
        return $this;
64
    }
65
66
    /**
67
     * @return \DOMDocument
68
     * @throws \ReflectionException
69
     */
70 7
    public function getXml()
71
    {
72 7
        $dom = new \DOMDocument("1.0", "utf-8");
73 7
        $root = $dom->createElement("job");
74 7
        $tasks = $dom->createElement("tasks");
75
        /** @var Task $task */
76 7
        foreach ($this->getTasks() as $task) {
77 7
            $taskItem = $dom->createElement("task");
0 ignored issues
show
Unused Code introduced by
The assignment to $taskItem is dead and can be removed.
Loading history...
78 7
            $taskNode = $task->getXml()->getElementsByTagName('task')->item(0);
79 7
            $tasks->appendChild($dom->importNode($taskNode, true));
80
        }
81 7
        $root->appendChild($tasks);
82 7
        $dom->appendChild($root);
83 7
        return $dom;
84
    }
85
}
86