Completed
Push — master ( 198a3e...84e362 )
by Rougin
10:23
created

PaginateTrait::prepareConfiguration()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 16
cts 16
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
crap 4
1
<?php
2
3
namespace Rougin\Credo\Traits;
4
5
/**
6
 * Paginate Trait
7
 *
8
 * @package Credo
9
 * @author  Rougin Royce Gutib <[email protected]>
10
 *
11
 * @property \CI_Config     $config
12
 * @property \CI_Input      $input
13
 * @property \CI_Loader     $load
14
 * @property \CI_Pagination $pagination
15
 * @property \CI_URI        $uri
16
 *
17
 * @method array   all()
18
 * @method integer countAll()
19
 * @method array   findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
20
 */
21
trait PaginateTrait
22
{
23
    /**
24
     * Limits the data based on given configuration and generates pagination links.
25
     *
26
     * @param  integer $page
27
     * @param  array   $config
28
     * @return array
29
     */
30 3
    public function paginate($page, $config = array())
31
    {
32 3
        $this->load->library('pagination');
33
34 3
        $config = $this->configuration($config);
35
36 3
        $config['per_page'] = (integer) $page;
37 3
38
        $config['total_rows'] = $this->countAll();
39 3
40
        $offset = $this->offset($page, $config);
41 3
42
        $this->pagination->initialize($config);
43 3
44
        $result = array($this->get($page, $offset));
0 ignored issues
show
Bug introduced by
It seems like get() 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...
45 3
46
        $result[] = $this->pagination->create_links();
47
48
        return (array) $result;
49
    }
50
51
    /**
52
     * Returns the offset from the defined configuration.
53
     *
54 3
     * @param  array $config
55
     * @return integer
56 3
     */
57
    protected function offset($page, array $config)
58 3
    {
59 3
        $offset = $this->uri->segment($config['uri_segment']);
60 3
61
        if ($config['page_query_string'] === true) {
62 3
            $segment = $config['query_string_segment'];
63 3
64 3
            $offset = $this->input->get($segment);
65
        }
66 3
67
        $numbers = $config['use_page_numbers'] && $offset !== 0;
68
69
        return $numbers ? ($page * $offset) - $page : $offset;
70
    }
71
72
    /**
73
     * Retrieves configuration from pagination.php.
74
     * If not available, will based on given and default data.
75
     *
76 3
     * @param  array $config
77
     * @return array
78 3
     */
79
    protected function configuration(array $config)
80
    {
81 3
        $this->load->helper('url');
82 3
83 3
        $items = array('base_url' => current_url());
84 3
85 3
        $items['page_query_string'] = false;
86 3
        $items['query_string_segment'] = 'per_page';
87
        $items['uri_segment'] = 3;
88 3
        $items['use_page_numbers'] = false;
89 3
90 3
        foreach ((array) $items as $key => $value) {
91 3
            if ($this->config->item($key) !== null) {
92 3
                $config[$key] = $this->config->item($key);
93 3
            } elseif (! isset($config[$key])) {
94 3
                $config[$key] = $value;
95 3
            }
96
        }
97
98
        return $config;
99
    }
100
}
101