Search::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php namespace Nord\Lumen\Search;
2
3
use Nord\Lumen\Search\Exceptions\InvalidArgument;
4
use Nord\Lumen\Search\Contracts\SearchAdapter;
5
use ReflectionClass;
6
7
class Search
8
{
9
10
    /**
11
     * @var Filter[]
12
     */
13
    private $filters = [];
14
15
    /**
16
     * @var Sort[]
17
     */
18
    private $sorts = [];
19
20
    /**
21
     * @var SearchAdapter
22
     */
23
    private $adapter;
24
25
    /**
26
     * @var StringParser
27
     */
28
    private $parser;
29
30
31
    /**
32
     * Search constructor.
33
     *
34
     * @param mixed         $filters
35
     * @param mixed         $sorts
36
     * @param SearchAdapter $adapter
37
     * @param StringParser  $parser
38
     */
39
    public function __construct($filters, $sorts, SearchAdapter $adapter, StringParser $parser = null)
40
    {
41
        $this->setParser($parser !== null ? $parser : new StringParser());
42
        $this->setFilters($filters);
43
        $this->setSorts($sorts);
44
        $this->setAdapter($adapter);
45
    }
46
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public function run()
52
    {
53
        $this->applyFilters();
54
        $this->applySorts();
55
56
        return $this->adapter->getResult();
57
    }
58
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function runWithPagination($pageNumber, $pageSize)
64
    {
65
        $this->applyFilters();
66
        $this->applySorts();
67
68
        return $this->adapter->getPartialResult(new Pagination($pageNumber, $pageSize));
69
    }
70
71
72
    /**
73
     *
74
     */
75
    protected function applyFilters()
76
    {
77
        foreach ($this->filters as $filter) {
78
            $property = $filter->getProperty();
79
            $format   = $filter->getFormat();
80
            $value    = $filter->getValue();
81
82
            if ($format !== null) {
83
                $value = $this->adapter->formatValue($format, $value);
84
            }
85
86
            switch ($filter->getType()) {
87
                case Filter::TYPE_BETWEEN:
88
                    list($from, $to) = explode(',', $value);
89
                    $this->adapter->applyBetweenFilter($property, $from, $to);
90
                    break;
91
                case Filter::TYPE_NOT_EQUALS:
92
                    $this->adapter->applyNotEqualsFilter($property, $value);
93
                    break;
94
                case Filter::TYPE_GREATER_THAN:
95
                    $this->adapter->applyGreaterThanFilter($property, $value);
96
                    break;
97
                case Filter::TYPE_LESS_THAN:
98
                    $this->adapter->applyLessThanFilter($property, $value);
99
                    break;
100
                case Filter::TYPE_GREATER_THAN_OR_EQUALS:
101
                    $this->adapter->applyGreaterThanOrEqualsFilter($property, $value);
102
                    break;
103
                case Filter::TYPE_LESS_THAN_OR_EQUALS:
104
                    $this->adapter->applyLessThanOrEqualsFilter($property, $value);
105
                    break;
106
                case Filter::TYPE_BEGINS_WITH:
107
                    $this->adapter->applyBeginsWithFilter($property, $value);
108
                    break;
109
                case Filter::TYPE_ENDS_WITH:
110
                    $this->adapter->applyEndsWithFilter($property, $value);
111
                    break;
112
                case Filter::TYPE_FREE_TEXT:
113
                    $this->adapter->applyFreeTextFilter($property, $value);
114
                    break;
115
                case Filter::TYPE_EQUALS:
116
                default:
117
                    $this->adapter->applyEqualsFilter($property, $value);
118
                    break;
119
            }
120
        }
121
    }
122
123
124
    /**
125
     *
126
     */
127
    protected function applySorts()
128
    {
129
        foreach ($this->sorts as $sort) {
130
            $this->adapter->applySort($sort->getProperty(), $sort->getDirection());
131
        }
132
    }
133
134
135
    /**
136
     * @param mixed $filters
137
     *
138
     * @throws InvalidArgument
139
     */
140 View Code Duplication
    protected function setFilters($filters)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
    {
142
        if (empty($filters)) {
143
            return;
144
        }
145
146
        if (is_string($filters)) {
147
            $filters = $this->parser->parse($filters);
148
        }
149
150
        if (!is_array($filters)) {
151
            throw new InvalidArgument('Search filter is malformed.');
152
        }
153
154
        foreach ($filters as $filter) {
155
            $this->filters[] = $filter instanceof Filter ? $filter : $this->createFilterFromConfig($filter);
156
        }
157
    }
158
159
160
    /**
161
     * @param array $config
162
     *
163
     * @return Filter
164
     */
165
    protected function createFilterFromConfig(array $config)
166
    {
167
        return (new ReflectionClass(Filter::class))->newInstanceArgs($config);
168
    }
169
170
171
    /**
172
     * @param mixed $sorts
173
     *
174
     * @throws InvalidArgument
175
     */
176 View Code Duplication
    protected function setSorts($sorts)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
177
    {
178
        if (empty($sorts)) {
179
            return;
180
        }
181
182
        if (is_string($sorts)) {
183
            $sorts = $this->parser->parse($sorts);
184
        }
185
186
        if (!is_array($sorts)) {
187
            throw new InvalidArgument('Search sort is malformed.');
188
        }
189
190
        foreach ($sorts as $sort) {
191
            $this->sorts[] = $sort instanceof Sort ? $sort : $this->createSortFromConfig($sort);
192
        }
193
    }
194
195
196
    /**
197
     * @param array $config
198
     *
199
     * @return Sort
200
     */
201
    protected function createSortFromConfig(array $config)
202
    {
203
        return (new ReflectionClass(Sort::class))->newInstanceArgs($config);
204
    }
205
206
207
    /**
208
     * @param SearchAdapter $adapter
209
     */
210
    private function setAdapter(SearchAdapter $adapter)
211
    {
212
        $this->adapter = $adapter;
213
    }
214
215
216
    /**
217
     * @param StringParser $parser
218
     */
219
    private function setParser(StringParser $parser)
220
    {
221
        $this->parser = $parser;
222
    }
223
}
224