Passed
Push — master ( de9f16...6836c0 )
by Aleksandr
02:35
created

FileController::out()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
4
namespace carono\exchange1c\controllers;
5
6
7
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\Filesy...n\FileNotFoundException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use yii\helpers\FileHelper;
9
10
class FileController extends Controller
11
{
12
    protected static function getAlias($path)
13
    {
14
        return \Yii::getAlias("@vendor/carono/yii2-1c-exchange/" . ltrim($path, '/'));
15
    }
16
17
    protected function out($file, $options = [])
18
    {
19
        $filename = self::getAlias($file);
20
        if (!file_exists($filename)) {
21
            throw new FileNotFoundException($filename);
22
        }
23
        $content = file_get_contents($filename);
24
        return \Yii::$app->response->sendContentAsFile($content, basename($filename), $options);
25
    }
26
27
    protected function outAsFile($file)
28
    {
29
        return $this->out($file);
30
    }
31
32
    protected function outAsImage($file)
33
    {
34
        $options = [
35
            'inline' => true,
36
            'mimeType' => FileHelper::getMimeType(self::getAlias($file))
37
        ];
38
        return $this->out($file, $options);
39
    }
40
41
    public function actionArticle($file)
42
    {
43
        return $this->outAsImage("files/articles/$file");
44
    }
45
46
    public function actionDoc($file)
47
    {
48
        return $this->outAsImage("/files/doc/$file");
49
    }
50
51
    public function actionXml($file)
52
    {
53
        return $this->outAsImage("/files/xml/$file");
54
    }
55
}