Passed
Push — v3 ( 2b006a...43f239 )
by Andrew
33:34 queued 16:32
created

src/controllers/StatisticsController.php (1 issue)

1
<?php
2
/**
3
 * Retour plugin for Craft CMS 3.x
4
 *
5
 * Retour allows you to intelligently redirect legacy URLs, so that you don't
6
 * lose SEO value when rebuilding & restructuring a website
7
 *
8
 * @link      https://nystudio107.com/
9
 * @copyright Copyright (c) 2018 nystudio107
10
 */
11
12
namespace nystudio107\retour\controllers;
13
14
use nystudio107\retour\Retour;
15
use nystudio107\retour\assetbundles\retour\RetourDashboardAsset;
16
use nystudio107\retour\helpers\MultiSite as MultiSiteHelper;
17
use nystudio107\retour\helpers\Permission as PermissionHelper;
18
19
use Craft;
20
use craft\web\Controller;
21
use craft\errors\MissingComponentException;
22
use craft\helpers\UrlHelper;
23
24
use yii\base\InvalidConfigException;
25
use yii\web\NotFoundHttpException;
26
use yii\web\Response;
27
28
/**
29
 * @author    nystudio107
30
 * @package   Retour
31
 * @since     3.0.0
32
 */
33
class StatisticsController extends Controller
34
{
35
    // Constants
36
    // =========================================================================
37
38
    const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/';
39
40
    // Protected Properties
41
    // =========================================================================
42
43
    protected $allowAnonymous = [];
44
45
    // Public Methods
46
    // =========================================================================
47
48
49
    /**
50
     * @param string|null $siteHandle
51
     * @param bool        $showWelcome
52
     *
53
     * @return Response
54
     * @throws NotFoundHttpException
55
     * @throws \yii\web\ForbiddenHttpException
56
     */
57
    public function actionDashboard(string $siteHandle = null, bool $showWelcome = false): Response
58
    {
59
        $variables = [];
60
        PermissionHelper::controllerPermissionCheck('retour:dashboard');
61
        // Trim the statistics
62
        Retour::$plugin->statistics->trimStatistics();
63
        // Get the site to edit
64
        $siteId = MultiSiteHelper::getSiteIdFromHandle($siteHandle);
65
        $pluginName = Retour::$settings->pluginName;
66
        $templateTitle = Craft::t('retour', 'Dashboard');
67
        $view = Craft::$app->getView();
68
        // Asset bundle
69
        try {
70
            $view->registerAssetBundle(RetourDashboardAsset::class);
71
        } catch (InvalidConfigException $e) {
72
            Craft::error($e->getMessage(), __METHOD__);
73
        }
74
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(
75
            '@nystudio107/retour/assetbundles/retour/dist',
76
            true
0 ignored issues
show
The call to yii\web\AssetManager::getPublishedUrl() has too many arguments starting with true. ( Ignorable by Annotation )

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

76
        /** @scrutinizer ignore-call */ 
77
        $variables['baseAssetsUrl'] = Craft::$app->assetManager->getPublishedUrl(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
77
        );
78
        // Enabled sites
79
        MultiSiteHelper::setMultiSiteVariables($siteHandle, $siteId, $variables);
80
        $variables['controllerHandle'] = 'dashboard';
81
82
        // Basic variables
83
        $variables['fullPageForm'] = false;
84
        $variables['docsUrl'] = self::DOCUMENTATION_URL;
85
        $variables['pluginName'] = $pluginName;
86
        $variables['title'] = $templateTitle;
87
        $siteHandleUri = Craft::$app->isMultiSite ? '/'.$siteHandle : '';
88
        $variables['crumbs'] = [
89
            [
90
                'label' => $pluginName,
91
                'url' => UrlHelper::cpUrl('retour'),
92
            ],
93
            [
94
                'label' => $templateTitle,
95
                'url' => UrlHelper::cpUrl('retour/dashboard'.$siteHandleUri),
96
            ],
97
        ];
98
        $variables['docTitle'] = "{$pluginName} - {$templateTitle}";
99
        $variables['selectedSubnavItem'] = 'dashboard';
100
        $variables['showWelcome'] = $showWelcome;
101
        $variables['settings'] = Retour::$settings;
102
103
        // Render the template
104
        return $this->renderTemplate('retour/dashboard/index', $variables);
105
    }
106
107
    /**
108
     * @return Response
109
     * @throws \yii\web\ForbiddenHttpException
110
     */
111
    public function actionClearStatistics(): Response
112
    {
113
        PermissionHelper::controllerPermissionCheck('retour:dashboard');
114
        $error = Retour::$plugin->statistics->clearStatistics();
115
        Craft::info(
116
            Craft::t(
117
                'retour',
118
                'Retour statistics cleared: {error}',
119
                ['error' => $error]
120
            ),
121
            __METHOD__
122
        );
123
        Retour::$plugin->clearAllCaches();
124
        try {
125
            Craft::$app->getSession()->setNotice(Craft::t('retour', 'Retour statistics cleared.'));
126
        } catch (MissingComponentException $e) {
127
            Craft::error($e->getMessage(), __METHOD__);
128
        }
129
130
        return $this->redirect('retour/dashboard');
131
    }
132
133
    /**
134
     * @return Response|void
135
     * @throws MissingComponentException
136
     * @throws \yii\web\BadRequestHttpException
137
     * @throws \yii\web\ForbiddenHttpException
138
     */
139
    public function actionDeleteStatistics()
140
    {
141
        PermissionHelper::controllerPermissionCheck('retour:dashboard');
142
        $request = Craft::$app->getRequest();
143
        $statisticIds = $request->getRequiredBodyParam('statisticIds');
144
        $stickyError = false;
145
        foreach ($statisticIds as $statisticId) {
146
            if (Retour::$plugin->statistics->deleteStatisticById($statisticId) === 0) {
147
                $stickyError = true;
148
            }
149
        }
150
        Craft::info(
151
            Craft::t(
152
                'retour',
153
                'Retour statistics deleted: {error}',
154
                ['error' => $stickyError]
155
            ),
156
            __METHOD__
157
        );
158
159
        Retour::$plugin->clearAllCaches();
160
        // Handle any cumulative errors
161
        if (!$stickyError) {
162
            // Clear the caches and continue on
163
            Craft::$app->getSession()->setNotice(Craft::t('retour', 'Retour statistics deleted.'));
164
165
            return $this->redirect('retour/dashboard');
166
        }
167
        Craft::$app->getSession()->setError(Craft::t('retour', "Couldn't delete statistic."));
168
169
        return;
170
    }
171
}
172