LampagerBehavior::paginate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 16
c 1
b 0
f 0
nc 1
nop 8
dl 0
loc 30
rs 9.7333
ccs 13
cts 13
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)->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 array $conditions
59
     * @param array $fields
60
     * @param array $order
61
     * @param int   $limit
62
     * @param int   $page
63
     * @param int   $recursive
64
     */
65
    public function paginate(Model $model, $conditions, $fields, $order, $limit, $page = 1, $recursive = null, array $extra = [])
66
    {
67
        /**
68
         * Extract extra parameters which may include
69 16
         *
70
         * @var bool  $forward
71
         * @var bool  $backward
72
         * @var bool  $exclusive
73
         * @var bool  $inclusive
74
         * @var bool  $seekable
75
         * @var bool  $unseekable
76
         * @var array $cursor
77
         */
78
        extract($extra, EXTR_SKIP);
79
80
        return $model->find('lampager', compact(array_intersect([
81
            'conditions',
82 16
            'fields',
83
            'order',
84 16
            'limit',
85 16
            'page',
86 16
            'recursive',
87 16
            'forward',
88 16
            'backward',
89 16
            'exclusive',
90 16
            'inclusive',
91 16
            'seekable',
92 16
            'unseekable',
93 16
            'cursor',
94 16
        ], array_keys(get_defined_vars()))));
95 16
    }
96
}
97