VisitorsController::actionIndex()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 10
cp 0
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://flipboxfactory.com/software/hubspot/license
6
 * @link       https://www.flipboxfactory.com/software/hubspot/
7
 */
8
9
namespace flipbox\craft\hubspot\cp\controllers\view;
10
11
use craft\helpers\UrlHelper;
12
use flipbox\craft\hubspot\HubSpot;
13
use flipbox\craft\hubspot\records\Visitor;
14
use yii\web\Response;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 1.1.0
19
 */
20
class VisitorsController extends AbstractController
21
{
22
    /**
23
     * The template base path
24
     */
25
    const TEMPLATE_BASE = parent::TEMPLATE_BASE . '/visitors';
26
27
    /**
28
     * The index view template path
29
     */
30
    const TEMPLATE_INDEX = self::TEMPLATE_BASE . '/index';
31
32
    /**
33
     * The detail view template path
34
     */
35
    const TEMPLATE_DETAIL = self::TEMPLATE_BASE . '/detail';
36
37
    /**
38
     * @return Response
39
     */
40
    public function actionIndex(): Response
41
    {
42
        $variables = [];
43
        $this->baseVariables($variables);
44
45
        $variables['visitors'] = Visitor::find()->all();
46
47
        return $this->renderTemplate(
48
            static::TEMPLATE_INDEX,
49
            $variables
50
        );
51
    }
52
53
    /**
54
     * @param $identifier
55
     * @return Response
56
     * @throws \flipbox\craft\ember\exceptions\RecordNotFoundException
57
     */
58
    public function actionDetail($identifier): Response
59
    {
60
        $visitor = Visitor::getOne($identifier);
61
62
        $variables = [];
63
64
        $this->baseUpsertVariables($visitor, $variables);
65
66
        // Full page form in the CP
67
        $variables['fullPageForm'] = true;
68
69
        $variables['visitor'] = $visitor;
70
        $variables['statusOptions'] = [
71
            [
72
                'label' => HubSpot::t("Successful"),
73
                'value' => Visitor::STATUS_SUCCESSFUL
74
            ],
75
            [
76
                'label' => HubSpot::t("Pending"),
77
                'value' => Visitor::STATUS_PENDING
78
            ],
79
            [
80
                'label' => HubSpot::t("Not Found"),
81
                'value' => Visitor::STATUS_NOT_FOUND
82
            ],
83
            [
84
                'label' => HubSpot::t("Error"),
85
                'value' => Visitor::STATUS_ERROR
86
            ]
87
        ];
88
89
        return $this->renderTemplate(
90
            static::TEMPLATE_DETAIL,
91
            $variables
92
        );
93
    }
94
95
96
    /*******************************************
97
     * BASE PATHS
98
     *******************************************/
99
100
    /**
101
     * @return string
102
     */
103
    protected function getBaseActionPath(): string
104
    {
105
        return parent::getBaseActionPath() . '/visitors';
106
    }
107
108
    /**
109
     * @return string
110
     */
111
    protected function getBaseCpPath(): string
112
    {
113
        return parent::getBaseCpPath() . '/visitors';
114
    }
115
116
    /*******************************************
117
     * VARIABLES
118
     *******************************************/
119
120
    /**
121
     * @param array $variables
122
     * @param Visitor $visitor
123
     */
124
    protected function baseUpsertVariables(Visitor $visitor, array &$variables = [])
125
    {
126
        $this->baseVariables($variables);
127
128
        // Breadcrumbs
129
        $variables['crumbs'][] = [
130
            'label' => $visitor->token,
131
            'url' => ''
132
        ];
133
134
        $variables['title'] .= ': ' . $visitor->token;
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    protected function baseVariables(array &$variables = [])
141
    {
142
        parent::baseVariables($variables);
143
144
        $title = HubSpot::t("Visitors");
145
        $variables['title'] .= ' ' . $title;
146
147
        // Breadcrumbs
148
        $variables['crumbs'][] = [
149
            'label' => $title,
150
            'url' => UrlHelper::url($this->getBaseCpPath())
151
        ];
152
    }
153
}
154