Vite   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 6
eloc 51
c 10
b 0
f 0
dl 0
loc 152
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A customAdminCpCacheOptions() 0 8 1
A clearAllCaches() 0 4 1
A installEventListeners() 0 48 2
A init() 0 14 1
A createSettingsModel() 0 3 1
1
<?php
2
/**
3
 * Vite plugin for Craft CMS
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 Craft;
14
use craft\base\Model;
15
use craft\base\Plugin;
16
use craft\cloud\cli\controllers\UpController;
17
use craft\events\CancelableEvent;
18
use craft\events\RegisterCacheOptionsEvent;
19
use craft\events\TemplateEvent;
20
use craft\utilities\ClearCaches;
21
use craft\web\twig\variables\CraftVariable;
22
use craft\web\View;
23
use nystudio107\vite\models\Settings;
24
use nystudio107\vite\services\ServicesTrait;
25
use nystudio107\vite\variables\ViteVariable;
26
use yii\base\Event;
27
28
/**
29
 * Class Vite
30
 *
31
 * @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 for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
32
 * @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...
33
 * @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 for @since tag indented incorrectly; expected 3 spaces but found 5
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
    // Traits
38
    // =========================================================================
39
40
    use ServicesTrait;
41
42
    // Static Properties
43
    // =========================================================================
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var Vite
47
     */
48
    public static Plugin $plugin;
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @var string
52
     */
53
    public static string $templateName;
54
55
    // Public Properties
56
    // =========================================================================
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @var string
60
     */
61
    public string $schemaVersion = '1.0.0';
62
63
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
64
     * @var bool
65
     */
66
    public bool $hasCpSettings = false;
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
69
     * @var bool
70
     */
71
    public bool $hasCpSection = false;
72
73
    // Public Methods
74
    // =========================================================================
75
76
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
77
     * @inheritdoc
78
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
79
    public function init(): void
80
    {
81
        parent::init();
82
        self::$plugin = $this;
83
        // Add our event listeners
84
        $this->installEventListeners();
85
        // Log that the plugin has loaded
86
        Craft::info(
87
            Craft::t(
88
                'vite',
89
                '{name} plugin loaded',
90
                ['name' => $this->name]
91
            ),
92
            __METHOD__
93
        );
94
    }
95
96
    /**
97
     * Clear all the caches!
98
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
99
    public function clearAllCaches(): void
100
    {
101
        // Clear all of Vite's caches
102
        $this->vite->invalidateCaches();
103
    }
104
105
    // Protected Methods
106
    // =========================================================================
107
108
    /**
109
     * Install our event listeners.
110
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
111
    protected function installEventListeners(): void
112
    {
113
        // Register our variable
114
        Event::on(
115
            CraftVariable::class,
116
            CraftVariable::EVENT_INIT,
117
            function(Event $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
118
                /** @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...
119
                $variable = $event->sender;
120
                $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...
121
                    'class' => ViteVariable::class,
122
                    'viteService' => $this->vite,
123
                ]);
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...
124
            }
125
        );
126
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
127
        Event::on(
128
            ClearCaches::class,
129
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
130
            function(RegisterCacheOptionsEvent $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
131
                Craft::debug(
132
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
133
                    __METHOD__
134
                );
135
                // Register our caches for the Clear Cache Utility
136
                $event->options = array_merge(
137
                    $event->options,
138
                    $this->customAdminCpCacheOptions()
139
                );
140
            }
141
        );
142
        // Remember the name of the currently rendering template
143
        // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE
144
        Event::on(
145
            View::class,
146
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
147
            static function(TemplateEvent $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
148
                self::$templateName = $event->template;
149
            }
150
        );
151
        // Clears cache after craft cloud/up is run, which Craft Cloud runs on deploy
152
        // Handler: UpController::EVENT_AFTER_UP
153
        if (class_exists(UpController::class)) {
154
            Event::on(
155
                UpController::class,
156
                UpController::EVENT_AFTER_UP,
157
                function(CancelableEvent $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

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

157
                function(/** @scrutinizer ignore-unused */ CancelableEvent $event) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
158
                    $this->clearAllCaches();
159
                }
160
            );
161
        }
162
    }
163
164
    /**
165
     * Returns the custom Control Panel cache options.
166
     *
167
     * @return array
168
     */
169
    protected function customAdminCpCacheOptions(): array
170
    {
171
        return [
172
            // Manifest cache
173
            [
174
                'key' => 'vite-file-cache',
175
                'label' => Craft::t('vite', 'Vite Cache'),
176
                'action' => [$this, 'clearAllCaches'],
177
            ],
178
        ];
179
    }
180
181
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
182
     * @inheritdoc
183
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
184
    protected function createSettingsModel(): ?Model
185
    {
186
        return new Settings();
187
    }
188
}
189