Passed
Push — v1 ( c4e279...3fd45a )
by Andrew
16:46 queued 09:49
created

Transcoder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 14
rs 10
1
<?php
2
/**
3
 * Transcoder plugin for Craft CMS 3.x
4
 *
5
 * Transcode videos to various formats, and provide thumbnails of the video
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) 2017 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\transcoder;
12
13
use nystudio107\transcoder\assetbundles\transcoder\TranscoderAsset;
14
use nystudio107\transcoder\services\Transcode;
15
use nystudio107\transcoder\variables\TranscoderVariable;
16
use nystudio107\transcoder\models\Settings;
17
18
use nystudio107\pluginmanifest\services\ManifestService;
19
20
use Craft;
21
use craft\base\Plugin;
22
use craft\console\Application as ConsoleApplication;
23
use craft\elements\Asset;
24
use craft\events\AssetThumbEvent;
25
use craft\events\PluginEvent;
26
use craft\events\RegisterUrlRulesEvent;
27
use craft\events\RegisterCacheOptionsEvent;
28
use craft\helpers\Assets as AssetsHelper;
29
use craft\helpers\FileHelper;
30
use craft\helpers\UrlHelper;
31
use craft\services\Assets;
32
use craft\services\Plugins;
33
use craft\utilities\ClearCaches;
34
use craft\web\twig\variables\CraftVariable;
35
use craft\web\UrlManager;
36
37
use yii\base\ErrorException;
38
use yii\base\Event;
39
40
/**
41
 * Class Transcode
42
 *
43
 * @author    nystudio107
0 ignored issues
show
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...
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
44
 * @package   Transcode
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
45
 * @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...
46
 *
47
 * @property Transcode          $transcode
48
 * @property Settings           $settings
49
 * @property ManifestService    $manifest
50
 * @method   Settings   getSettings()
51
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
52
class Transcoder extends Plugin
53
{
54
    // Static Properties
55
    // =========================================================================
56
57
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
58
     * @var Transcoder
59
     */
60
    public static $plugin;
61
62
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
63
     * @var Settings
64
     */
65
    public static $settings;
66
67
    // Static Methods
68
    // =========================================================================
69
70
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $id 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...
71
     * @inheritdoc
72
     */
73
    public function __construct($id, $parent = null, array $config = [])
74
    {
75
        $config['components'] = [
76
            'transcode' => Transcode::class,
77
            // Register the manifest service
78
            'manifest' => [
79
                'class' => ManifestService::class,
80
                'assetClass' => TranscoderAsset::class,
81
                'devServerManifestPath' => 'http://craft-transcoder-buildchain:8080/',
82
                'devServerPublicPath' => 'http://craft-transcoder-buildchain:8080/',
83
            ],
84
        ];
85
86
        parent::__construct($id, $parent, $config);
87
    }
88
89
    // Public Properties
90
    // =========================================================================
91
92
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
93
     * @var string
94
     */
95
    public $schemaVersion = '1.0.0';
96
97
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
98
     * @var bool
99
     */
100
    public $hasCpSection = false;
101
102
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
103
     * @var bool
104
     */
105
    public $hasCpSettings = false;
106
107
    // Public Methods
108
    // =========================================================================
109
110
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
111
     * @inheritdoc
112
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
113
    public function init()
114
    {
115
        parent::init();
116
        self::$plugin = $this;
117
        // Initialize properties
118
        self::$settings = self::$plugin->getSettings();
119
        // Handle console commands
120
        if (Craft::$app instanceof ConsoleApplication) {
121
            $this->controllerNamespace = 'nystudio107\transcoder\console\controllers';
122
        }
123
        // Add in our Craft components
124
        $this->addComponents();
125
        // Install our global event handlers
126
        $this->installEventHandlers();
127
        // We've loaded!
128
        Craft::info(
129
            Craft::t(
130
                'transcoder',
131
                '{name} plugin loaded',
132
                ['name' => $this->name]
133
            ),
134
            __METHOD__
135
        );
136
    }
137
138
    /**
139
     * Clear all the caches!
140
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
141
    public function clearAllCaches()
142
    {
143
        $transcoderPaths = self::$plugin->getSettings()->transcoderPaths;
144
145
        foreach ($transcoderPaths as $key => $value) {
146
            $dir = Craft::parseEnv($value);
147
            try {
148
                FileHelper::clearDirectory($dir);
0 ignored issues
show
Bug introduced by
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

148
                FileHelper::clearDirectory(/** @scrutinizer ignore-type */ $dir);
