Passed
Push — develop ( f1d7bf...0cdaf2 )
by Andrew
03:58
created

FileController::actionExportDataSamples()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 13
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 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) 2019 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\helpers\Permission as PermissionHelper;
14
15
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...
16
use craft\db\Query;
0 ignored issues
show
Bug introduced by
The type craft\db\Query 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 League\Csv\CannotInsertRecord;
0 ignored issues
show
Bug introduced by
The type League\Csv\CannotInsertRecord 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
use League\Csv\Writer;
0 ignored issues
show
Bug introduced by
The type League\Csv\Writer 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...
21
22
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
23
 * @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...
24
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
25
 * @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...
26
 */
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...
27
class FileController extends Controller
28
{
29
    // Constants
30
    // =========================================================================
31
32
    const EXPORT_DATA_SAMPLES_COLUMNS = [
33
        'siteId',
34
        'title',
35
        'url',
36
        'queryString',
37
        'dns',
38
        'connect',
39
        'firstByte',
40
        'firstPaint',
41
        'firstContentfulPaint',
42
        'domInteractive',
43
        'pageLoad',
44
        'countryCode',
45
        'device',
46
        'browser',
47
        'os',
48
        'mobile',
49
        'craftTotalMs',
50
        'craftDbMs',
51
        'craftDbCnt',
52
        'craftTwigMs',
53
        'craftTwigCnt',
54
        'craftOtherMs',
55
        'craftOtherCnt',
56
        'craftTotalMemory',
57
    ];
58
59
    const EXPORT_ERROR_SAMPLES_COLUMNS = [
60
        'siteId',
61
        'title',
62
        'url',
63
        'queryString',
64
        'type',
65
        'pageErrors',
66
    ];
67
68
    // Protected Properties
69
    // =========================================================================
70
71
    protected $allowAnonymous = [];
72
73
    // Public Methods
74
    // =========================================================================
75
76
    /**
77
     * Export the data samples table as a CSV file
78
     *
79
     * @param string   $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
80
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     *
82
     * @throws \yii\web\ForbiddenHttpException
83
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
    public function actionExportDataSamples(string $pageUrl = '', int $siteId = null)
85
    {
86
        PermissionHelper::controllerPermissionCheck('webperf:performance');
87
        try {
88
            $this->exportCsvFile(
89
                'webperf-data-samples',
90
                '{{%webperf_data_samples}}',
91
                self::EXPORT_DATA_SAMPLES_COLUMNS,
92
                $pageUrl,
93
                $siteId
94
            );
95
        } catch (CannotInsertRecord $e) {
96
            Craft::error($e->getMessage(), __METHOD__);
97
        }
98
    }
99
100
    /**
101
     * Export the error samples table as a CSV file
102
     *
103
     * @param string   $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
104
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
105
     *
106
     * @throws \yii\web\ForbiddenHttpException
107
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
108
    public function actionExportErrorSamples(string $pageUrl = '', int $siteId = null)
109
    {
110
        PermissionHelper::controllerPermissionCheck('webperf:errors');
111
        try {
112
            $this->exportCsvFile(
113
                'webperf-error-samples',
114
                '{{%webperf_error_samples}}',
115
                self::EXPORT_ERROR_SAMPLES_COLUMNS,
116
                $pageUrl,
117
                $siteId
118
            );
119
        } catch (CannotInsertRecord $e) {
120
            Craft::error($e->getMessage(), __METHOD__);
121
        }
122
    }
123
124
    // Public Methods
125
    // =========================================================================
126
127
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
128
     * @param string   $filename
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
129
     * @param string   $table
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
130
     * @param array    $columns
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
131
     * @param string   $pageUrl
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
132
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
133
     *
134
     * @throws \League\Csv\CannotInsertRecord
135
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
136
    protected function exportCsvFile(
137
        string $filename,
138
        string $table,
139
        array $columns,
140
        string $pageUrl = '',
141
        int $siteId = null
142
    ) {
143
        // If your CSV document was created or is read on a Macintosh computer,
144
        // add the following lines before using the library to help PHP detect line ending in Mac OS X
145
        if (!ini_get('auto_detect_line_endings')) {
146
            ini_set('auto_detect_line_endings', '1');
147
        }
148
        // Query the db table
149
        $query = (new Query())
150
            ->from([$table])
151
            ->select($columns)
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
152
            ;
153
        if ($siteId !== null) {
154
            $query
155
                ->where(['siteId' => $siteId])
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
156
            ;
157
        }
158
        if (!empty($pageUrl)) {
159
            $query
160
                ->where(['pageUrl' => $pageUrl])
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
161
            ;
162
        }
163
        $data = $query
164
            ->all();
165
        // Create our CSV file writer
166
        $csv = Writer::createFromFileObject(new \SplTempFileObject());
167
        $csv->insertOne($columns);
168
        $csv->insertAll($data);
169
        $csv->output($filename.'.csv');
170
        exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
171
    }
172
}
173