Completed
Push — master ( 86fe2f...647445 )
by Marco
14:10
created

RequestManager::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 9

Duplication

Lines 11
Ratio 52.38 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 11
loc 21
rs 9.3142
cc 3
eloc 9
nc 3
nop 1
1
<?php namespace Comodojo\RpcClient\Components;
2
3
use \Comodojo\RpcClient\RpcRequest;
4
use \Exception;
5
6
/**
7
 * Protocol Trait
8
 *
9
 * @package     Comodojo Spare Parts
10
 * @author      Marco Giovinazzi <[email protected]>
11
 * @license     MIT
12
 *
13
 * LICENSE:
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 */
23
24
class RequestManager {
25
26
    private $requests = array();
27
28
    public function clean() {
29
30
        $this->requests = array();
31
32
        return $this;
33
34
    }
35
36
    public function add(RpcRequest $request) {
37
38
        // $uid = $request->getUniqueId();
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
39
40
        // $this->requests[$uid] = $request;
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
41
42
        $this->requests[] = $request;
43
44
        return $this;
45
46
    }
47
48
    public function get($uid = null) {
49
50 View Code Duplication
        if ( is_null($uid) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
51
52
            return $this->requests;
53
54
        } else if ( $key = $this->searchByUid($uid) != null ) {
55
56
            return $this->requests[$key];
57
58
        } else {
59
60
            return null;
61
62
        }
63
64
    }
65
66
    public function delete($uid = null) {
67
68
        if ( is_null($uid) ) {
69
70
            $this->requests = arrray();
71
72
            return true;
73
74 View Code Duplication
        } else if ( $key = $this->searchByUid($uid) != null ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
76
            unset($this->requests[$key]);
77
78
            return true;
79
80
        } else {
81
82
            return false;
83
84
        }
85
86
    }
87
88
    private function searchByUid($uid) {
0 ignored issues
show
Unused Code introduced by
The parameter $uid is not used and could be removed.

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

Loading history...
89
90
        $element = array_filter(
91
            $this->requests,
92
            function ($e) {
93
                return $e->getUid() == $uid;
0 ignored issues
show
Bug introduced by
The variable $uid 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...
94
            }
95
        );
96
97
        return sizeof($element) == 1 ? array_keys($element)[0] : null;
98
99
    }
100
101
}
102