Passed
Push — v1 ( bf5890...0a561e )
by Andrew
06:24 queued 03:18
created

DataSamplesController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 26
dl 0
loc 81
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A actionDeleteAllSamples() 0 17 3
A actionDeleteSamplesByUrl() 0 17 3
A actionDeleteSampleById() 0 13 2
1
<?php
2
/**
3
 * Webperf plugin for Craft CMS 3.x
4
 *
5
 * Monitor the performance of your webpages through real-world user timing data
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\webperf\controllers;
12
13
use nystudio107\webperf\Webperf;
14
use nystudio107\webperf\helpers\Permission as PermissionHelper;
15
16
use Craft;
0 ignored issues
show
Bug introduced by
The type Craft was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use craft\web\Controller;
0 ignored issues
show
Bug introduced by
The type craft\web\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
use yii\web\Response;
0 ignored issues
show
Bug introduced by
The type yii\web\Response was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 2 spaces but found 4
Loading history...
23
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 3 spaces but found 5
Loading history...
25
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
26
class DataSamplesController extends Controller
27
{
28
    // Constants
29
    // =========================================================================
30
31
    // Public Methods
32
    // =========================================================================
33
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
35
     * @param int $id
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
36
     *
37
     * @return Response
38
     * @throws \craft\errors\MissingComponentException
39
     * @throws \yii\web\ForbiddenHttpException
40
     */
41
    public function actionDeleteSampleById(int $id): Response
42
    {
43
        PermissionHelper::controllerPermissionCheck('webperf:delete-data-samples');
44
        if (Webperf::$plugin->dataSamples->deleteSampleById($id)) {
45
            // Clear the caches and continue on
46
            Webperf::$plugin->clearAllCaches();
47
            Craft::$app->getSession()->setNotice(Craft::t('webperf', 'Data sample deleted.'));
48
49
            return $this->redirect(Craft::$app->getRequest()->referrer);
50
        }
51
        Craft::$app->getSession()->setError(Craft::t('webperf', "Couldn't delete data sample."));
52
53
        return $this->redirect(Craft::$app->getRequest()->referrer);
54
    }
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
57
     * @param string   $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
58
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
59
     *
60
     * @return Response
61
     * @throws \craft\errors\MissingComponentException
62
     * @throws \yii\web\ForbiddenHttpException
63
     */
64
    public function actionDeleteSamplesByUrl(string $pageUrl, int $siteId = null): Response
65
    {
66
        // We may be passed 0 or other "empty" values, so coerce to null
67
        if (empty($siteId)) {
68
            $siteId = null;
69
        }
70
        PermissionHelper::controllerPermissionCheck('webperf:delete-data-samples');
71
        if (Webperf::$plugin->dataSamples->deleteDataSamplesByUrl($pageUrl, $siteId)) {
72
            // Clear the caches and continue on
73
            Webperf::$plugin->clearAllCaches();
74
            Craft::$app->getSession()->setNotice(Craft::t('webperf', 'Data samples deleted.'));
75
76
            return $this->redirect(Craft::$app->getRequest()->referrer);
77
        }
78
        Craft::$app->getSession()->setError(Craft::t('webperf', "Couldn't delete data samples."));
79
80
        return $this->redirect(Craft::$app->getRequest()->referrer);
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     *
86
     * @return Response
87
     * @throws \craft\errors\MissingComponentException
88
     * @throws \yii\web\ForbiddenHttpException
89
     */
90
    public function actionDeleteAllSamples(int $siteId = null): Response
91
    {
92
        // We may be passed 0 or other "empty" values, so coerce to null
93
        if (empty($siteId)) {
94
            $siteId = null;
95
        }
96
        PermissionHelper::controllerPermissionCheck('webperf:delete-data-samples');
97
        if (Webperf::$plugin->dataSamples->deleteAllDataSamples($siteId)) {
98
            // Clear the caches and continue on
99
            Webperf::$plugin->clearAllCaches();
100
            Craft::$app->getSession()->setNotice(Craft::t('webperf', 'All data samples deleted.'));
101
102
            return $this->redirect(Craft::$app->getRequest()->referrer);
103
        }
104
        Craft::$app->getSession()->setError(Craft::t('webperf', "Couldn't delete data samples."));
105
106
        return $this->redirect(Craft::$app->getRequest()->referrer);
107
    }
108
}
109