IndexTrait   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 35
c 3
b 1
f 0
dl 0
loc 60
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A index() 0 58 2
1
<?php
2
3
namespace Jidaikobo\Kontiki\Controllers\Traits;
4
5
use Jidaikobo\Kontiki\Handlers\TableHandler;
6
use Jidaikobo\Kontiki\Renderers\TableRenderer;
7
use Psr\Http\Message\ResponseInterface as Response;
8
use Psr\Http\Message\ServerRequestInterface as Request;
9
10
trait IndexTrait
11
{
12
    private function index(
13
        Request $request,
14
        Response $response,
15
        string $context
16
    ): Response {
17
        // Get data using the new getData method
18
        $data = $this->model->getIndexData($context, $request->getQueryParams());
19
20
        // render table
21
        $content = $this->tableService->tableHtml(
22
            $data,
23
            $this->adminDirName,
24
            $this->getRoutes(),
0 ignored issues
show
Bug introduced by
It seems like getRoutes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
            $this->/** @scrutinizer ignore-call */ 
25
                   getRoutes(),
Loading history...
25
            $context
26
        );
27
28
        // set messages
29
        $error = $this->flashManager->getData('errors', []);
30
        $success = $this->flashManager->getData('success', []);
31
32
        // render messages
33
        $content = $this->tableService->addMessages(
34
            $content,
35
            $error,
36
            $success
37
        );
38
39
        // pagination
40
        $paginationSuffix = $context == 'all' ? '' : '/' . $context;
41
        $content .= $this->model->getPagination()->render(
42
            env('BASEPATH', '') . "/{$this->adminDirName}/index" . $paginationSuffix
43
        );
44
        $totalItems = $this->model->getPagination()->getTotalItems();
45
        $currentPage = $this->model->getPagination()->getCurrentPage();
46
47
        $title = 'x_index_' . $context;
48
        $title_placeholder = $context . ' index of :name';
49
50
        $pageTitle = __(
51
            $title,
52
            $title_placeholder,
53
            ['name' => __($this->label)]
54
        );
55
56
        $pageNum = __(
57
            'x_page',
58
            ' index of :name',
59
            ['name' => $currentPage]
60
        );
61
62
        return $this->renderResponse(
0 ignored issues
show
Bug introduced by
It seems like renderResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        return $this->/** @scrutinizer ignore-call */ renderResponse(
Loading history...
63
            $response,
64
            $pageTitle,
65
            $content,
66
            'layout.php',
67
            [
68
                'title' => $pageTitle . ' (' . $pageNum . ')',
69
                'h1' => $pageTitle . ' (' . count($data) . '/'  . $totalItems . ')',
70
            ]
71
        );
72
    }
73
}
74