Completed
Push — develop ( cba78f...09bcdb )
by Nicolas
12:47
created

Pool::get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
3
namespace devtransition\jobbers;
4
5
use devtransition\jobbers\exception\DublciateException;
6
7
class Pool
8
{
9
10
    /**
11
     * @var Job[]
12
     */
13
    private $_jobs = [];
14
    private $_results = [];
15
16
    public function add(Job &$job)
17
    {
18
        if (isset($this->_jobs[$job->getId()])) {
19
            throw new DublciateException('job with given id already exists in pool');
20
        }
21
        $this->_jobs[$job->getId()] = $job;
22
        $this->_results[$job->getId()] = &$job->getResult();
23
        return true;
24
    }
25
26 View Code Duplication
    public function remove($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
    {
28
        if (!isset($this->_jobs[$id])) {
29
            throw new \InvalidArgumentException('id does not exist in pool');
30
        }
31
        unset($this->_jobs[$id]);
32
        return true;
33
    }
34
35 View Code Duplication
    public function &get($id = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
36
    {
37
        if ($id) {
38
            if (!isset($this->_jobs[$id])) {
39
                throw new \InvalidArgumentException('id does not exist in pool');
40
            }
41
            return $this->_results[$id];
42
        } else {
43
            // All
44
            return $this->_results;
45
        }
46
    }
47
48
    public function &run()
49
    {
50
        foreach ($this->_jobs as $id => $job) {
51
            $job->run();
52
        }
53
54
        return $this->_results;
55
    }
56
57
}