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

FileController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 44
dl 0
loc 84
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A actionExportDataSamples() 0 4 1
A exportCsvFile() 0 25 3
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\helpers\Permission as PermissionHelper;
14
15
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...
16
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...
17
18
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...
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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...
22
 * @package   Webperf
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @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...
24
 */
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...
25
class FileController extends Controller
26
{
27
    // Constants
28
    // =========================================================================
29
30
    const EXPORT_DATA_SAMPLES_COLUMNS = [
31
        'siteId',
32
        'title',
33
        'url',
34
        'queryString',
35
        'dns',
36
        'connect',
37
        'firstByte',
38
        'firstPaint',
39
        'firstContentfulPaint',
40
        'domInteractive',
41
        'pageLoad',
42
        'countryCode',
43
        'device',
44
        'browser',
45
        'os',
46
        'mobile',
47
        'craftTotalMs',
48
        'craftDbMs',
49
        'craftDbCnt',
50
        'craftTwigMs',
51
        'craftTwigCnt',
52
        'craftOtherMs',
53
        'craftOtherCnt',
54
        'craftTotalMemory',
55
    ];
56
    // Protected Properties
57
    // =========================================================================
58
59
    protected $allowAnonymous = [];
60
61
    // Public Methods
62
    // =========================================================================
63
64
    /**
65
     * Export the statistics table as a CSV file
66
     *
67
     * @throws \yii\web\ForbiddenHttpException
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function actionExportDataSamples()
70
    {
71
        PermissionHelper::controllerPermissionCheck('webperf:pages');
72
        $this->exportCsvFile('webperf-data-samples', '{{%webperf_data_samples}}', self::EXPORT_DATA_SAMPLES_COLUMNS);
73
    }
74
75
    // Public Methods
76
    // =========================================================================
77
78
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
79
     * @param string   $filename
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
80
     * @param string   $table
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     * @param array    $columns
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
82
     * @param int|null $siteId
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
83
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
84
    protected function exportCsvFile(string $filename, string $table, array $columns, int $siteId = null)
85
    {
86
        // If your CSV document was created or is read on a Macintosh computer,
87
        // add the following lines before using the library to help PHP detect line ending in Mac OS X
88
        if (!ini_get('auto_detect_line_endings')) {
89
            ini_set('auto_detect_line_endings', '1');
90
        }
91
        // Query the db table
92
        $query = (new Query())
93
            ->from([$table])
94
            ->select($columns)
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
95
            ;
96
        if ($siteId !== null) {
97
            $query
98
                ->where(['siteId' => $siteId])
0 ignored issues
show
Coding Style introduced by
Space after closing parenthesis of function call prohibited
Loading history...
99
                ;
100
        }
101
        $data = $query
102
            ->all();
103
        // Create our CSV file writer
104
        $csv = Writer::createFromFileObject(new \SplTempFileObject());
105
        $csv->insertOne($columns);
106
        $csv->insertAll($data);
107
        $csv->output($filename.'.csv');
108
        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...
109
    }
110
}
111