Issues (1783)

src/controllers/StatisticsController.php (26 issues)

1
<?php
2
/**
3
 * Retour plugin for Craft CMS
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/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\retour\controllers;
13
14
use Craft;
15
use craft\errors\MissingComponentException;
16
use craft\helpers\UrlHelper;
17
use craft\web\Controller;
18
use nystudio107\retour\assetbundles\retour\RetourDashboardAsset;
19
use nystudio107\retour\helpers\MultiSite as MultiSiteHelper;
20
use nystudio107\retour\helpers\Permission as PermissionHelper;
21
use nystudio107\retour\Retour;
22
use yii\base\InvalidConfigException;
23
use yii\web\BadRequestHttpException;
24
use yii\web\ForbiddenHttpException;
25
use yii\web\NotFoundHttpException;
26
use yii\web\Response;
27
28
/**
0 ignored issues
show
Missing short description in doc comment
Loading history...
29
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
30
 * @package   Retour
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
31
 * @since     3.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
32
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
33
class StatisticsController extends Controller
34
{
35
    // Constants
36
    // =========================================================================
37
38
    protected const DOCUMENTATION_URL = 'https://github.com/nystudio107/craft-retour/';
39
40
    // Protected Properties
41
    // =========================================================================
42
43
    protected array|bool|int $allowAnonymous = [];
44
45
    // Public Methods
46
    // =========================================================================
47
48
49
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
50
     * @param string|null $siteHandle
0 ignored issues
show
Missing parameter comment
Loading history...
51
     * @param bool $showWelcome
0 ignored issues
show
Missing parameter comment
Loading history...
Expected 8 spaces after parameter type; 1 found
Loading history...
52
     *
53
     * @return Response
54
     * @throws NotFoundHttpException
55
     * @throws 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();
0 ignored issues
show
The method trimStatistics() does not exist on null. ( Ignorable by Annotation )

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

62
        Retour::$plugin->statistics->/** @scrutinizer ignore-call */ 
63
                                     trimStatistics();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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/web/assets/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
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
108
     * @return Response
109
     * @throws 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();
0 ignored issues
show
The method clearAllCaches() does not exist on null. ( Ignorable by Annotation )

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

123
        Retour::$plugin->/** @scrutinizer ignore-call */ 
124
                         clearAllCaches();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
134
     * @return ?Response
135
     * @throws MissingComponentException
136
     * @throws BadRequestHttpException
137
     * @throws ForbiddenHttpException
138
     */
139
    public function actionDeleteStatistics(): ?Response
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 null;
170
    }
171
}
172