Issues (306)

src/Transcoder.php (40 issues)

1
<?php
2
/**
3
 * Transcoder plugin for Craft CMS
4
 *
5
 * Transcode videos to various formats, and provide thumbnails of the video
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\transcoder;
12
13
use Craft;
14
use craft\base\Plugin;
15
use craft\console\Application as ConsoleApplication;
16
use craft\elements\Asset;
17
use craft\events\AssetThumbEvent;
18
use craft\events\PluginEvent;
19
use craft\events\RegisterCacheOptionsEvent;
20
use craft\events\RegisterUrlRulesEvent;
21
use craft\helpers\Assets as AssetsHelper;
22
use craft\helpers\FileHelper;
23
use craft\helpers\UrlHelper;
24
use craft\services\Assets;
25
use craft\services\Plugins;
26
use craft\utilities\ClearCaches;
27
use craft\web\twig\variables\CraftVariable;
28
use craft\web\UrlManager;
29
use nystudio107\transcoder\models\Settings;
30
use nystudio107\transcoder\services\ServicesTrait;
31
use nystudio107\transcoder\variables\TranscoderVariable;
32
use yii\base\ErrorException;
33
use yii\base\Event;
34
35
/**
36
 * Class Transcode
37
 *
38
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
39
 * @package   Transcode
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
40
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
41
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
42
class Transcoder extends Plugin
43
{
44
    // Traits
45
    // =========================================================================
46
47
    use ServicesTrait;
48
49
    // Static Properties
50
    // =========================================================================
51
52
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
53
     * @var Transcoder
54
     */
55
    public static $plugin;
56
57
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
58
     * @var Settings
59
     */
60
    public static $settings;
61
62
    // Public Properties
63
    // =========================================================================
64
65
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
66
     * @var string
67
     */
68
    public $schemaVersion = '1.0.0';
69
70
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
71
     * @var bool
72
     */
73
    public $hasCpSection = false;
74
75
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
76
     * @var bool
77
     */
78
    public $hasCpSettings = false;
79
80
    // Public Methods
81
    // =========================================================================
82
83
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
84
     * @inheritdoc
85
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
86
    public function init()
87
    {
88
        parent::init();
89
        self::$plugin = $this;
90
        // Initialize properties
91
        self::$settings = self::$plugin->getSettings();
92
        // Handle console commands
93
        if (Craft::$app instanceof ConsoleApplication) {
94
            $this->controllerNamespace = 'nystudio107\transcoder\console\controllers';
95
        }
96
        // Add in our Craft components
97
        $this->addComponents();
98
        // Install our global event handlers
99
        $this->installEventHandlers();
100
        // We've loaded!
101
        Craft::info(
102
            Craft::t(
103
                'transcoder',
104
                '{name} plugin loaded',
105
                ['name' => $this->name]
106
            ),
107
            __METHOD__
108
        );
109
    }
110
111
    /**
112
     * Clear all the caches!
113
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
114
    public function clearAllCaches()
115
    {
116
        $transcoderPaths = self::$plugin->getSettings()->transcoderPaths;
117
118
        foreach ($transcoderPaths as $key => $value) {
119
            $dir = Craft::parseEnv($value);
0 ignored issues
show
Deprecated Code introduced by
The function Craft::parseEnv() has been deprecated: in 3.7.29. [[App::parseEnv()]] should be used instead. ( Ignorable by Annotation )

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

119
            $dir = /** @scrutinizer ignore-deprecated */ Craft::parseEnv($value);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
120
            try {
121
                FileHelper::clearDirectory($dir);
0 ignored issues
show
It seems like $dir can also be of type boolean and null; however, parameter $dir of craft\helpers\FileHelper::clearDirectory() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

121
                FileHelper::clearDirectory(/** @scrutinizer ignore-type */ $dir);
Loading history...
122
                Craft::info(
123
                    Craft::t(
124
                        'transcoder',
125
                        '{name} cache directory cleared',
126
                        ['name' => $key]
127
                    ),
128
                    __METHOD__
129
                );
130
            } catch (ErrorException $e) {
131
                // the directory doesn't exist
132
                Craft::error($e->getMessage(), __METHOD__);
133
            }
