Completed
Push — master ( ea324c...2e8bb7 )
by Andrey
01:33
created

FileinfoController::actionIndex()   B

Complexity

Conditions 9
Paths 13

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 43
c 0
b 0
f 0
rs 7.6764
nc 13
cc 9
nop 0
1
<?php
2
3
namespace Itstructure\MFUploader\controllers;
4
5
use yii\web\{Controller, BadRequestHttpException};
6
use yii\filters\{VerbFilter, AccessControl};
7
use Itstructure\MFUploader\Module;
8
use Itstructure\MFUploader\models\Mediafile;
9
use Itstructure\MFUploader\traits\ResponseTrait;
10
11
/**
12
 * Class FileinfoController
13
 * Controller class to get file information using view template.
14
 *
15
 * @property Module $module
16
 *
17
 * @package Itstructure\MFUploader\controllers
18
 *
19
 * @author Andrey Girnik <[email protected]>
20
 */
21
class FileinfoController extends Controller
22
{
23
    use ResponseTrait;
24
25
    /**
26
     * Initialize.
27
     */
28
    public function init()
29
    {
30
        $this->enableCsrfValidation = $this->module->enableCsrfValidation;
31
32
        parent::init();
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 View Code Duplication
    public function behaviors()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
39
    {
40
        return [
41
            'access' => [
42
                'class' => AccessControl::class,
43
                'rules' => [
44
                    [
45
                        'allow' => true,
46
                        'roles' => $this->module->accessRoles,
47
                    ],
48
                ],
49
            ],
50
            'verbs' => [
51
                'class' => VerbFilter::class,
52
                'actions' => [
53
                    'index' => ['POST'],
54
                ],
55
            ],
56
        ];
57
    }
58
59
    /**
60
     * Get file info.
61
     *
62
     * @throws BadRequestHttpException
63
     *
64
     * @return string
65
     */
66
    public function actionIndex()
67
    {
68
        $id = \Yii::$app->request->post('id');
69
70
        if (empty($id) || !is_numeric($id)) {
71
            throw new BadRequestHttpException(Module::t('actions', 'Parameter id must be sent.'));
72
        }
73
74
        /** @var Mediafile $model */
75
        $model = Mediafile::findOne($id);
76
77
        // Set url to set file by Java script.
78
        if ($model->isImage()) {
79
            $urlToSetFile = $model->getThumbUrl(Module::THUMB_ALIAS_MEDIUM);
80
            if (empty($urlToSetFile)) {
81
                $urlToSetFile = $model->getViewUrl();
82
            }
83
84
        } else {
85
            $urlToSetFile = $model->getViewUrl();
86
        }
87
88
        // Set width to set file by Java script.
89
        if ($model->isImage()) {
90
            $widthToSetFile = isset($this->module->thumbsConfig[Module::THUMB_ALIAS_MEDIUM]) ?
91
                $this->module->thumbsConfig[Module::THUMB_ALIAS_MEDIUM]['size'][0] : Module::ORIGINAL_PREVIEW_WIDTH;
92
93
        } elseif ($model->isAudio() || $model->isVideo()) {
94
            $widthToSetFile = Module::ORIGINAL_PREVIEW_WIDTH;
95
96
        } else {
97
            $widthToSetFile = null;
98
        }
99
100
        return $this->renderAjax('index', [
101
            'model' => $model,
102
            'urlToSetFile' => $urlToSetFile,
103
            'widthToSetFile' => $widthToSetFile,
104
            'fileAttributeName' => $this->module->fileAttributeName,
105
            'updateUrl' => Module::getUpdateUrl($this->module->defaultStorageType),
106
            'deleteUrl' => Module::getDeleteUrl($this->module->defaultStorageType),
107
        ]);
108
    }
109
}
110