Passed
Push — master ( b534e3...17b30a )
by Mihail
19:51
created

ActionUpdate   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 52
Duplicated Lines 5.77 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 3
loc 52
rs 10
wmc 9
lcom 1
cbo 6

1 Method

Rating   Name   Duplication   Size   Complexity  
D update() 3 41 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 Apps\Controller\Front\Content;
4
5
use Apps\Model\Front\Content\FormNarrowContentUpdate;
6
use Ffcms\Core\App;
7
use Ffcms\Core\Arch\View;
8
use Ffcms\Core\Exception\ForbiddenException;
9
use Ffcms\Core\Exception\NotFoundException;
10
use Ffcms\Core\Network\Request;
11
use Ffcms\Core\Network\Response;
12
use Apps\ActiveRecord\Content as ContentRecord;
13
14
/**
15
 * Trait ActionUpdate
16
 * @package Apps\Controller\Front\Content
17
 * @property View $view
18
 * @property Request $request
19
 * @property Response $response
20
 * @method array getConfigs
21
 */
22
trait ActionUpdate
23
{
24
    /**
25
     * Update personal content items or add new content item
26
     * @param int|null $id
27
     * @return null|string
28
     * @throws ForbiddenException
29
     * @throws NotFoundException
30
     * @throws \Ffcms\Core\Exception\SyntaxException
31
     */
32
    public function update($id = null)
33
    {
34
        // check if user is auth
35
        if (!App::$User->isAuth()) {
36
            throw new ForbiddenException(__('Only authorized users can add content'));
37
        }
38
39
        // check if user add enabled
40
        $configs = $this->getConfigs();
0 ignored issues
show
Bug introduced by
It seems like getConfigs() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
41
        if (!(bool)$configs['userAdd']) {
42
            throw new NotFoundException(__('User add is disabled'));
43
        }
44
45
        // find record in db
46
        $record = ContentRecord::findOrNew($id);
47
        $new = $record->id === null;
48
49
        // reject edit published items and items from other authors
50 View Code Duplication
        if (($new === false && (int)$record->author_id !== App::$User->identity()->getId()) || (int)$record->display === 1) {
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...
51
            throw new ForbiddenException(__('You have no permissions to edit this content'));
52
        }
53
54
        // initialize model
55
        $model = new FormNarrowContentUpdate($record, $configs);
56
        if ($model->send() && $model->validate()) {
57
            $model->make();
58
            // if is new - make redirect to listing & add notify
59
            if ($new === true) {
60
                App::$Session->getFlashBag()->add('success', __('Content successfully added'));
61
                $this->response->redirect('content/my');
62
            } else {
63
                App::$Session->getFlashBag()->add('success', __('Content successfully updated'));
64
            }
65
        }
66
67
        // render view output
68
        return $this->view->render('update', [
69
            'model' => $model,
70
            'configs' => $configs
71
        ]);
72
    }
73
}
74