Completed
Push — master ( 6db6da...5370d9 )
by Angel
03:42
created

FileStream::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace roaresearch\yii2\roa\actions;
4
5
use roaresearch\yii2\roa\FileRecord;
6
7
use Yii;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * Access and show s the content of a file on the browser or download it.
12
 *
13
 * @author Angel (Faryshta) Guevara <[email protected]>
14
 */
15
class FileStream extends Action
16
{
17
    /**
18
     * @var string GET parameter to decide if force the download or show it on
19
     * the browser.
20
     */
21
    public $downloadParam = 'download';
22
23
    /**
24
     * @inheritdoc
25
     */
26
    public function init()
27
    {
28
        parent::init();
29
        $interfaces = class_implements($this->modelClass);
30
        if (empty($interfaces[FileRecord::class])) {
31
            throw new InvalidConfigException(
32
                "The class `{$this->modelClass}` must implement "
33
                    . FileRecord::class
34
            );
35
        }
36
    }
37
38
    /**
39
     * Shows the file on the browser or download it after checking access.
40
     *
41
     * @param mixed $id the identifier value.
42
     * @param string $ext the requested file extension.
43
     */
44
    public function run($id, string $ext)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
45
    {
46
        $this->checkAccess(
47
            ($model = $this->findModel($id)),
48
            Yii::$app->request->queryParams
49
        );
50
51
        return Yii::$app->response->sendFile(
52
            $model->filePath($ext),
0 ignored issues
show
Bug introduced by
The method filePath() does not seem to exist on object<yii\db\ActiveRecordInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            $model->fileName($ext),
0 ignored issues
show
Bug introduced by
The method fileName() does not seem to exist on object<yii\db\ActiveRecordInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
            [
55
                'mimeType' => $model->fileMimeType($ext),
0 ignored issues
show
Bug introduced by
The method fileMimeType() does not seem to exist on object<yii\db\ActiveRecordInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
                'inline' => !Yii::$app->request
57
                    ->getQueryParam($this->downloadParam, false),
58
            ]
59
        );
60
    }
61
}
62