FileStream   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 43
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 14 1
A init() 0 8 2
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use roaresearch\yii2\roa\FileRecord;
6
use Yii;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Access and show s the content of a file on the browser or download it.
11
 *
12
 * @author Angel (Faryshta) Guevara <[email protected]>
13
 */
14
class FileStream extends Action
15
{
16
    /**
17
     * @var string GET parameter to decide if force the download or show it on
18
     * the browser.
19
     */
20
    public $downloadParam = 'download';
21
22
    /**
23
     * @inheritdoc
24
     */
25
    public function init()
26
    {
27
        parent::init();
28
        $interfaces = class_implements($this->modelClass);
29
        if (empty($interfaces[FileRecord::class])) {
30
            throw new InvalidConfigException(
31
                "The class `{$this->modelClass}` must implement "
32
                    . FileRecord::class
33
            );
34
        }
35
    }
36
37
    /**
38
     * Shows the file on the browser or download it after checking access.
39
     *
40
     * @param mixed $id the identifier value.
41
     * @param string $ext the requested file extension.
42
     */
43
    public function run($id, string $ext)
44
    {
45
        $this->checkAccess(
46
            ($model = $this->findModel($id)),
47
            Yii::$app->request->getQueryParams()
48
        );
49
50
        return Yii::$app->response->sendFile(
51
            $model->filePath($ext),
0 ignored issues
show
Bug introduced by
The method filePath() does not exist on yii\db\ActiveRecordInterface. It seems like you code against a sub-type of yii\db\ActiveRecordInterface such as yii\db\BaseActiveRecord. ( Ignorable by Annotation )

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

51
            $model->/** @scrutinizer ignore-call */ 
52
                    filePath($ext),
Loading history...
52
            $model->fileName($ext),
0 ignored issues
show
Bug introduced by
The method fileName() does not exist on yii\db\ActiveRecordInterface. It seems like you code against a sub-type of yii\db\ActiveRecordInterface such as yii\db\BaseActiveRecord. ( Ignorable by Annotation )

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

52
            $model->/** @scrutinizer ignore-call */ 
53
                    fileName($ext),
Loading history...
53
            [
54
                'mimeType' => $model->fileMimeType($ext),
0 ignored issues
show
Bug introduced by
The method fileMimeType() does not exist on yii\db\ActiveRecordInterface. It seems like you code against a sub-type of yii\db\ActiveRecordInterface such as yii\db\BaseActiveRecord. ( Ignorable by Annotation )

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

54
                'mimeType' => $model->/** @scrutinizer ignore-call */ fileMimeType($ext),
Loading history...
55
                'inline' => !Yii::$app->request
56
                    ->getQueryParam($this->downloadParam, false),
57
            ]
58
        );
59
    }
60
}
61