134
        }
135
    }
136
137
    // Protected Methods
138
    // =========================================================================
139
140
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
141
     * @inheritdoc
142
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
143
    protected function createSettingsModel()
144
    {
145
        return new Settings();
146
    }
147
148
    /**
149
     * Add in our Craft components
150
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
151
    protected function addComponents()
152
    {
153
        // Register our variables
154
        Event::on(
155
            CraftVariable::class,
156
            CraftVariable::EVENT_INIT,
157
            function (Event $event) {
158
                /** @var CraftVariable $variable */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
159
                $variable = $event->sender;
160
                $variable->set('transcoder', [
0 ignored issues
show
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
161
                    'class' => TranscoderVariable::class,
162
                    'viteService' => $this->vite,
163
                ]);
0 ignored issues
show
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...
164
            }
165
        );
166
    }
167
168
    /**
169
     * Install our event handlers
170
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
171
    protected function installEventHandlers()
172
    {
173
        $settings = $this->getSettings();
174
        // Handler: Assets::EVENT_GET_THUMB_PATH
175
        Event::on(
176
            Assets::class,
177
            Assets::EVENT_GET_THUMB_PATH,
178
            function (AssetThumbEvent $event) {
179
                Craft::debug(
180
                    'Assets::EVENT_GET_THUMB_PATH',
181
                    __METHOD__
182
                );
183
                /** @var Asset $asset */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
184
                $asset = $event->asset;
185
                if (AssetsHelper::getFileKindByExtension($asset->filename) === Asset::KIND_VIDEO) {
0 ignored issues
show
It seems like $asset->filename can also be of type null; however, parameter $file of craft\helpers\Assets::getFileKindByExtension() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

185
                if (AssetsHelper::getFileKindByExtension(/** @scrutinizer ignore-type */ $asset->filename) === Asset::KIND_VIDEO) {
Loading history...
186
                    $path = Transcoder::$plugin->transcode->handleGetAssetThumbPath($event);
187
                    if (!empty($path)) {
188
                        $event->path = $path;
189
                    }
190
                }
191
            }
192
        );
193
        if ($settings->clearCaches) {
194
            // Add the Transcode path to the list of things the Clear Caches tool can delete.
195
            Event::on(
196
                ClearCaches::class,
197
                ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
198
                function (RegisterCacheOptionsEvent $event) {
199
                    $event->options[] = [
200
                        'key' => 'transcoder',
201
                        'label' => Craft::t('transcoder', 'Transcoder caches'),
202
                        'action' => [$this, 'clearAllCaches'],
203
                    ];
204
                }
205
            );
206
        }
207
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
208
        Event::on(
209
            Plugins::class,
210
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
211
            function (PluginEvent $event) {
212
                if ($event->plugin === $this) {
213
                    $request = Craft::$app->getRequest();
214
                    if ($request->isCpRequest) {
215
                        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('transcoder/welcome'))->send();
216
                    }
217
                }
218
            }
219
        );
220
        $request = Craft::$app->getRequest();
221
        // Install only for non-console site requests
222
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
223
            $this->installSiteEventListeners();
224
        }
225
    }
226
227
    /**
228
     * Install site event listeners for site requests only
229
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
230
    protected function installSiteEventListeners()
231
    {
232
        // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
233
        Event::on(
234
            UrlManager::class,
235
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
236
            function (RegisterUrlRulesEvent $event) {
237
                Craft::debug(
238
                    'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
239
                    __METHOD__
240
                );
241
                // Register our Control Panel routes
242
                $event->rules = array_merge(
243
                    $event->rules,
244
                    $this->customFrontendRoutes()
245
                );
246
            }
247
        );
248
    }
249
250
    /**
251
     * Return the custom frontend routes
252
     *
253
     * @return array
254
     */
255
    protected function customFrontendRoutes(): array
256
    {
257
        return [
258
        ];
259
    }
260
}
261