Completed
Push — 2.0 ( 76e968...4129d9 )
by Marco
13:01
created

Table::bulk()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.439
c 0
b 0
f 0
cc 5
eloc 12
nc 3
nop 1
1
<?php namespace Comodojo\Extender\Task;
2
3
use \Comodojo\Foundation\Base\Configuration;
4
use \Comodojo\Foundation\Events\Manager as EventsManager;
5
use \Comodojo\Foundation\DataAccess\ArrayAccessTrait;
6
use \Comodojo\Foundation\DataAccess\IteratorTrait;
7
use \Comodojo\Foundation\DataAccess\CountableTrait;
8
use \Comodojo\Daemon\Traits\LoggerTrait;
9
use \Comodojo\Daemon\Traits\EventsTrait;
10
use \Comodojo\Extender\Traits\ConfigurationTrait;
11
use \Psr\Log\LoggerInterface;
12
use \Iterator;
13
use \ArrayAccess;
14
use \Countable;
15
use \Exception;
16
17
/**
18
* @package     Comodojo Extender
19
* @author      Marco Giovinazzi <[email protected]>
20
* @license     MIT
21
*
22
* LICENSE:
23
*
24
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
* THE SOFTWARE.
31
 */
32
33
34
class Table implements Iterator, ArrayAccess, Countable {
35
36
    use ArrayAccessTrait;
37
    use IteratorTrait;
38
    use CountableTrait;
39
    use LoggerTrait;
40
    use ConfigurationTrait;
41
    use EventsTrait;
42
43
    /**
44
     * @var array
45
     */
46
    private $data = [];
47
48
    public function __construct(
49
        Configuration $configuration,
50
        LoggerInterface $logger,
51
        EventsManager $events
52
    ) {
53
54
        $this->setConfiguration($configuration);
55
        $this->setLogger($logger);
56
        $this->setEvents($events);
57
58
    }
59
60
    /**
61
     * Get the task item
62
     *
63
     * @param string $name
64
     * @return TaskItem|null
65
     */
66
    public function get($name) {
67
68
        if ( array_key_exists($name, $this->data) ) {
69
            return $this->data[$name];
70
        }
71
72
        return null;
73
74
    }
75
76
    /**
77
     * Add a new task to table
78
     *
79
     * @param string $name
80
     * @param string $class
81
     * @param string $description
82
     * @return bool
83
     */
84
    public function add($name, $class, $description = null) {
85
86
        if ( array_key_exists($name, $this->data) ) {
87
            $this->logger->warning("Skipping duplicate task $name ($class)");
88
            return false;
89
        }
90
91
        if ( empty($name) || empty($class) || !class_exists($class) ) {
92
            $this->logger->warning("Skipping invalid task definition", array(
93
                "NAME"       => $name,
94
                "CLASS"      => $class,
95
                "DESCRIPTION"=> $description
96
            ));
97
            return false;
98
        }
99
100
        $this->data[$name] = new TaskItem(
101
            $this->getConfiguration(),
102
            $this->getEvents(),
103
            $this->getLogger(),
104
            $name,
105
            $class,
106
            $description
107
        );
108
109
        $this->logger->debug("Task $name ($class) in table");
110
111
        return true;
112
113
    }
114
115
    /**
116
     * Delete a task from table
117
     *
118
     * @param string $name
119
     * @return bool
120
     */
121
    public function delete($name) {
122
123
        if ( array_key_exists($name, $this->data) ) {
124
            unset($this->data[$name]);
125
            return true;
126
        }
127
128
        return false;
129
130
    }
131
132
    /**
133
     * Load a bulk task list into the table
134
     *
135
     * @param array $tasks
136
     * @return bool
137
     */
138
    public function addBulk(array $tasks) {
139
140
        $result = [];
141
142
        foreach($tasks as $task) {
143
144
            if ( empty($task['name']) || empty($task['class']) ) {
145
146
                $this->logger->warning("Skipping invalid task definition", array(
147
                    "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...
148
                    "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...
149
                    "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...
150
                ));
151
                $result[] = false;
152
153
            } else {
154
155
                $result[] = $this->add($task['name'], $task['class'], empty($task['description']) ? null : $task['description']);
156
157
            }
158
159
        }
160
161
        return $result;
162
163
    }
164
165
    public static function create(
166
        Configuration $configuration,
167
        LoggerInterface $logger,
168
        EventsManager $events
169
    ) {
170
171
        return new Table($configuration, $logger, $events);
172
173
    }
174
175
}
176