Passed
Push — v1 ( 06db2b...43a7dc )
by Andrew
12:59 queued 05:51
created

Vite::injectErrorEntry()   B

Complexity

Conditions 9
Paths 11

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 19
rs 8.0555
cc 9
nc 11
nop 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Vite::createSettingsModel() 0 3 1
1
<?php
2
/**
3
 * Vite plugin for Craft CMS 3.x
4
 *
5
 * Allows the use of the Vite.js next generation frontend tooling with Craft CMS
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) 2021 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\vite;
12
13
use nystudio107\vite\models\Settings;
14
use nystudio107\vite\variables\ViteVariable;
15
16
use nystudio107\pluginvite\services\ViteService;
17
18
use Craft;
19
use craft\base\Plugin;
20
use craft\events\RegisterCacheOptionsEvent;
21
use craft\utilities\ClearCaches;
22
use craft\web\twig\variables\CraftVariable;
23
24
use yii\base\Event;
25
26
/**
27
 * Class Vite
28
 *
29
 * @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
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
30
 * @package   Vite
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
31
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
32
 *
33
 * @property  ViteService $vite
0 ignored issues
show
Coding Style introduced by
Tag value for @property tag indented incorrectly; expected 1 spaces but found 2
Loading history...
34
 */
0 ignored issues
show
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @category tag in class comment
Loading history...
35
class Vite extends Plugin
36
{
37
    // Static Properties
38
    // =========================================================================
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
41
     * @var Vite
42
     */
43
    public static $plugin;
44
45
    // Static Methods
46
    // =========================================================================
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $id should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
49
     * @inheritdoc
50
     */
51
    public function __construct($id, $parent = null, array $config = [])
52
    {
53
        $config['components'] = [
54
            'vite' => [
55
                'class' => ViteService::class,
56
            ]
57
        ];
58
59
        parent::__construct($id, $parent, $config);
60
    }
61
62
    // Public Properties
63
    // =========================================================================
64
65
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
66
     * @var string
67
     */
68
    public $schemaVersion = '1.0.0';
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @var bool
72
     */
73
    public $hasCpSettings = false;
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
76
     * @var bool
77
     */
78
    public $hasCpSection = false;
79
80
    // Public Methods
81
    // =========================================================================
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
84
     * @inheritdoc
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function init()
87
    {
88
        parent::init();
89
        self::$plugin = $this;
90
91
        // Configure our Vite service with the settings
92
        $settings = $this->getSettings();
93
        if ($settings) {
94
            $settingsAttrs = $settings->getAttributes();
95
            $viteAttrs = $this->vite->getAttributes();
96
            Craft::configure($this->vite, array_intersect_key(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
97
                $settingsAttrs,
98
                $viteAttrs
99
            ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
100
        }
101
        // Register our variable
102
        Event::on(
103
            CraftVariable::class,
104
            CraftVariable::EVENT_INIT,
105
            function (Event $event) {
106
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
107
                $variable = $event->sender;
108
                $variable->set('vite', [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
109
                    'class' => ViteVariable::class,
110
                    'viteService' => $this->vite,
111
                ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
112
            }
113
        );
114
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
115
        Event::on(
116
            ClearCaches::class,
117
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
118
            function (RegisterCacheOptionsEvent $event) {
119
                Craft::debug(
120
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
121
                    __METHOD__
122
                );
123
                // Register our caches for the Clear Cache Utility
124
                $event->options = array_merge(
125
                    $event->options,
126
                    $this->customAdminCpCacheOptions()
127
                );
128
            }
129
        );
130
        // Log that the plugin has loaded
131
        Craft::info(
132
            Craft::t(
133
                'vite',
134
                '{name} plugin loaded',
135
                ['name' => $this->name]
136
            ),
137
            __METHOD__
138
        );
139
    }
140
141
    /**
142
     * Clear all the caches!
143
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
144
    public function clearAllCaches()
145
    {
146
        // Clear all of Vite's caches
147
        $this->vite->invalidateCaches();
148
    }
149
150
    // Protected Methods
151
    // =========================================================================
152
153
    /**
154
     * Returns the custom Control Panel cache options.
155
     *
156
     * @return array
157
     */
158
    protected function customAdminCpCacheOptions(): array
159
    {
160
        return [
161
            // Manifest cache
162
            [
163
                'key' => 'vite-file-cache',
164
                'label' => Craft::t('vite', 'Vite Cache'),
165
                'action' => [$this, 'clearAllCaches'],
166
            ],
167
        ];
168
    }
169
170
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
171
     * @inheritdoc
172
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
173
    protected function createSettingsModel()
174
    {
175
        return new Settings();
176
    }
177
}
178