Test Failed
Push — master ( a6b51e...5fffdb )
by Gabriel
08:05
created

HasAuthenticationVariablesTrait   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 135
rs 10
c 0
b 0
f 0
ccs 0
cts 46
cp 0
wmc 22
lcom 1
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuthenticationVariablesInView() 0 4 1
A getAuthenticationVariables() 0 8 2
A initAuthenticationVariables() 0 4 1
A generateAuthenticationVariables() 0 8 1
A getAuthenticationVarRedirect() 0 11 4
A getAuthenticationValues() 0 5 1
A checkInitAuthenticationValues() 0 6 2
A getAuthenticationValue() 0 5 2
A initAuthenticationValues() 0 4 1
A generateAuthenticationValues() 0 9 3
A decodeVariables() 0 4 1
A encodeVariable() 0 4 1
A isEncodedVariable() 0 7 2
1
<?php
2
3
namespace ByTIC\Hello\Modules\AbstractModule\Controllers\Traits;
4
5
use Nip\Controllers\Traits\AbstractControllerTrait;
6
7
/**
8
 * Trait HasAuthenticationVariablesTrait
9
 * @package KM42\Hello\Modules\AbstractModule\Controllers\Traits
10
 */
11
trait HasAuthenticationVariablesTrait
12
{
13
    use AbstractControllerTrait;
14
15
    protected $authenticationVariable = null;
16
    protected $authenticationValues = null;
17
18
    public function setAuthenticationVariablesInView()
19
    {
20
        $this->getView()->set('authenticationVariables', $this->getAuthenticationVariables());
21
    }
22
23
    /**
24
     * @return array
25
     */
26
    protected function getAuthenticationVariables()
27
    {
28
        if ($this->authenticationVariable === null) {
29
            $this->initAuthenticationVariables();
30
        }
31
32
        return $this->authenticationVariable;
33
    }
34
35
    protected function initAuthenticationVariables()
36
    {
37
        $this->authenticationVariable = $this->generateAuthenticationVariables();
38
    }
39
40
    /**
41
     * @return array
42
     */
43
    protected function generateAuthenticationVariables()
44
    {
45
        $variables = [
46
            'redirect' => $this->getAuthenticationVarRedirect(),
47
        ];
48
49
        return $variables;
50
    }
51
52
    /**
53
     * @return string|null
54
     */
55
    protected function getAuthenticationVarRedirect()
56
    {
57
        if (method_exists($this, 'generateAuthenticationVarRedirect')) {
58
            $redirect = $this->generateAuthenticationVarRedirect();
0 ignored issues
show
Bug introduced by
The method generateAuthenticationVarRedirect() does not exist on ByTIC\Hello\Modules\Abst...nticationVariablesTrait. Did you maybe mean redirect()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
59
        } else {
60
            $redirect = $this->getRequest()->query->has('redirect')
61
                ? $this->getRequest()->query->get('redirect')
62
                : null;
63
        }
64
        return $this->isEncodedVariable($redirect) ? $redirect : $this->encodeVariable($redirect);
65
    }
66
67
68
    /**
69
     * @return array
70
     */
71
    protected function getAuthenticationValues()
72
    {
73
        $this->checkInitAuthenticationValues();
74
        return $this->authenticationValues;
75
    }
76
77
    protected function checkInitAuthenticationValues()
78
    {
79
        if ($this->authenticationValues === null) {
80
            $this->initAuthenticationValues();
81
        }
82
    }
83
84
    /**
85
     * @param $key
86
     * @param null $default
87
     * @return mixed
88
     */
89
    protected function getAuthenticationValue($key, $default = null)
90
    {
91
        $this->checkInitAuthenticationValues();
92
        return isset($this->authenticationValues[$key]) ? $this->authenticationValues[$key] : $default;
93
    }
94
95
    /**
96
     * @return array
97
     */
98
    protected function initAuthenticationValues()
99
    {
100
        return $this->authenticationValues = $this->generateAuthenticationValues();
101
    }
102
103
    /**
104
     * @return array
105
     */
106
    protected function generateAuthenticationValues()
107
    {
108
        $variables = $this->getAuthenticationVariables();
109
        $return = [];
110
        foreach ($variables as $key => $value) {
111
            $return[$key] = $this->isEncodedVariable($value) ? $this->decodeVariables($value) : $value;
112
        }
113
        return $return;
114
    }
115
116
    /**
117
     * @param $value
118
     * @return mixed
119
     */
120
    protected function decodeVariables($value)
121
    {
122
        return base64_decode(strtr($value, '$_-', '+/='));
123
    }
124
125
    /**
126
     * @param $value
127
     * @return mixed
128
     */
129
    protected function encodeVariable($value)
130
    {
131
        return strtr(base64_encode($value), '+/=', '$_-');
132
    }
133
134
    /**
135
     * @param $data
136
     * @return bool
137
     */
138
    protected function isEncodedVariable($data)
139
    {
140
        if ($this->encodeVariable($this->decodeVariables($data)) === $data) {
141
            return true;
142
        }
143
        return false;
144
    }
145
}
146