Completed
Push — feature/add-set ( f483fa )
by jake
03:25
created

Task::getCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * Purple - Run tasks on collections
4
 *
5
 * PHP version 5
6
 *
7
 * Copyright (C) 2016 Jake Johns
8
 *
9
 * This software may be modified and distributed under the terms
10
 * of the MIT license.  See the LICENSE file for details.
11
 *
12
 * @category  Task
13
 * @package   Jnjxp\Purple
14
 * @author    Jake Johns <[email protected]>
15
 * @copyright 2016 Jake Johns
16
 * @license   http://jnj.mit-license.org/2016 MIT License
17
 * @link      https://github.com/jnjxp/jnjxp.purple
18
 */
19
20
namespace Jnjxp\Purple;
21
22
/**
23
 * Task
24
 *
25
 * @category Task
26
 * @package  Jnjxp\Purple
27
 * @author   Jake Johns <[email protected]>
28
 * @license  http://jnj.mit-license.org/ MIT License
29
 * @link     https://github.com/jnjxp/jnjxp.purple
30
 *
31
 * @see TaskInterface
32
 */
33
class Task implements TaskInterface
34
{
35
    /**
36
     * Collection
37
     *
38
     * @var mixed
39
     *
40
     * @access protected
41
     */
42
    protected $collection;
43
44
    /**
45
     * Item tasks
46
     *
47
     * @var QueueInterface
48
     *
49
     * @access protected
50
     */
51
    protected $itemTasks;
52
53
    /**
54
     * Collection tasks
55
     *
56
     * @var QueueInterface
57
     *
58
     * @access protected
59
     */
60
    protected $collectionTasks;
61
62
    /**
63
     * __construct
64
     *
65
     * @param mixed          $collection          DESCRIPTION
66
     * @param QueueInterface $itemTaskQueue       DESCRIPTION
67
     * @param QueueInterface $collectionTaskQueue DESCRIPTION
68
     *
69
     * @access public
70
     */
71 3
    public function __construct(
72
        $collection,
73
        QueueInterface $itemTaskQueue = null,
74
        QueueInterface $collectionTaskQueue = null
75
    ) {
76 3
        $this->collection = $collection;
77 3
        $this->itemTasks = $itemTaskQueue ?: new Queue;
78 3
        $this->collectionTasks = $collectionTaskQueue ?: new Queue;
79 3
    }
80
81
    /**
82
     * Add an item task
83
     *
84
     * @param mixed $task     task or task spec
85
     * @param int   $priority priority of task
86
     *
87
     * @return $this
88
     *
89
     * @access public
90
     */
91 3
    public function each($task, $priority = 1000)
92
    {
93 3
        $this->itemTasks->insert($task, $priority);
94 3
        return $this;
95
    }
96
97
    /**
98
     * Add Item Tasks
99
     *
100
     * @param mixed $tasks    tasks to add
101
     * @param int   $priority task priority
102
     *
103
     * @return $this
104
     *
105
     * @access public
106
     */
107 1
    public function addItemTasks($tasks, $priority = 1000)
108
    {
109 1
        foreach ($tasks as $task) {
110 1
            $this->each($task, $priority);
111
        }
112 1
        return $this;
113
    }
114
115
    /**
116
     * Set Item Tasks
117
     *
118
     * @param mixed $tasks    Tasks to set
119
     * @param int   $priority Task priority
120
     *
121
     * @return mixed
122
     * @throws exceptionclass [description]
123
     *
124
     * @access public
125
     */
126 1
    public function setItemTasks($tasks, $priority = 1000)
127
    {
128 1
        $this->itemTasks->clear();
129 1
        return $this->addItemTasks($tasks, $priority);
130
    }
131
132
    /**
133
     * Add a collection task
134
     *
135
     * @param mixed $task     task
136
     * @param int   $priority order priority
137
     *
138
     * @return $this
139
     *
140
     * @access public
141
     */
142 3
    public function all($task, $priority = 1000)
143
    {
144 3
        $this->collectionTasks->insert($task, $priority);
145 3
        return $this;
146
    }
147
148
    /**
149
     * Add Collection Tasks
150
     *
151
     * @param mixed $tasks    tasks to add
152
     * @param int   $priority task priority
153
     *
154
     * @return $this
155
     *
156
     * @access public
157
     */
158 1
    public function addCollectionTasks($tasks, $priority = 1000)
159
    {
160 1
        foreach ($tasks as $task) {
161 1
            $this->all($task, $priority);
162
        }
163 1
        return $this;
164
    }
165
166
    /**
167
     * Set Collection Tasks
168
     *
169
     * @param mixed $tasks    tasks to set
170
     * @param int   $priority priority of tasks
171
     *
172
     * @return mixed
173
     *
174
     * @access public
175
     */
176 1
    public function setCollectionTasks($tasks, $priority = 1000)
177
    {
178 1
        $this->collectionTasks->clear();
179 1
        return $this->addCollectionTasks($tasks, $priority);
180
    }
181
182
    /**
183
     * Get collection
184
     *
185
     * @return mixed
186
     *
187
     * @access public
188
     */
189 2
    public function getCollection()
190
    {
191 2
        return $this->collection;
192
    }
193
194
    /**
195
     * Get item tasks
196
     *
197
     * @return QueueInterface
198
     *
199
     * @access public
200
     */
201 3
    public function getItemTasks()
202
    {
203 3
        return $this->itemTasks;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->itemTasks; (Jnjxp\Purple\QueueInterface) is incompatible with the return type declared by the interface Jnjxp\Purple\TaskInterface::getItemTasks of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
204
    }
205
206
    /**
207
     * Get collection tasks
208
     *
209
     * @return QueueInterface
210
     *
211
     * @access public
212
     */
213 3
    public function getCollectionTasks()
214
    {
215 3
        return $this->collectionTasks;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->collectionTasks; (Jnjxp\Purple\QueueInterface) is incompatible with the return type declared by the interface Jnjxp\Purple\TaskInterface::getCollectionTasks of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
216
    }
217
}
218