Mask   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 26
c 2
b 0
f 0
dl 0
loc 112
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 1 1
A __get() 0 6 2
A __construct() 0 8 2
A getAttributes() 0 2 1
A getParent() 0 2 1
A __call() 0 24 6
A __set() 0 4 1
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\PHPIMAP\Support\Masks;
14
15
use Illuminate\Support\Str;
16
use Webklex\PHPIMAP\Exceptions\MethodNotFoundException;
17
18
/**
19
 * Class Mask
20
 *
21
 * @package Webklex\PHPIMAP\Support\Masks
22
 */
23
class Mask {
24
25
    /**
26
     * Available attributes
27
     *
28
     * @var array $attributes
29
     */
30
    protected $attributes = [];
31
32
    /**
33
     * Parent instance
34
     *
35
     * @var object $parent
36
     */
37
    protected $parent;
38
39
    /**
40
     * Mask constructor.
41
     * @param $parent
42
     */
43
    public function __construct($parent) {
44
        $this->parent = $parent;
45
46
        if(method_exists($this->parent, 'getAttributes')){
47
            $this->attributes = array_merge($this->attributes, $this->parent->getAttributes());
48
        }
49
50
        $this->boot();
51
    }
52
53
    /**
54
     * Boot method made to be used by any custom mask
55
     */
56
    protected function boot(){}
57
58
    /**
59
     * Call dynamic attribute setter and getter methods and inherit the parent calls
60
     * @param string $method
61
     * @param array $arguments
62
     *
63
     * @return mixed
64
     * @throws MethodNotFoundException
65
     */
66
    public function __call(string $method, array $arguments) {
67
        if(strtolower(substr($method, 0, 3)) === 'get') {
68
            $name = Str::snake(substr($method, 3));
69
70
            if(isset($this->attributes[$name])) {
71
                return $this->attributes[$name];
72
            }
73
74
        }elseif (strtolower(substr($method, 0, 3)) === 'set') {
75
            $name = Str::snake(substr($method, 3));
76
77
            if(isset($this->attributes[$name])) {
78
                $this->attributes[$name] = array_pop($arguments);
79
80
                return $this->attributes[$name];
81
            }
82
83
        }
84
85
        if(method_exists($this->parent, $method) === true){
86
            return call_user_func_array([$this->parent, $method], $arguments);
87
        }
88
89
        throw new MethodNotFoundException("Method ".self::class.'::'.$method.'() is not supported');
90
    }
91
92
    /**
93
     * Magic setter
94
     * @param $name
95
     * @param $value
96
     *
97
     * @return mixed
98
     */
99
    public function __set($name, $value) {
100
        $this->attributes[$name] = $value;
101
102
        return $this->attributes[$name];
103
    }
104
105
    /**
106
     * Magic getter
107
     * @param $name
108
     *
109
     * @return mixed|null
110
     */
111
    public function __get($name) {
112
        if(isset($this->attributes[$name])) {
113
            return $this->attributes[$name];
114
        }
115
116
        return null;
117
    }
118
119
    /**
120
     * Get the parent instance
121
     *
122
     * @return object
123
     */
124
    public function getParent(){
125
        return $this->parent;
126
    }
127
128
    /**
129
     * Get all available attributes
130
     *
131
     * @return array
132
     */
133
    public function getAttributes(): array {
134
        return $this->attributes;
135
    }
136
137
}