Completed
Push — master ( 2f5afb...f40faa )
by Filipe
02:44
created

EntityListingMethods   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 7

Test Coverage

Coverage 90.24%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 8
c 3
b 1
f 2
lcom 2
cbo 7
dl 0
loc 111
ccs 37
cts 41
cp 0.9024
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 13 1
A getPagination() 0 12 2
A getListingService() 0 9 2
A getSearchFilter() 0 8 1
A getSearchFields() 0 11 2
1
<?php
2
3
/**
4
 * This file is part of slick/mvc package
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace Slick\Mvc\Controller;
11
12
use Slick\Filter\StaticFilter;
13
use Slick\Mvc\Service\Entity\EntityListingService;
14
use Slick\Mvc\Service\Entity\QueryFilter\SearchFilter;
15
use Slick\Mvc\Utils\Pagination;
16
17
/**
18
 * Entity Listing Methods
19
 *
20
 * @package Slick\Mvc\Controller
21
 * @author  Filipe Silva <[email protected]>
22
 */
23
trait EntityListingMethods
24
{
25
26
    use EntityBasedMethods;
27
    
28
    /**
29
     * @var int
30
     */
31
    protected $rowsPerPage = 12;
32
33
    /**
34
     * @var Pagination
35
     */
36
    protected $pagination;
37
38
    /**
39
     * @var EntityListingService
40
     */
41
    protected $listingService;
42
43
    /**
44
     * @var string[]
45
     */
46
    protected $searchFields;
47
48
    /**
49
     * @var string
50
     */
51
    protected $order;
52
53
    /**
54
     * Handle the request to display a list of entities
55
     */
56 2
    public function index()
57
    {
58 2
        $this->getListingService()
59 2
            ->setPagination($this->getPagination())
60 2
            ->getFilters()->add($this->getSearchFilter());
61 2
        $this->set(
62
            [
63 2
                $this->getEntityNamePlural() => $this->getListingService()
64 2
                    ->getList(),
65 2
                'pagination' => $this->getListingService()->getPagination()
66 2
            ]
67 2
        );
68 2
    }
69
70
    /**
71
     * Get pagination for roes per page property
72
     *
73
     * @return Pagination
74
     */
75 4
    protected function getPagination()
76
    {
77 4
        if (null == $this->pagination) {
78 4
            $this->pagination = new Pagination(
79
                [
80 4
                    'rowsPerPage' => $this->rowsPerPage,
81 4
                    'request' => $this->getRequest()
82 4
                ]
83 4
            );
84 4
        }
85 4
        return $this->pagination;
86
    }
87
88
    /**
89
     * Get the entity listing service
90
     *
91
     * @return EntityListingService
92
     */
93 2
    protected function getListingService()
94
    {
95 2
        if (null == $this->listingService) {
96
            $this->listingService = new EntityListingService(
97
                $this->getEntityClassName()
98
            );
99
        }
100 2
        return $this->listingService;
101
    }
102
103
    /**
104
     * Get search filter 
105
     * 
106
     * @return SearchFilter
107
     */
108 4
    protected function getSearchFilter()
109
    {
110 4
        $pattern = $this->getRequest()->getQuery('pattern', null);
0 ignored issues
show
Bug introduced by
The method getQuery() does not exist on Psr\Http\Message\ServerRequestInterface. Did you maybe mean getQueryParams()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
111 4
        $pattern = StaticFilter::filter('text', $pattern);
112 4
        $this->set('pattern', $pattern);
113
        
114 4
        return new SearchFilter(['pattern' => $pattern]);
115
    }
116
117
    /**
118
     * Get the fields list to use on search filter
119
     * 
120
     * @return array|\string[]
121
     */
122 2
    protected function getSearchFields()
123
    {
124 2
        if (null == $this->searchFields) {
125 2
            $field = $this->getEntityDescriptor()->getDisplayFiled();
126 2
            $this->searchFields = [
127 2
                $this->getEntityDescriptor()
128 2
                    ->getTableName().'.'.$field->getField()
129 2
            ];
130 2
        }
131 2
        return $this->searchFields;
132
    }
133
}