AbstractAuthenticationControllerTrait   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 13
eloc 27
c 1
b 0
f 1
dl 0
loc 83
ccs 0
cts 39
cp 0
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getRedirectURL() 0 10 3
A beforeAction() 0 4 1
A afterActionViewVariables() 0 5 1
A checkIsAuthenticated() 0 4 3
A getGenericRedirectURL() 0 4 1
A generateModelName() 0 3 1
A doSuccessRedirect() 0 8 2
A _setMeta() 0 7 1
1
<?php
2
3
namespace ByTIC\Hello\Modules\AbstractModule\Controllers\Traits;
4
5
/**
6
 * Trait AbstractAuthenticationControllerTrait
7
 * @package ByTIC\Hello\Modules\AbstractModule\Controllers\Traits
8
 */
9
trait AbstractAuthenticationControllerTrait
10
{
11
    use HasAuthenticationVariablesTrait;
12
13
    protected function beforeAction()
14
    {
15
        parent::beforeAction();
16
        $this->checkIsAuthenticated();
17
    }
18
19
    protected function afterActionViewVariables()
20
    {
21
        parent::afterActionViewVariables();
22
23
        $this->setAuthenticationVariablesInView();
24
    }
25
26
    /**
27
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
28
     */
29
    protected function checkIsAuthenticated()
30
    {
31
        if ($this->getName() != 'logout' && $this->_getUser()->authenticated()) {
0 ignored issues
show
Bug introduced by
It seems like _getUser() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
        if ($this->getName() != 'logout' && $this->/** @scrutinizer ignore-call */ _getUser()->authenticated()) {
Loading history...
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

31
        if ($this->/** @scrutinizer ignore-call */ getName() != 'logout' && $this->_getUser()->authenticated()) {
Loading history...
32
            $this->doSuccessRedirect();
33
        }
34
    }
35
36
    /**
37
     * @param null $type
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $type is correct as it would always require null to be passed?
Loading history...
38
     */
39
    protected function doSuccessRedirect($type = null)
40
    {
41
        $type = empty($type) ? $this->getName() : $type;
42
        $this->flashRedirect(
43
            $this->getModelManager()->getMessage($type . '-success'),
0 ignored issues
show
Bug introduced by
It seems like getModelManager() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

43
            $this->/** @scrutinizer ignore-call */ 
44
                   getModelManager()->getMessage($type . '-success'),
Loading history...
44
            $this->getRedirectURL(),
45
            'success',
46
            'index'
0 ignored issues
show
Bug introduced by
'index' of type string is incompatible with the type boolean expected by parameter $name of ByTIC\Hello\Modules\Abst...rTrait::flashRedirect(). ( Ignorable by Annotation )

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

46
            /** @scrutinizer ignore-type */ 'index'
Loading history...
47
        );
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    protected function getRedirectURL()
54
    {
55
        $redirectVariable = $this->getAuthenticationValue('redirect');
56
        if (!empty($redirectVariable)) {
57
            $url = urldecode($redirectVariable);
58
            if (valid_url($url)) {
0 ignored issues
show
Bug introduced by
The function valid_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

58
            if (/** @scrutinizer ignore-call */ valid_url($url)) {
Loading history...
59
                return $url;
60
            }
61
        }
62
        return $this->getGenericRedirectURL();
63
    }
64
65
    /**
66
     * @return string|null
67
     */
68
    protected function getGenericRedirectURL()
69
    {
70
        $module = $this->getRequest()->getModuleName();
71
        return $this->Url()->assemble($module . '.logged_in', $this->getAuthenticationVariables());
0 ignored issues
show
Bug introduced by
It seems like Url() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

71
        return $this->/** @scrutinizer ignore-call */ Url()->assemble($module . '.logged_in', $this->getAuthenticationVariables());
Loading history...
72
    }
73
74
    /**
75
     * @param $action
76
     */
77
    protected function _setMeta($action)
78
    {
79
        $label = $this->getModelManager()->getLabel($action . '-title');
80
        $urlMethod = 'get' . ucfirst($action) . 'URL';
81
        $this->getView()->Breadcrumbs()->addItem($label, $this->_getUser()->getManager()->$urlMethod());
82
83
        $this->getView()->Meta()->prependTitle($label);
84
    }
85
86
    /**
87
     * @inheritdoc
88
     */
89
    protected function generateModelName()
90
    {
91
        return get_class($this->_getUser()->getManager());
92
    }
93
}
94