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

ActionIndex   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 80
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 5

1 Method

Rating   Name   Duplication   Size   Complexity  
C index() 0 69 12
1
<?php
2
3
namespace Apps\Controller\Front\Profile;
4
5
use Ffcms\Core\Arch\View;
6
use Ffcms\Core\Exception\NotFoundException;
7
use Apps\ActiveRecord\Profile as ProfileRecords;
8
use Ffcms\Core\Helper\HTML\SimplePagination;
9
use Ffcms\Core\Helper\Type\Any;
10
use Ffcms\Core\Helper\Type\Str;
11
use Ffcms\Core\Network\Response;
12
13
/**
14
 * Trait ActionIndex. Index action in Profile controller.
15
 * @package Apps\Controller\Front\Profile
16
 * @property Response $response
17
 * @property View $view
18
 */
19
trait ActionIndex
20
{
21
    /**
22
     * List user profiles on website by defined filter
23
     * @param string $name
24
     * @param null|string|int $value
25
     * @return string
26
     * @throws NotFoundException
27
     * @throws \Ffcms\Core\Exception\SyntaxException
28
     */
29
    public function index($name, $value = null)
30
    {
31
        $records = null;
32
        // set current page and offset
33
        $page = (int)$this->request->query->get('page', 0);
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...
34
        $cfgs = $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...
35
        $userPerPage = (int)$cfgs['usersOnPage'];
36
        if ($userPerPage < 1) {
37
            $userPerPage = 1;
38
        }
39
        $offset = $page * $userPerPage;
40
41
        switch ($name) {
42
            case 'rating': // rating list, order by rating DESC
43
                // check if rating is enabled
44
                if ((int)$cfgs['rating'] !== 1) {
45
                    throw new NotFoundException();
46
                }
47
                $records = (new ProfileRecords())->orderBy('rating', 'DESC');
48
                break;
49
            case 'hobby': // search by hobby
50
                if (Str::likeEmpty($value)) {
51
                    throw new NotFoundException();
52
                }
53
                $records = (new ProfileRecords())->where('hobby', 'like', '%' . $value . '%');
54
                break;
55
            case 'city':
56
                if (Str::likeEmpty($value)) {
57
                    throw new NotFoundException();
58
                }
59
                $records = (new ProfileRecords())->where('city', $value);
60
                break;
61
            case 'born':
62
                if ($value === null || !Any::isInt($value)) {
63
                    throw new NotFoundException();
64
                }
65
                $records = (new ProfileRecords())->where('birthday', 'like', $value . '-%');
66
                break;
67
            case 'all':
68
                $records = (new ProfileRecords())->orderBy('id', 'DESC');
69
                break;
70
            default:
71
                $this->response->redirect('profile/index/all');
72
                break;
73
        }
74
75
        // build pagination
76
        $pagination = new SimplePagination([
77
            'url' => ['profile/index', $name, $value],
78
            'page' => $page,
79
            'step' => $userPerPage,
80
            'total' => $records->count()
81
        ]);
82
83
        // get profile list with relation for user and role tables in 1 query
84
        $profiles = $records->with(['user', 'user.role'])
85
            ->skip($offset)
86
            ->take($userPerPage)
87
            ->get();
88
89
        // render output view
90
        return $this->view->render('index', [
91
            'records' => $profiles,
92
            'pagination' => $pagination,
93
            'id' => $name,
94
            'add' => $value,
95
            'ratingOn' => (int)$cfgs['rating']
96
        ]);
97
    }
98
}