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

FileinfoController   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 89
Duplicated Lines 22.47 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 6
dl 20
loc 89
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 1
A behaviors() 20 20 1
B actionIndex() 0 43 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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