Completed
Push — master ( 28c620...75a8a0 )
by Peter
02:54
created

Manager   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 93.75%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 100
ccs 15
cts 16
cp 0.9375
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A searchByName() 0 4 1
A getValidSortColumn() 0 4 2
A getValidSortDirection() 0 4 2
A search() 0 15 3
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\Search;
11
12
use AnimeDb\Bundle\CatalogBundle\Entity\Search;
13
14
/**
15
 * Item search.
16
 *
17
 * @author  Peter Gribanov <[email protected]>
18
 */
19
class Manager
20
{
21
    /**
22
     * @var string
23
     */
24
    const DEFAULT_SORT_COLUMN = 'date_update';
25
26
    /**
27
     * @var string
28
     */
29
    const DEFAULT_SORT_DIRECTION = 'DESC';
30
31
    /**
32
     * @var DriverInterface
33
     */
34
    protected $driver;
35
36
    /**
37
     * @var array
38
     */
39
    public static $sort_columns = [
40
        'name',
41
        'date_update',
42
        'rating',
43
        'date_premiere',
44
        'date_end',
45
    ];
46
47
    /**
48
     * @var array
49
     */
50
    public static $sort_direction = [
51
        'DESC',
52
        'ASC',
53
    ];
54
55
    /**
56
     * @param DriverInterface $driver
57
     */
58 13
    public function __construct(DriverInterface $driver)
59
    {
60 13
        $this->driver = $driver;
61 13
    }
62
63
    /**
64
     * @param Search $data
65
     * @param int|null $limit
66
     * @param int|null $offset
67
     * @param string|null $sort_column
68
     * @param string|null $sort_direction
69
     *
70
     * @return array {list:[],total:0}
71
     */
72 3
    public function search(
73
        Search $data,
74
        $limit = 0,
75
        $offset = 0,
76
        $sort_column = self::DEFAULT_SORT_COLUMN,
77
        $sort_direction = self::DEFAULT_SORT_DIRECTION
78
    ) {
79 3
        return $this->driver->search(
80
            $data,
81 3
            ($limit > 0 ? (int) $limit : 0),
82 3
            ($offset > 0 ? (int) $offset : 0),
83 3
            $this->getValidSortColumn($sort_column),
84 3
            $this->getValidSortDirection($sort_direction)
85
        );
86
    }
87
88
    /**
89
     * @param string $name
90
     * @param int $limit
91
     *
92
     * @return array
93
     */
94 1
    public function searchByName($name, $limit = 0)
95
    {
96 1
        return $this->driver->searchByName($name, $limit);
97
    }
98
99
    /**
100
     * @param string|null $column
101
     *
102
     * @return string
103
     */
104 9
    public function getValidSortColumn($column = self::DEFAULT_SORT_COLUMN)
105
    {
106 9
        return in_array($column, self::$sort_columns) ? $column : self::DEFAULT_SORT_COLUMN;
107
    }
108
109
    /**
110
     * @param string|null $direction
111
     *
112
     * @return string
113
     */
114 6
    public function getValidSortDirection($direction = self::DEFAULT_SORT_DIRECTION)
115
    {
116 6
        return in_array($direction, self::$sort_direction) ? $direction : self::DEFAULT_SORT_DIRECTION;
117
    }
118
}
119