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

ActionIgnore::ignore()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 55
Code Lines 30

Duplication

Lines 7
Ratio 12.73 %

Importance

Changes 0
Metric Value
cc 7
eloc 30
nc 10
nop 0
dl 7
loc 55
rs 7.8235
c 0
b 0
f 0

How to fix   Long Method   

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
namespace Apps\Controller\Front\Profile;
4
5
use Apps\ActiveRecord\Blacklist;
6
use Apps\Model\Front\Profile\FormIgnoreAdd;
7
use Ffcms\Core\App;
8
use Ffcms\Core\Arch\View;
9
use Ffcms\Core\Exception\ForbiddenException;
10
use Ffcms\Core\Helper\HTML\SimplePagination;
11
use Ffcms\Core\Network\Response;
12
use Ffcms\Core\Network\Request;
13
14
/**
15
 * Trait ActionIgnore
16
 * @package Apps\Controller\Front\Profile
17
 * @property View $view
18
 * @property Request $request
19
 * @property Response $response
20
 */
21
trait ActionIgnore
22
{
23
    private $_blockPerPage;
24
25
    /**
26
     * Show users in blacklist and allow add new users
27
     * @return string
28
     * @throws \Ffcms\Core\Exception\SyntaxException
29
     * @throws ForbiddenException
30
     */
31
    public function ignore()
32
    {
33
        // check if not auth
34
        if (!App::$User->isAuth()) {
35
            throw new ForbiddenException();
36
        }
37
38
        // get user object and init model of it
39
        $user = App::$User->identity();
40
        $model = new FormIgnoreAdd($user);
41
42
        // set user id from ?id= get param if form not sended
43
        if (!$model->send()) {
44
            $uid = (int)$this->request->query->get('id');
45
            if ($uid > 0) {
46
                $model->id = $uid;
47
            }
48
        }
49
50
        // sended new block add?
51 View Code Duplication
        if ($model->send() && $model->validate()) {
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...
52
            if ($model->save()) {
53
                App::$Session->getFlashBag()->add('success', __('User was successful blocked!'));
54
            } else {
55
                App::$Session->getFlashBag()->add('error', __('This user is always in ignore list'));
56
            }
57
        }
58
59
        // get blocked users
60
        $query = Blacklist::where('user_id', '=', $user->getId());
61
62
        $page = (int)$this->request->query->get('page');
63
        $offset = $page * $this->_blockPerPage;
64
65
        // build pagination
66
        $pagination = new SimplePagination([
67
            'url' => ['profile/ignore'],
68
            'page' => $page,
69
            'step' => $this->_blockPerPage,
70
            'total' => $query->count()
71
        ]);
72
73
        // get records as object
74
        $records = $query->with(['targetUser', 'targetUser.profile'])
75
            ->skip($offset)
76
            ->take($this->_blockPerPage)
77
            ->get();
78
79
        // render output view
80
        return $this->view->render('ignore', [
81
            'records' => $records,
82
            'model' => $model,
83
            'pagination' => $pagination
84
        ]);
85
    }
86
}