Passed
Push — master ( 00f43d...a0d391 )
by Malte
02:56
created

Mask   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 13

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __set() 0 4 1
A __get() 0 6 2
A getAttributes() 0 2 1
A getParent() 0 2 1
A __call() 0 24 6
A __construct() 0 5 2
1
<?php
2
/*
3
* File: Mask.php
4
* Category: Mask
5
* Author: M.Goldenbaum
6
* Created: 14.03.19 20:49
7
* Updated: -
8
*
9
* Description:
10
*  -
11
*/
12
13
namespace Webklex\IMAP\Support\Masks;
14
15
use Webklex\IMAP\Exceptions\MethodNotFoundException;
16
17
/**
18
 * Class Mask
19
 *
20
 * @package Webklex\IMAP\Support\Masks
21
 */
22
class Mask {
23
24
    /**
25
     * @var array $attributes
26
     */
27
    protected $attributes = [];
28
29
    /**
30
     * @var object $parent
31
     */
32
    protected $parent;
33
34
    /**
35
     * Mask constructor.
36
     * @param $parent
37
     */
38
    public function __construct($parent) {
39
        $this->parent = $parent;
40
41
        if(method_exists($this->parent, 'getAttributes')){
42
            $this->attributes = array_merge($this->attributes, $this->parent->getAttributes());
43
        }
44
    }
45
46
47
    /**
48
     * Call dynamic attribute setter and getter methods and inherit the parent calls
49
     * @param string $method
50
     * @param array $arguments
51
     *
52
     * @return mixed
53
     * @throws MethodNotFoundException
54
     */
55
    public function __call($method, $arguments) {
56
        if(strtolower(substr($method, 0, 3)) === 'get') {
57
            $name = snake_case(substr($method, 3));
0 ignored issues
show
Deprecated Code introduced by
The function snake_case() has been deprecated: Str::snake() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

57
            $name = /** @scrutinizer ignore-deprecated */ snake_case(substr($method, 3));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
58
59
            if(isset($this->attributes[$name])) {
60
                return $this->attributes[$name];
61
            }
62
63
        }elseif (strtolower(substr($method, 0, 3)) === 'set') {
64
            $name = snake_case(substr($method, 3));
0 ignored issues
show
Deprecated Code introduced by
The function snake_case() has been deprecated: Str::snake() should be used directly instead. Will be removed in Laravel 5.9. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

64
            $name = /** @scrutinizer ignore-deprecated */ snake_case(substr($method, 3));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
65
66
            if(isset($this->attributes[$name])) {
67
                $this->attributes[$name] = array_pop($arguments);
68
69
                return $this->attributes[$name];
70
            }
71
72
        }
73
74
        if(method_exists($this->parent, $method) === true){
75
            return call_user_func_array([$this->parent, $method], $arguments);
76
        }
77
78
        throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
79
    }
80
81
    /**
82
     * @param $name
83
     * @param $value
84
     *
85
     * @return mixed
86
     */
87
    public function __set($name, $value) {
88
        $this->attributes[$name] = $value;
89
90
        return $this->attributes[$name];
91
    }
92
93
    /**
94
     * @param $name
95
     *
96
     * @return mixed|null
97
     */
98
    public function __get($name) {
99
        if(isset($this->attributes[$name])) {
100
            return $this->attributes[$name];
101
        }
102
103
        return null;
104
    }
105
106
    /**
107
     * @return mixed
108
     */
109
    public function getParent(){
110
        return $this->parent;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    public function getAttributes(){
117
        return $this->attributes;
118
    }
119
120
}