Passed
Push — main ( e5d18b...282b2b )
by Sugeng
03:08
created

FileAction   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 2
b 0
f 0
dl 0
loc 44
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 22 5
A getFilesystem() 0 3 1
1
<?php
2
3
namespace diecoding\flysystem\actions;
4
5
use DateTimeImmutable;
6
use Yii;
7
use yii\base\Action;
8
use yii\helpers\Json;
9
use yii\web\NotFoundHttpException;
10
11
/**
12
 * FileAction for handle LocalComponent.
13
 *
14
 * To use FileAction, you need to do the following steps:
15
 *
16
 * First, declare an action of FileAction type in the `actions()` method of your `SiteController`
17
 * class (or whatever controller you prefer), like the following:
18
 *
19
 * ```php
20
 * public function actions()
21
 * {
22
 *     return [
23
 *         'file' => [
24
 *             'class' => \diecoding\flysystem\actions\FileAction::class,
25
 *             'component' => 'fs',
26
 *         ],
27
 *     ];
28
 * }
29
 * ```
30
 * 
31
 * @link      https://sugengsulistiyawan.my.id/
32
 * @author    Sugeng Sulistiyawan <[email protected]>
33
 * @copyright Copyright (c) 2023
34
 */
35
class FileAction extends Action
36
{
37
    /**
38
     * @var string filesystem config component (fs)
39
     */
40
    public $component = 'fs';
41
42
    /**
43
     * @return \diecoding\flysystem\AbstractComponent
44
     */
45
    public function getFilesystem()
46
    {
47
        return Yii::$app->get($this->component);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Yii::app->get($this->component) also could return the type mixed which is incompatible with the documented return type diecoding\flysystem\AbstractComponent.
Loading history...
48
    }
49
50
    /**
51
     * Runs the action.
52
     *
53
     * @param string|null $data
54
     * @return mixed result content
55
     * @throws NotFoundHttpException if data not valid
56
     */
57
    public function run($data = null)
58
    {
59
        try {
60
            $params = Json::decode($this->filesystem->decrypt($data));
0 ignored issues
show
Bug Best Practice introduced by
The property filesystem does not exist on diecoding\flysystem\actions\FileAction. Since you implemented __get, consider adding a @property annotation.
Loading history...
61
62
            $now = (int) (new DateTimeImmutable())->getTimestamp();
63
            $expires = (int) $params['expires'];
64
65
            if (!$this->getFilesystem()->fileExists($params['path']) || ($expires > 0 && $expires < $now)) {
66
                throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
67
            }
68
69
            $content = $this->getFilesystem()->read($params['path']);
70
            $mimeType = $this->getFilesystem()->mimeType($params['path']);
71
            $attachmentName = (string) pathinfo($params['path'], PATHINFO_BASENAME);
72
        } catch (\Throwable $th) {
73
            throw new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
74
        }
75
76
        return Yii::$app->getResponse()->sendContentAsFile($content, $attachmentName, [
77
            'mimeType' => $mimeType,
78
            'inline' => true,
79
        ]);
80
    }
81
}