Completed
Push — 2.0 ( 2c7009...c833bc )
by Marco
03:23
created

Table   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 87
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 9 2
A add() 0 21 4
A delete() 0 10 2
B bulk() 0 26 5
1
<?php namespace Comodojo\Extender\Tasks;
2
3
use \Comodojo\Extender\Components\ArrayAccess as ArrayAccessTrait;
4
use \Comodojo\Extender\Components\Iterator as IteratorTrait;
5
use \Comodojo\Extender\Components\Countable as CountableTrait;
6
use \Comodojo\Dispatcher\Components\DataAccess as DataAccessTrait;
7
use \Psr\Log\LoggerInterface;
8
use \Iterator;
9
use \ArrayAccess;
10
use \Countable;
11
12
/**
13
 * @package     Comodojo Dispatcher
14
 * @author      Marco Giovinazzi <[email protected]>
15
 * @author      Marco Castiello <[email protected]>
16
 * @license     GPL-3.0+
17
 *
18
 * LICENSE:
19
 *
20
 * This program is free software: you can redistribute it and/or modify
21
 * it under the terms of the GNU Affero General Public License as
22
 * published by the Free Software Foundation, either version 3 of the
23
 * License, or (at your option) any later version.
24
 *
25
 * This program is distributed in the hope that it will be useful,
26
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28
 * GNU Affero General Public License for more details.
29
 *
30
 * You should have received a copy of the GNU Affero General Public License
31
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32
 */
33
34
35
class Table implements Iterator, ArrayAccess, Countable {
36
37
    use ArrayAccessTrait;
38
    use IteratorTrait;
39
    use CountableTrait;
40
41
    private $data = array();
42
43
    private $logger;
44
45
    public function __construct(LoggerInterface $logger) {
46
47
        $this->logger = $logger;
48
49
    }
50
51
    public function get($name) {
52
53
        if ( array_key_exists($name, $this->data) ) {
54
            return $this->data[$name];
55
        }
56
57
        return null;
58
59
    }
60
61
    public function add($name, $class, $description = null) {
62
63
        if ( array_key_exists($name, $this->data) ) {
64
            $this->logger->warning("Skipping duplicate task $name ($class)");
65
            return false;
66
        }
67
68
        if ( empty($name) || empty($class) ) {
69
            $this->logger->warning("Skipping invalid task definition", array(
70
                "NAME"       => $name,
71
                "CLASS"      => $class,
72
                "DESCRIPTION"=> $description
73
            ));
74
            return false;
75
        }
76
77
        $this->data[$name] = new Task($name, $class, $description);
78
79
        return true;
80
81
    }
82
83
    public function delete($name) {
84
85
        if ( array_key_exists($name, $this->data) ) {
86
            unset($this->data[$name]);
87
            return true;
88
        }
89
90
        return false;
91
92
    }
93
94
    public function bulk($tasks) {
95
96
        $result = array();
97
98
        foreach($tasks as $task) {
99
100
            if ( empty($task['name']) || empty($task['class']) ) {
101
102
                $this->logger->warning("Skipping invalid task definition", array(
103
                    "NAME"       => $name,
0 ignored issues
show
Bug introduced by
The variable $name does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
104
                    "CLASS"      => $class,
0 ignored issues
show
Bug introduced by
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
105
                    "DESCRIPTION"=> $description
0 ignored issues
show
Bug introduced by
The variable $description does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
106
                ));
107
                $result[] = false;
108
109
            } else {
110
111
                $result[] = $this->add($task['name'], $task['class'], empty($task['description']) ? null : $task['description']);
112
113
            }
114
115
        }
116
117
        return $result;
118
119
    }
120
121
}
122