HasProcessor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 86.67%

Importance

Changes 0
Metric Value
dl 0
loc 68
ccs 13
cts 15
cp 0.8667
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A useFormatter() 0 5 1
A restoreFormatter() 0 5 1
A process() 0 4 1
A useProcessor() 0 5 1
A validateProcessor() 0 7 3
1
<?php
2
3
namespace mav3rick177\RapidPagination\Base\Concerns;
4
5
use mav3rick177\RapidPagination\Base\Contracts\Formatter;
6
use mav3rick177\RapidPagination\Base\AbstractProcessor;
7
use mav3rick177\RapidPagination\Base\Exceptions\InvalidArgumentException;
8
use mav3rick177\RapidPagination\Base\Query;
9
10
trait HasProcessor
11
{
12
    /**
13
     * @var AbstractProcessor
14
     */
15
    protected $processor;
16
17
    /**
18
     * Use custom formatter.
19
     *
20
     * @param  callable|Formatter|string $formatter
21
     * @return $this
22
     */
23 2
    public function useFormatter($formatter)
24
    {
25 2
        $this->processor->useFormatter($formatter);
26 1
        return $this;
27
    }
28
29
    /**
30
     * Restore default formatter.
31
     *
32
     * @return $this
33
     */
34 1
    public function restoreFormatter()
35
    {
36 1
        $this->processor->restoreFormatter();
37 1
        return $this;
38
    }
39
40
    /**
41
     * Get result from external resources.
42
     *
43
     * @param  Query $query
44
     * @param  mixed $rows
45
     * @return mixed
46
     */
47 2
    public function process(Query $query, $rows)
48
    {
49 2
        return $this->processor->process($query, $rows);
50
    }
51
52
    /**
53
     * Use custom processor.
54
     *
55
     * @param  AbstractProcessor|string $processor
56
     * @return $this
57
     */
58 1
    public function useProcessor($processor)
59
    {
60 1
        $this->processor = static::validateProcessor($processor);
61
        return $this;
62
    }
63
64
    /**
65
     * Validate processor and return in normalized form.
66
     *
67
     * @param  mixed             $processor
68
     * @return AbstractProcessor
69
     */
70 1
    protected static function validateProcessor($processor)
71
    {
72 1
        if (!is_subclass_of($processor, AbstractProcessor::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \mav3rick177\RapidPagina...bstractProcessor::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
73 1
            throw new InvalidArgumentException('Processor must be an instanceof ' . AbstractProcessor::class);
74
        }
75
        return is_string($processor) ? new $processor() : $processor;
76
    }
77
}
78