Completed
Push — master ( 37b943...607dad )
by Filipe
06:01
created

EntityListingMethods   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 2
Metric Value
wmc 8
c 4
b 2
f 2
lcom 2
cbo 6
dl 0
loc 130
ccs 41
cts 41
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 13 1
A getPagination() 0 12 2
A getSearchFilter() 0 8 1
A getSearchFields() 0 11 2
getRequest() 0 1 ?
getEntityDescriptor() 0 1 ?
getEntityNamePlural() 0 1 ?
A getListingService() 0 9 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 Psr\Http\Message\ServerRequestInterface;
13
use Slick\Filter\StaticFilter;
14
use Slick\Http\PhpEnvironment\Request;
15
use Slick\Mvc\Service\Entity\EntityListingService;
16
use Slick\Mvc\Service\Entity\QueryFilter\SearchFilter;
17
use Slick\Mvc\Utils\Pagination;
18
19
/**
20
 * Entity Listing Methods
21
 *
22
 * @package Slick\Mvc\Controller
23
 * @author  Filipe Silva <[email protected]>
24
 */
25
trait EntityListingMethods
26
{
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(
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
62
            [
63 2
                $this->getEntityNamePlural() => $this->getListingService()
64 2
                    ->getList(),
65 2
                'pagination' => $this->getListingService()->getPagination()
66 1
            ]
67 1
        );
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 2
                ]
83 2
            );
84 2
        }
85 4
        return $this->pagination;
86
    }
87
88
    /**
89
     * Get the entity listing service
90
     *
91
     * @return EntityListingService
92
     */
93 4
    protected function getListingService()
94
    {
95 4
        if (null == $this->listingService) {
96 2
            $this->listingService = new EntityListingService(
97 2
                $this->getEntityClassName()
0 ignored issues
show
Bug introduced by
It seems like getEntityClassName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
98 1
            );
99 1
        }
100 4
        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);
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
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 1
            ];
130 1
        }
131 2
        return $this->searchFields;
132
    }
133
134
    /**
135
     * Gets updated HTTP request
136
     *
137
     * @return ServerRequestInterface|Request
138
     */
139
    abstract public function getRequest();
140
141
    /**
142
     * Get the current entity descriptor
143
     *
144
     * @return \Slick\Orm\Descriptor\EntityDescriptorInterface
145
     */
146
    abstract protected function getEntityDescriptor();
147
148
    /**
149
     * Get the plural name of the entity
150
     *
151
     * @return string
152
     */
153
    abstract protected function getEntityNamePlural();
154
}