Completed
Push — master ( 31165a...a6c946 )
by Dmytro
04:37
created

AbstractHandler::sort()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 12
1
<?php
2
declare(strict_types=1);
3
4
namespace Paymaxi\Component\Query\Handler;
5
6
use Paymaxi\Component\Query\Filter\FilterInterface;
7
use Paymaxi\Component\Query\Sort\SortInterface;
8
9
/**
10
 * Class AbstractHandler
11
 *
12
 * @package Paymaxi\Component\Query\Handler
13
 */
14
abstract class AbstractHandler implements SortHandlerInterface, FilterHandlerInterface
15
{
16
    /** @var SortInterface[] */
17
    protected $sortingFields = [];
18
19
    /** @var FilterInterface[] */
20
    protected $filteringFields = [];
21
22
    /**
23
     * @param SortInterface $sort
24
     */
25
    public function addSorting(SortInterface $sort):void
26
    {
27
        $this->sortingFields[] = $sort;
28
    }
29
30
    /**
31
     * @param FilterInterface $filter
32
     */
33 4
    public function addFilter(FilterInterface $filter): void
34
    {
35 4
        $this->filteringFields[] = $filter;
36 4
    }
37
38
    /**
39
     * @param SortInterface $sort
40
     * @param string $order
41
     */
42
    abstract protected function handleSorting(SortInterface $sort, string $order): void;
43
44
    /**
45
     * @param FilterInterface $filter
46
     * @param mixed $value
47
     */
48
    abstract protected function handleFiltering(FilterInterface $filter, $value): void;
49
50
    /**
51
     * @param string $field
52
     * @param string $order
53
     */
54
    public function sort(string $field, string $order): void
55
    {
56
        /** @var SortInterface $sort */
57
        foreach ($this->sortingFields as $sort) {
58
            if ($sort->supports($field)) {
59
                $this->handleSorting($sort, $order);
60
            }
61
        }
62
    }
63
64
    /**
65
     * @param string $field
66
     * @param mixed $value
67
     */
68 3
    public function filter(string $field, $value): void
69
    {
70
        /** @var FilterInterface $filteringField */
71 3
        foreach ($this->filteringFields as $filteringField) {
72 3
            if ($filteringField->supports($field)) {
73 3
                $this->handleFiltering($filteringField, $value);
74
            }
75
        }
76 3
    }
77
}
78