Passed
Pull Request — master (#7)
by Chito
02:51
created

LampagerBehavior::paginate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 8
ccs 16
cts 16
cp 1
crap 1

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
App::uses('ModelBehavior', 'Model');
4
App::uses('LampagerPaginator', 'Lampager.Model');
5
App::uses('LampagerArrayProcessor', 'Lampager.Model');
6
7
class LampagerBehavior extends ModelBehavior
8
{
9
    /** @var string[] */
10
    public $mapMethods = [
11
        '/\b_findLampager\b/' => 'findLampager',
12
    ];
13
14
    /**
15
     * {@inheritdoc}
16
     */
17
    public function setup(Model $model, $config = [])
18
    {
19 32
        $model->findMethods['lampager'] = true;
20
    }
21 32
22 32
    /**
23
     * {@inheritdoc}
24
     */
25
    public function cleanup(Model $model)
26
    {
27 32
        unset($model->findMethods['lampager']);
28
    }
29 32
30 32
    /**
31
     * Hanldle the custom finder. Only called by Model::find().
32
     *
33
     * @param  Model  $model   Model.
34
     * @param  string $method  Method.
35
     * @param  string $state   Either "before" or "after"
36
     * @param  array  $query   Query.
37
     * @param  array  $results Results.
38
     * @return array
39
     */
40
    public function findLampager(Model $model, $method, $state, array $query = [], array $results = [])
41
    {
42 32
        return $this->{__FUNCTION__ . ucfirst($state)}($model, $query, $results);
43
    }
44 32
45
    protected function findLampagerBefore(Model $model, array $query = [], array $results = [])
46
    {
47 32
        return LampagerPaginator::create($model, $query)->transformer->build(isset($query['cursor']) ? $query['cursor'] : []);
48
    }
49 32
50
    protected function findLampagerAfter(Model $model, array $query = [], array $results = [])
51
    {
52 32
        return LampagerArrayProcessor::create($model)->process($query['config'], $results);
53
    }
54 32
55
    /**
56
     * Paginate the Model. Only called by PaginatorComponent::paginate().
57
     *
58
     * @param Model $model
59
     * @param array $conditions
60
     * @param array $fields
61
     * @param array $order
62
     * @param int   $limit
63
     * @param int   $page
64
     * @param int   $recursive
65
     * @param array $extra
66
     */
67
    public function paginate(Model $model, $conditions, $fields, $order, $limit, $page = 1, $recursive = null, array $extra = [])
68
    {
69 16
        /**
70
         * Extract extra parameters which may include
71
         *
72
         * @var bool  $forward
73
         * @var bool  $backward
74
         * @var bool  $exclusive
75
         * @var bool  $inclusive
76
         * @var bool  $seekable
77
         * @var bool  $unseekable
78
         * @var array $cursor
79
         */
80
        extract($extra, EXTR_SKIP);
81
82 16
        return $model->find('lampager', compact(array_intersect([
83
            'conditions',
84 16
            'fields',
85 16
            'order',
86 16
            'limit',
87 16
            'page',
88 16
            'recursive',
89 16
            'forward',
90 16
            'backward',
91 16
            'exclusive',
92 16
            'inclusive',
93 16
            'seekable',
94 16
            'unseekable',
95 16
            'cursor',
96 16
        ], array_keys(get_defined_vars()))));
97 16
    }
98
}
99