Passed
Push — master ( 3de7ee...b534e3 )
by Mihail
14:25
created

ActionShow   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 3.66 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
dl 3
loc 82
rs 10
c 0
b 0
f 0
wmc 11
lcom 1
cbo 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C show() 3 70 11

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\Profile;
4
use Apps\ActiveRecord\Blacklist;
5
use Apps\ActiveRecord\WallPost;
6
use Apps\Model\Front\Profile\FormWallPost;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Exception\NotFoundException;
11
use Ffcms\Core\Helper\HTML\SimplePagination;
12
use Ffcms\Core\Network\Response;
13
14
/**
15
 * Trait ActionShow
16
 * @package Apps\Controller\Front\Profile
17
 * @property View $view
18
 * @property Response $response
19
 */
20
trait ActionShow
21
{
22
23
    /**
24
     * Show user profile: data, wall posts, other features
25
     * @param int $userId
26
     * @return string
27
     * @throws \Ffcms\Core\Exception\SyntaxException
28
     * @throws NotFoundException
29
     * @throws ForbiddenException
30
     */
31
    public function show($userId)
32
    {
33
        $cfg = $this->application->configs;
0 ignored issues
show
Bug introduced by
The property application does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
34 View Code Duplication
        if (!(bool)$cfg['guestView'] && !App::$User->isAuth()) {
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...
35
            throw new ForbiddenException(__('You must login to view other profile'));
36
        }
37
38
        // check if target exists
39
        if (!App::$User->isExist($userId)) {
40
            throw new NotFoundException(__('This profile is not exist'));
41
        }
42
43
        $targetPersone = App::$User->identity($userId); // target user object instance of Apps\ActiveRecord\User
44
        $viewerPersone = App::$User->identity(); // current user object(viewer) instance of Apps\ActiveRecord\User
45
46
        $wallModel = null;
47
        // if current user is auth - allow to post messages on wall current user
48
        if (App::$User->isAuth() && $viewerPersone->role->can('global/write')) {
49
            $wallModel = new FormWallPost();
50
            // check if request post is done and rules validated
51
            if ($wallModel->send() && $wallModel->validate()) {
52
                // maybe in blacklist?
53
                if (!Blacklist::check($viewerPersone->getId(), $targetPersone->getId())) {
54
                    App::$Session->getFlashBag()->add('error', __('This user are in your black list or you are in blacklist!'));
55
                } else {
56
                    // check if message added
57
                    if ($wallModel->makePost($targetPersone, $viewerPersone, (int)$cfg['delayBetweenPost'])) {
58
                        App::$Session->getFlashBag()->add('success', __('The message was successful posted!'));
59
                    } else {
60
                        App::$Session->getFlashBag()->add('warning', __('Posting message was failed! Please, wait few seconds'));
61
                    }
62
                }
63
            }
64
        }
65
66
        // pagination and query params
67
        $wallPage = (int)$this->request->query->get('page');
0 ignored issues
show
Bug introduced by
The property request does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
        $wallItems = (int)$cfg['wallPostOnPage'];
69
        $wallOffset = $wallPage * $wallItems;
70
71
        // get wall posts by target user_id
72
        $wallQuery = WallPost::where('target_id', $targetPersone->getId());
73
74
        // build pagination
75
        $wallPagination = new SimplePagination([
76
            'url' => ['profile/show', $userId, null],
77
            'page' => $wallPage,
78
            'step' => $wallItems,
79
            'total' => $wallQuery->count()
80
        ]);
81
82
        // get wall messages as object
83
        $wallRecords = $wallQuery->with(['senderUser', 'senderUser.profile', 'senderUser.role'])
84
            ->orderBy('id', 'desc')
85
            ->skip($wallOffset)
86
            ->take($wallItems)
87
            ->get();
88
89
        // render output view
90
        return $this->view->render('show', [
91
            'user' => $targetPersone,
92
            'viewer' => $viewerPersone,
93
            'isSelf' => ($viewerPersone !== null && $viewerPersone->id === $targetPersone->id),
94
            'wall' => $wallModel,
95
            'notify' => App::$Session->getFlashBag()->all(),
96
            'wallRecords' => $wallRecords,
97
            'pagination' => $wallPagination,
98
            'ratingOn' => (int)$cfg['rating'] === 1
99
        ]);
100
    }
101
}