Completed
Push — develop ( 7d6075...b57d28 )
by
unknown
23:57 queued 15:50
created

PaginationBuilder::getResult()   D

Complexity

Conditions 9
Paths 72

Size

Total Lines 32
Code Lines 17

Duplication

Lines 8
Ratio 25 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 8
loc 32
rs 4.909
cc 9
eloc 17
nc 72
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Controller\Plugin;
12
13
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
14
15
/**
16
 * Collects pagination related configuration and passes it to the appropriate
17
 * controller plugin.
18
 *
19
 * In the controller, you only have to call one plugin to do all the YAWIK pagination magic.
20
 *
21
 * @see \Core\Controller\Plugin\PaginationParams
22
 * @see \Core\Controller\Plugin\SearchForm
23
 * @see \Core\Controller\Plugin\CreatePaginator
24
 *
25
 * 
26
 * @author Mathias Gelhausen <[email protected]>
27
 * @since 0.25
28
 */
29
class PaginationBuilder extends AbstractPlugin
30
{
31
32
    /**
33
     * The internal configuration stack.
34
     *
35
     * @var array
36
     */
37
    protected $stack = [];
38
39
    /**
40
     * The generated result array.
41
     *
42
     * @var array
43
     */
44
    protected $result = [];
45
46
    /**
47
     * Entry point.
48
     *
49
     * if $stack is provided, the internal stack is set to exact that value, so
50
     * please be sure what you're doing.
51
     *
52
     * If you pass a boolean TRUE as $stack, the internal stack is reset.
53
     *
54
     * @param null|array|bool $stack
55
     * @param bool $returnResult Should the result be immediately be returned instead of
56
     *                           self. Only affective when $stack is an array.
57
     *
58
     * @return self|array
59
     * @throws \InvalidArgumentException
60
     */
61
    public function __invoke($stack = null, $returnResult = true)
62
    {
63
        if (true === $stack) {
64
            $this->stack = [];
65
            return $this;
66
        }
67
68
        if (null === $stack) {
69
            return $this;
70
        }
71
72
        if (!is_array($stack)) {
73
            throw new \InvalidArgumentException('Expected argument to be of type array, but received ' . gettype($stack));
74
        }
75
76
        $this->stack = $stack;
77
78
        return $returnResult ? $this->getResult() : $this;
79
    }
80
81
    /**
82
     * Add arguments for the call to the CreatePaginator plugin.
83
     *
84
     * @see \Core\Controller\Plugin\CreatePaginator::__invoke()
85
     *
86
     * @param string       $paginatorName
87
     * @param array  $defaultParams
88
     * @param bool   $usePostParams
89
     * @param string $as The name of the key in the result array.
90
     *
91
     * @return self
92
     */
93
    public function paginator($paginatorName, $defaultParams = [], $usePostParams = false, $as = 'paginator')
94
    {
95
        if (is_string($defaultParams)) {
96
            $as = $defaultParams;
97
            $defaultParams = [];
98
        } else if (is_string($usePostParams)) {
99
            $as = $usePostParams;
100
            $usePostParams = false;
101
        }
102
103
        $this->stack['paginator'] = ['as' => $as, $paginatorName, $defaultParams, $usePostParams];
104
        return $this;
105
    }
106
107
    /**
108
     * Add arguments for the call to the SearchForm plugin.
109
     *
110
     * @see \Core\Controller\Plugin\SearchForm::get()
111
     *
112
     * @param        $elementsFieldset
113
     * @param null   $buttonsFieldset
114
     * @param string $as The name of the key in the result array.
115
     *
116
     * @return self
117
     */
118
    public function form($elementsFieldset, $buttonsFieldset = null, $as = 'searchform')
119
    {
120
        if (null !== $buttonsFieldset && 0 === strpos($buttonsFieldset, '@')) {
121
            $as = substr($buttonsFieldset, 1);
122
            $buttonsFieldset = null;
123
        }
124
125
        $this->stack['form'] = ['as' => $as, $elementsFieldset, $buttonsFieldset];
126
        return $this;
127
    }
128
129
    /**
130
     * Add arguments for the call to the PaginatorParams plugin.
131
     *
132
     * @see \Core\Controller\Plugin\PaginationParams::getParams()
133
     *
134
     * @param       $namespace
135
     * @param array $defaults
136
     *
137
     * @return self
138
     */
139
    public function params($namespace, $defaults = [ 'page' => 1 ])
140
    {
141
        $this->stack['params'] = [$namespace, $defaults];
142
        return $this;
143
    }
144
145
    /**
146
     * Calls the stacked plugins in the right order and returns the result array.
147
     *
148
     * The returned array can directly be returned from the controller or be used to populate a
149
     * view model.
150
     *
151
     * The search form plugin is only called (and thus the form only present in the result array)
152
     * if the request is NOT an ajax request. (as the  form is never rerendered on ajax requests)
153
     *
154
     * @param null|string $paginatorAlias Name of the paginator in the result array
155
     * @param null|string $formAlias Name of the search form in the result array
156
     *
157
     * @return array
158
     */
159
    public function getResult($paginatorAlias = null, $formAlias = null)
160
    {
161 View Code Duplication
        if (null === $paginatorAlias) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
            $paginatorAlias = isset($this->stack['paginator']['as'])
163
                            ? $this->stack['paginator']['as']
164
                            : 'paginator';
165
        }
166
167 View Code Duplication
        if (null === $formAlias) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
168
            $formAlias = isset($this->stack['form']['as']) ? $this->stack['form']['as'] : 'searchform';
169
        }
170
171
        /* @var \Zend\Mvc\Controller\AbstractController $controller
172
         * @var \Zend\Http\Request $request */
173
        $result = [];
174
        $controller = $this->getController();
175
        $request = $controller->getRequest();
176
177
        if (isset($this->stack['params'])) {
178
            $this->callPlugin('paginationParams', $this->stack['params']);
179
        }
180
181
        if (isset($this->stack['form']) && !$request->isXmlHttpRequest()) {
182
            $result[$formAlias] = $this->callPlugin('searchform', $this->stack['form']);
183
        }
184
185
        if (isset($this->stack['paginator'])) {
186
            $result[$paginatorAlias] = $this->callPlugin('paginator', $this->stack['paginator']);
187
        }
188
189
        return $result;
190
    }
191
192
    /**
193
     * Calls an invokable controller plugin.
194
     *
195
     * @param string $name
196
     * @param array $args
197
     *
198
     * @return mixed The return value of the called plugin.
199
     */
200
    protected function callPlugin($name, $args)
201
    {
202
        /* @var \Zend\Mvc\Controller\AbstractController $controller */
203
        $controller = $this->getController();
204
        $plugin = $controller->plugin($name);
205
206
        /* We remove the array entry with the key "as" here.
207
         * because we want to keep it in the stack array */
208
        unset($args['as']);
209
210
        return call_user_func_array($plugin, $args);
211
    }
212
}