Test Failed
Push — master ( e3c39f...fe570d )
by Mihail
07:20
created

Apps/Controller/Front/Profile/ActionIndex.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace Apps\Controller\Front\Profile;
4
5
use Apps\ActiveRecord\Profile as ProfileRecords;
6
use Ffcms\Core\Arch\View;
7
use Ffcms\Core\Exception\NotFoundException;
8
use Ffcms\Core\Helper\HTML\SimplePagination;
0 ignored issues
show
The type Ffcms\Core\Helper\HTML\SimplePagination was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
     */
28
    public function index(string $name, ?string $value = null): ?string
29
    {
30
        $records = null;
31
        // set current page and offset
32
        $page = (int)$this->request->query->get('page', 0);
33
        $cfgs = $this->application->configs;
34
        $userPerPage = (int)$cfgs['usersOnPage'];
35
        if ($userPerPage < 1) {
36
            $userPerPage = 1;
37
        }
38
        $offset = $page * $userPerPage;
39
40
        switch ($name) {
41
            case 'rating': // rating list, order by rating DESC
42
                // check if rating is enabled
43
                if ((int)$cfgs['rating'] !== 1) {
44
                    throw new NotFoundException();
45
                }
46
                $records = (new ProfileRecords())->orderBy('rating', 'DESC');
47
                break;
48
            case 'hobby': // search by hobby
49
                if (Str::likeEmpty($value)) {
50
                    throw new NotFoundException();
51
                }
52
                $records = (new ProfileRecords())->where('hobby', 'like', '%' . $value . '%');
53
                break;
54
            case 'city':
55
                if (Str::likeEmpty($value)) {
56
                    throw new NotFoundException();
57
                }
58
                $records = (new ProfileRecords())->where('city', $value);
59
                break;
60
            case 'born':
61
                if ($value === null || !Any::isInt($value)) {
62
                    throw new NotFoundException();
63
                }
64
                $records = (new ProfileRecords())->where('birthday', 'like', $value . '-%');
65
                break;
66
            case 'all':
67
                $records = (new ProfileRecords())->orderBy('id', 'DESC');
68
                break;
69
            default:
70
                $this->response->redirect('profile/index/all');
71
                break;
72
        }
73
74
        // get total records count
75
        $totalCount = $records->count();
76
77
        // get profile list with relation for user and role tables in 1 query
78
        $profiles = $records->with(['user', 'user.role'])
79
            ->skip($offset)
80
            ->take($userPerPage)
81
            ->get();
82
83
        // render output view
84
        return $this->view->render('profile/index', [
85
            'records' => $profiles,
86
            'pagination' => [
87
                'page' => $page,
88
                'total' => $totalCount,
89
                'step' => $userPerPage
90
            ],
91
            'id' => $name,
92
            'add' => $value,
93
            'ratingOn' => (int)$cfgs['rating']
94
        ]);
95
    }
96
}
97