Base   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 19
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 192
ccs 57
cts 57
cp 1
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B __call() 0 24 5
A getDataWrapper() 0 4 1
A setDataWrapper() 0 5 1
A getHash() 0 8 2
A setHash() 0 5 1
A getName() 0 10 2
A setName() 0 5 1
A generateHash() 0 4 1
A exportContextData() 0 4 1
A prepareExport() 0 10 1
A getFullInfo() 0 4 1
A setFullInfo() 0 5 1
1
<?php
2
3
namespace GFG\DTOContext\Context;
4
5
use GFG\DTOContext\DataWrapper;
6
7
class Base implements ContextInterface
8
{
9
    /**
10
     * Meaninful information about the context
11
     *
12
     * @var array
13
     */
14
    protected $info = [];
15
16
    /**
17
     * Context name
18
     *
19
     * @var string
20
     */
21
    protected $name;
22
23
    /**
24
     * Hash to identify an operation using this context
25
     *
26
     * @var string
27
     */
28
    protected $hash;
29
    
30
    /**
31
     * DataWrapper
32
     *
33
     * @var DataWrapper\DataWrapperInterface
34
     */
35
    protected $dataWrapper;
36
37
    /**
38
     * @param DataWrapper\DataWrapperInterface $dataWrapper
39
     */
40 12
    public function __construct(DataWrapper\DataWrapperInterface $dataWrapper)
41
    {
42 12
        $this->setDataWrapper($dataWrapper);
43 12
    }
44
45
    /**
46
     * Magic method to add additional meaninful information about the context
47
     *
48
     * @param string $method
49
     * @param array $arguments
50
     * @return mixed
51
     */
52 3
    public function __call($method, array $arguments)
53
    {
54 3
        $action = substr($method, 0, 3);
55
56 3
        if (in_array($action, ['get', 'set'])) {
57
58 2
            $property = substr($method, 3);
59 2
            $return   = null;
60
61
            switch ($action) {
62 2
                case 'get':
63 1
                    $return = (isset($this->info[$property])) ?
64 1
                        $this->info[$property] : null;
65 1
                    break;
66
                
67 2
                case 'set':
68 2
                    $this->info[$property] = current($arguments);
69 2
                    $return = $this;
70 2
                    break;
71
            }
72
73 2
            return $return;
74
        }
75 1
    }
76
77
    /**
78
     * Returns all the info values
79
     *
80
     * @return array
81
     */
82 1
    public function getFullInfo()
83
    {
84 1
        return $this->info;
85
    }
86
87
    /**
88
     * Set the info property
89
     *
90
     * @param array $fullInfo
91
     * @return Base
92
     */
93 1
    public function setFullInfo(array $fullInfo)
94
    {
95 1
        $this->info = $fullInfo;
96 1
        return $this;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102 3
    public function getDataWrapper()
103
    {
104 3
        return $this->dataWrapper;
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110 12
    public function setDataWrapper(DataWrapper\DataWrapperInterface $dataWrapper)
111
    {
112 12
        $this->dataWrapper = $dataWrapper;
113 12
        return $this;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 3
    public function getHash()
120
    {
121 3
        if (!$this->hash) {
122 2
            $this->hash = $this->generateHash();
123 2
        }
124
125 3
        return $this->hash;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 1
    public function setHash($hash)
132
    {
133 1
        $this->hash = $hash;
134 1
        return $this;
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140 5
    public function getName()
141
    {
142 5
        return mb_strtolower(
143 5
            str_replace(
144 5
                ['_', '\\'],
145 5
                '.',
146 5
                $this->name ?: strtolower(static::class)
147 5
            )
148 5
        );
149
    }
150
151
    /**
152
     * @param string $name
153
     * @return Base
154
     */
155 3
    public function setName($name)
156
    {
157 3
        $this->name = $name;
158 3
        return $this;
159
    }
160
161
    /**
162
     * Generate a unique hash
163
     *
164
     * @return string
165
     */
166 2
    protected function generateHash()
167
    {
168 2
        return sha1(uniqid(mt_rand(), true));
169
    }
170
171
    /**
172
     * Export context data
173
     *
174
     * @return void
175
     * {@inheritdoc}
176
     */
177 1
    public function exportContextData()
178
    {
179 1
        return $this->prepareExport($this->dataWrapper->toArray());
180
    }
181
182
    /**
183
     * Prepare the info to be exported
184
     *
185
     * @param array
186
     * @return array
187
     */
188 1
    protected function prepareExport($data)
189
    {
190
        return [
191 1
            'name'         => $this->getName(),
192 1
            'info'         => $this->info,
193 1
            'hash'         => $this->getHash(),
194 1
            'data_wrapper' => get_class($this->getDatawrapper()),
195
            'data'         => $data
196 1
        ];
197
    }
198
}
199