EntityListingMethods::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 12
cts 12
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
crap 1
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
use Slick\Orm\Repository\EntityRepository;
19
20
/**
21
 * Entity Listing Methods
22
 *
23
 * @package Slick\Mvc\Controller
24
 * @author  Filipe Silva <[email protected]>
25
 */
26
trait EntityListingMethods
27
{
28
    
29
    /**
30
     * @var int
31
     */
32
    protected $rowsPerPage = 12;
33
34
    /**
35
     * @var Pagination
36
     */
37
    protected $pagination;
38
39
    /**
40
     * @var EntityListingService
41
     */
42
    protected $listingService;
43
44
    /**
45
     * @var string[]
46
     */
47
    protected $searchFields;
48
49
    /**
50
     * Handle the request to display a list of entities
51
     */
52 2
    public function index()
53
    {
54 2
        $this->getListingService()
55 2
            ->setOrder($this->getOrder())
56 2
            ->setPagination($this->getPagination())
57 2
            ->getFilters()->add($this->getSearchFilter());
58 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...
59
            [
60 2
                $this->getEntityNamePlural() => $this->getListingService()
61 2
                    ->getList(),
62 2
                'pagination' => $this->getListingService()->getPagination()
63 1
            ]
64 1
        );
65 2
    }
66
67
    /**
68
     * Get pagination for roes per page property
69
     *
70
     * @return Pagination
71
     */
72 4
    protected function getPagination()
73
    {
74 4
        if (null == $this->pagination) {
75 4
            $this->pagination = new Pagination(
76
                [
77 4
                    'rowsPerPage' => $this->rowsPerPage,
78 4
                    'request' => $this->getRequest()
79 2
                ]
80 2
            );
81 2
        }
82 4
        return $this->pagination;
83
    }
84
85
    /**
86
     * Get the entity listing service
87
     *
88
     * @return EntityListingService
89
     */
90 4
    protected function getListingService()
91
    {
92 4
        if (null == $this->listingService) {
93 2
            $this->listingService = new EntityListingService(
94 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...
95 1
            );
96 1
        }
97 4
        return $this->listingService;
98
    }
99
100
    /**
101
     * Get search filter 
102
     * 
103
     * @return SearchFilter
104
     */
105 4
    protected function getSearchFilter()
106
    {
107 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...
108 4
        $pattern = StaticFilter::filter('text', $pattern);
109 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...
110
        
111 4
        return new SearchFilter(['pattern' => $pattern]);
112
    }
113
114
    /**
115
     * Get the fields list to use on search filter
116
     * 
117
     * @return array|\string[]
118
     */
119 2
    protected function getSearchFields()
120
    {
121 2
        if (null == $this->searchFields) {
122 2
            $field = $this->getEntityDescriptor()->getDisplayFiled();
123 2
            $this->searchFields = [
124 2
                $this->getEntityDescriptor()
125 2
                    ->getTableName().'.'.$field->getField()
126 1
            ];
127 1
        }
128 2
        return $this->searchFields;
129
    }
130
131
    /**
132
     * Returns the query order by clause
133
     * 
134
     * @return string
135
     */
136 2
    protected function getOrder()
137
    {
138
        /** @var EntityRepository $repo */
139 2
        $repo = $this->getRepository();
0 ignored issues
show
Bug introduced by
It seems like getRepository() 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...
140 2
        $table = $repo->getEntityDescriptor()->getTableName();
141 2
        $pmk = $repo->getEntityDescriptor()->getPrimaryKey()->getField();
142 2
        return "{$table}.{$pmk} DESC";
143
    }
144
145
    /**
146
     * Gets updated HTTP request
147
     *
148
     * @return ServerRequestInterface|Request
149
     */
150
    abstract public function getRequest();
151
152
    /**
153
     * Get the current entity descriptor
154
     *
155
     * @return \Slick\Orm\Descriptor\EntityDescriptorInterface
156
     */
157
    abstract protected function getEntityDescriptor();
158
159
    /**
160
     * Get the plural name of the entity
161
     *
162
     * @return string
163
     */
164
    abstract protected function getEntityNamePlural();
165
}