Loading history...
149
                Craft::info(
150
                    Craft::t(
151
                        'transcoder',
152
                        '{name} cache directory cleared',
153
                        ['name' => $key]
154
                    ),
155
                    __METHOD__
156
                );
157
            } catch (ErrorException $e) {
158
                // the directory doesn't exist
159
                Craft::error($e->getMessage(), __METHOD__);
160
            }
161
        }
162
    }
163
164
    // Protected Methods
165
    // =========================================================================
166
167
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
168
     * @inheritdoc
169
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
170
    protected function createSettingsModel()
171
    {
172
        return new Settings();
173
    }
174
175
    /**
176
     * Add in our Craft components
177
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
178
    protected function addComponents()
179
    {
180
        // Register our variables
181
        Event::on(
182
            CraftVariable::class,
183
            CraftVariable::EVENT_INIT,
184
            function (Event $event) {
185
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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...
186
                $variable = $event->sender;
187
                $variable->set('transcoder', [
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...
188
                    'class' => TranscoderVariable::class,
189
                    'manifestService' => $this->manifest,
190
                ]);
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...
191
            }
192
        );
193
    }
194
195
    /**
196
     * Install our event handlers
197
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
198
    protected function installEventHandlers()
199
    {
200
        $settings = $this->getSettings();
201
        // Handler: Assets::EVENT_GET_THUMB_PATH
202
        Event::on(
203
            Assets::class,
204
            Assets::EVENT_GET_THUMB_PATH,
205
            function (AssetThumbEvent $event) {
206
                Craft::debug(
207
                    'Assets::EVENT_GET_THUMB_PATH',
208
                    __METHOD__
209
                );
210
                /** @var Asset $asset */
0 ignored issues
show
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
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...
211
                $asset = $event->asset;
212
                if (AssetsHelper::getFileKindByExtension($asset->filename) === Asset::KIND_VIDEO) {
0 ignored issues
show
Bug introduced by
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

212
                if (AssetsHelper::getFileKindByExtension(/** @scrutinizer ignore-type */ $asset->filename) === Asset::KIND_VIDEO) {
Loading history...
213
                    $path = Transcoder::$plugin->transcode->handleGetAssetThumbPath($event);
214
                    if (!empty($path)) {
215
                        $event->path = $path;
216
                    }
217
                }
218
            }
219
        );
220
        if ($settings->clearCaches) {
221
            // Add the Transcode path to the list of things the Clear Caches tool can delete.
222
            Event::on(
223
                ClearCaches::class,
224
                ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
225
                function (RegisterCacheOptionsEvent $event) {
226
                    $event->options[] = [
227
                        'key' => 'transcoder',
228
                        'label' => Craft::t('transcoder', 'Transcoder caches'),
229
                        'action' => [$this, 'clearAllCaches'],
230
                    ];
231
                }
232
            );
233
        }
234
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
235
        Event::on(
236
            Plugins::class,
237
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
238
            function (PluginEvent $event) {
239
                if ($event->plugin === $this) {
240
                    $request = Craft::$app->getRequest();
241
                    if ($request->isCpRequest) {
242
                        Craft::$app->getResponse()->redirect(UrlHelper::cpUrl('transcoder/welcome'))->send();
243
                    }
244
                }
245
            }
246
        );
247
        $request = Craft::$app->getRequest();
248
        // Install only for non-console site requests
249
        if ($request->getIsSiteRequest() && !$request->getIsConsoleRequest()) {
250
            $this->installSiteEventListeners();
251
        }
252
    }
253
254
    /**
255
     * Install site event listeners for site requests only
256
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
257
    protected function installSiteEventListeners()
258
    {
259
        // Handler: UrlManager::EVENT_REGISTER_SITE_URL_RULES
260
        Event::on(
261
            UrlManager::class,
262
            UrlManager::EVENT_REGISTER_SITE_URL_RULES,
263
            function (RegisterUrlRulesEvent $event) {
264
                Craft::debug(
265
                    'UrlManager::EVENT_REGISTER_SITE_URL_RULES',
266
                    __METHOD__
267
                );
268
                // Register our Control Panel routes
269
                $event->rules = array_merge(
270
                    $event->rules,
271
                    $this->customFrontendRoutes()
272
                );
273
            }
274
        );
275
    }
276
277
    /**
278
     * Return the custom frontend routes
279
     *
280
     * @return array
281
     */
282
    protected function customFrontendRoutes(): array
283
    {
284
        return [
285
        ];
286
    }
287
}
288