AbstractViewController::insertVariables()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 9
cp 0
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
namespace flipbox\patron\cp\controllers\view\providers;
4
5
use Craft;
6
use craft\helpers\UrlHelper;
7
use flipbox\patron\cp\Cp;
8
use flipbox\patron\Patron;
9
use flipbox\patron\records\Provider;
10
11
/**
12
 * @property Cp $module
13
 */
14
abstract class AbstractViewController extends \flipbox\patron\cp\controllers\view\AbstractViewController
15
{
16
    /**
17
     * The index view template path
18
     */
19
    const TEMPLATE_BASE = 'patron/_cp/provider';
20
21
22
    /*******************************************
23
     * TABS
24
     *******************************************/
25
26
    /**
27
     * @param Provider $provider
28
     * @return array
29
     */
30
    protected function getTabs(Provider $provider): array
31
    {
32
        if ($provider->getId() === null) {
33
            return [];
34
        }
35
36
        $baseUrl = Craft::$app->getRequest()->getSegment(1) . '/' .
37
            Craft::$app->getRequest()->getSegment(2) . '/' .
38
            Craft::$app->getRequest()->getSegment(3);
39
40
        return [
41
            'general' => [
42
                'label' => Craft::t('patron', 'General'),
43
                'url' => UrlHelper::url($baseUrl . '/')
44
            ],
45
            'tokens' => [
46
                'label' => Craft::t('patron', 'Tokens'),
47
                'url' => UrlHelper::url($baseUrl . '/tokens')
48
            ]
49
        ];
50
    }
51
52
    /*******************************************
53
     * PATHS
54
     *******************************************/
55
56
    /**
57
     * @inheritdoc
58
     */
59
    protected function getBaseCpProviderPath(): string
60
    {
61
        return parent::getBaseCpPath() . '/providers/' . Craft::$app->getRequest()->getSegment(3);
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getBaseCpPath() instead of getBaseCpProviderPath()). Are you sure this is correct? If so, you might want to change this to $this->getBaseCpPath().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    protected function getBaseCpProvidersPath(): string
68
    {
69
        return parent::getBaseCpPath() . '/providers';
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (getBaseCpPath() instead of getBaseCpProvidersPath()). Are you sure this is correct? If so, you might want to change this to $this->getBaseCpPath().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    protected function getBaseActionPath(): string
76
    {
77
        return parent::getBaseActionPath() . '/providers';
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    protected function getBaseCpPath(): string
84
    {
85
        return parent::getBaseCpPath() . '/providers';
86
    }
87
88
    /*******************************************
89
     * VARIABLES
90
     *******************************************/
91
92
    /**
93
     * Set base variables used to generate template views
94
     *
95
     * @param array $variables
96
     */
97
    protected function baseVariables(array &$variables = [])
98
    {
99
        parent::baseVariables($variables);
100
101
        // Page title
102
        $variables['title'] .= ': ' . Craft::t('patron', "Providers");
103
104
        // Breadcrumbs
105
        $variables['crumbs'][] = [
106
            'label' => Craft::t('patron', "Providers"),
107
            'url' => UrlHelper::url(Patron::getInstance()->getUniqueId() . '/providers')
108
        ];
109
    }
110
111
    /**
112
     * @param array $variables
113
     */
114
    protected function insertVariables(array &$variables)
115
    {
116
        // Apply base view variables
117
        $this->baseVariables($variables);
118
119
        // Set the "Continue Editing" URL
120
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/{id}');
121
122
        // Append title
123
        $variables['title'] .= ' - ' . Craft::t('patron', 'New');
124
125
        // Breadcrumbs
126
        $variables['crumbs'][] = [
127
            'label' => Craft::t('patron', 'New'),
128
            'url' => UrlHelper::url($variables['baseCpPath'] . '/new')
129
        ];
130
    }
131
132
    /*******************************************
133
     * UPDATE VARIABLES
134
     *******************************************/
135
136
    /**
137
     * @param array $variables
138
     * @param Provider $provider
139
     * @throws \ReflectionException
140
     */
141
    protected function updateVariables(array &$variables, Provider $provider)
142
    {
143
        // Apply base view variables
144
        $this->baseVariables($variables);
145
146
        // Set the "Continue Editing" URL
147
        $variables['continueEditingUrl'] = $this->getBaseContinueEditingUrl('/' . $provider->getId());
148
        $variables['baseCpProvidersPath'] = $this->getBaseCpProvidersPath();
149
        $variables['baseCpProviderPath'] = $this->getBaseCpProviderPath();
150
151
        $providerInfo = $provider->getInfo();
152
153
        // Append title
154
        $variables['title'] .= ' - ' . ($providerInfo['name'] ?? '');
155
156
        // Breadcrumbs
157
        $variables['crumbs'][] = [
158
            'label' => $providerInfo['name'] ?? '',
159
            'url' => UrlHelper::url(
160
                Patron::getInstance()->getUniqueId() . '/providers/' . $provider->getId()
161
            )
162
        ];
163
    }
164
}
165