Completed
Push — master ( 938584...21729c )
by Carlos
04:30
created

ProfileFileStream::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
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 ProfileFileStream extends \yii\rest\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
    }
29
30
    /**
31
     * Shows the file on the browser or download it after checking access.
32
     *
33
     * @param mixed $id the identifier value.
34
     * @param string $ext the requested file extension.
35
     */
36
    public function run(string $ext)
37
    {
38
        $model = Yii::$app->user->identity;
39
        if (!$model instanceof FileRecord) {
40
            throw new InvalidConfigException(
41
                "The user class must implement " . FileRecord::class
42
            );
43
        }
44
45
        return Yii::$app->response->sendFile(
46
            $model->filePath($ext),
47
            $model->fileName($ext),
48
            [
49
                'mimeType' => $model->fileMimeType($ext),
50
                'inline' => !Yii::$app->request
51
                    ->getQueryParam($this->downloadParam, false),
52
            ]
53
        );
54
    }
55
}
56
57