MediaController::actionView()   D
last analyzed

Complexity

Conditions 10
Paths 31

Size

Total Lines 39
Code Lines 23

Duplication

Lines 11
Ratio 28.21 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 11
loc 39
rs 4.8196
cc 10
eloc 23
nc 31
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace frontend\controllers;
9
10
use common\models\Media;
11
use common\models\MediaComment as Comment;
12
use Yii;
13
use yii\web\Controller;
14
use yii\web\NotFoundHttpException;
15
16
/**
17
 * Class MediaController
18
 *
19
 * @author Agiel K. Saputra <[email protected]>
20
 * @since 0.1.0
21
 */
22
class MediaController extends Controller
23
{
24
    /**
25
     * @param integer|null $id Media ID
26
     * @param string|null $slug Media slug
27
     * @return mixed
28
     * @throws \yii\web\NotFoundHttpException
29
     */
30
    public function actionView($id = null, $slug = null)
31
    {
32
        $render = 'view';
33
34
        $comment = new Comment();
35
36
        if ($id) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $id of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
37
            $model = $this->findModel($id);
38
        } elseif ($slug) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $slug of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
39
            $model = $this->findModelBySlug($slug);
40
        } else {
41
            throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
42
        }
43
44 View Code Duplication
        if ($comment->load(Yii::$app->request->post()) && $comment->save()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
            if (!$comment->parent) {
46
                $model->comment_count++;
47
            }
48
            if ($model->save()) {
0 ignored issues
show
Bug introduced by
The method save cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
49
                $this->refresh();
50
            }
51
        }
52
53 View Code Duplication
        if ($model->password && $model->password !== Yii::$app->request->post('password')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
54
            return $this->render('protected', ['media' => $model]);
55
        }
56
57
        if (is_file($this->view->theme->basePath . '/media/view-'
58
            . substr($model->mime_type, 0, strpos($model->mime_type, '/', 1)) . '.php')
59
        ) {
60
            $render = 'view-' . substr($model->mime_type, 0, strpos($model->mime_type, '/', 1));
61
        }
62
63
        return $this->render($render, [
64
            'media' => $model,
65
            'metadata' => $model->getMeta('metadata'),
0 ignored issues
show
Bug introduced by
The method getMeta cannot be called on $model (of type array|boolean).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
66
            'comment' => $comment,
67
        ]);
68
    }
69
70
    /**
71
     * Finds the Media model based on its primary key value.
72
     * If the model is not found, a 404 HTTP exception will be thrown.
73
     *
74
     * @param integer $id Media ID
75
     * @return Media the loaded model
76
     * @throws NotFoundHttpException if the model cannot be found
77
     */
78
    protected function findModel($id)
79
    {
80
        if (($model = Media::findOne($id)) !== null) {
81
            return $model;
82
        }
83
84
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
85
    }
86
87
    /**
88
     * Finds the Media model based on its primary key value.
89
     * If the model is not found, a 404 HTTP exception will be thrown.
90
     *
91
     * @param string $slug Media slug
92
     * @return Media the loaded model
93
     * @throws NotFoundHttpException if the model cannot be found
94
     */
95 View Code Duplication
    protected function findModelBySlug($slug)
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...
96
    {
97
        if (($model = Media::findOne(['slug' => $slug])) !== null) {
98
            return $model;
99
        }
100
101
        throw new NotFoundHttpException(Yii::t('writesdown', 'The requested page does not exist.'));
102
    }
103
}
104