ListControls::getSortColumns()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 13
cts 13
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
10
namespace AnimeDb\Bundle\CatalogBundle\Service\Item;
11
12
use AnimeDb\Bundle\CatalogBundle\Service\Item\Search\Manager;
13
14
/**
15
 * Item list controls service.
16
 *
17
 * @author  Peter Gribanov <[email protected]>
18
 */
19
class ListControls
20
{
21
    /**
22
     * @var int
23
     */
24
    const DEFAULT_LIMIT = 8;
25
26
    /**
27
     * Limit for show all items.
28
     *
29
     * @var int
30
     */
31
    const LIMIT_ALL = 0;
32
33
    /**
34
     * Limit name for show all items.
35
     *
36
     * @var int
37
     */
38
    const LIMIT_ALL_NAME = 'All (%total%)';
39
40
    /**
41
     * Limits on the number of items per page.
42
     *
43
     * @var array
44
     */
45
    public static $limits = [8, 16, 32, self::LIMIT_ALL];
46
47
    /**
48
     * Sort items by column.
49
     *
50
     * @var array
51
     */
52
    public static $sort_by_column = [
53
        'name' => [
54
            'title' => 'Item name',
55
            'name' => 'Name',
56
        ],
57
        'date_update' => [
58
            'title' => 'Last updated item',
59
            'name' => 'Update',
60
        ],
61
        'rating' => [
62
            'title' => 'Item rating',
63
            'name' => 'Rating',
64
        ],
65
        'date_premiere' => [
66
            'title' => 'Date premiere',
67
            'name' => 'Date premiere',
68
        ],
69
        'date_end' => [
70
            'title' => 'End date of issue',
71
            'name' => 'Date end',
72
        ],
73
    ];
74
75
    /**
76
     * @var Manager
77
     */
78
    protected $searcher;
79
80
    /**
81
     * @param Manager $searcher
82
     */
83 24
    public function __construct(Manager $searcher)
84
    {
85 24
        $this->searcher = $searcher;
86 24
    }
87
88
    /**
89
     * Get limit list items.
90
     *
91
     * @param array $query
92
     *
93
     * @return int
94
     */
95 14
    public function getLimit(array $query = [])
96
    {
97 14
        if (isset($query['limit']) && is_numeric($query['limit']) && in_array($query['limit'], self::$limits)) {
98 8
            return (int) $query['limit'];
99
        }
100
101 6
        return self::DEFAULT_LIMIT;
102
    }
103
104
    /**
105
     * Get list limits.
106
     *
107
     * @param array $query
108
     *
109
     * @return array
110
     */
111 7
    public function getLimits(array $query = [])
112
    {
113 7
        $limits = [];
114 7
        $current_limit = $this->getLimit($query);
115
116 7
        foreach (self::$limits as $limit) {
117 7
            $limits[] = [
118 7
                'link' => '?'.http_build_query(array_merge($query, ['limit' => $limit])),
119 7
                'name' => $limit ? $limit : self::LIMIT_ALL_NAME,
120 7
                'count' => $limit,
121 7
                'current' => $current_limit == $limit,
122
            ];
123 7
        }
124
125 7
        return $limits;
126
    }
127
128
    /**
129
     * @param array $query
130
     *
131
     * @return string
132
     */
133 4
    public function getSortColumn(array $query = [])
134
    {
135 4
        return $this->searcher->getValidSortColumn(isset($query['sort_by']) ? $query['sort_by'] : null);
136
    }
137
138
    /**
139
     * @param array $query
140
     *
141
     * @return array
142
     */
143 2
    public function getSortColumns(array $query = [])
144
    {
145 2
        $current_sort_by = $this->getSortColumn($query);
146
147
        // sort by
148 2
        $sort_by = [];
149 2
        foreach (self::$sort_by_column as $column => $info) {
150 2
            $sort_by[] = [
151 2
                'name' => $info['name'],
152 2
                'title' => $info['title'],
153 2
                'current' => $current_sort_by == $column,
154 2
                'link' => '?'.http_build_query(
155 2
                    array_merge($query, ['sort_by' => $column])
156 2
                ),
157
            ];
158 2
        }
159
160 2
        return $sort_by;
161
    }
162
163
    /**
164
     * @param array $query
165
     *
166
     * @return string
167
     */
168 6
    public function getSortDirection(array $query = [])
169
    {
170 6
        $sort_direction = isset($query['sort_direction']) ? $query['sort_direction'] : null;
171
172 6
        return $this->searcher->getValidSortDirection($sort_direction);
173
    }
174
175
    /**
176
     * @param array $query
177
     *
178
     * @return string
179
     */
180 3
    public function getSortDirectionLink(array $query = [])
181
    {
182 3
        $direction = $this->getSortDirection($query) == 'ASC' ? 'DESC' : 'ASC';
183
184 3
        return '?'.http_build_query(
185 3
            array_merge($query, ['sort_direction' => $direction])
186 3
        );
187
    }
188
}
189