Direction::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace mav3rick177\RapidPagination\Base\Query;
4
5
use mav3rick177\RapidPagination\Base\Exceptions\Query\BadKeywordException;
6
7
/**
8
 * Class Direction
9
 */
10
class Direction
11
{
12
    const FORWARD = 'forward';
13
    const BACKWARD = 'backward';
14
15
    /**
16
     * @var string
17
     */
18
    protected $direction;
19
20
    /**
21
     * Direction constructor.
22
     *
23
     * @param string $direction
24
     */
25 17
    public function __construct($direction)
26
    {
27 17
        $this->direction = static::validate($direction);
28
    }
29
30
    /**
31
     * @param $direction
32
     * @return string
33
     */
34 17 View Code Duplication
    protected static function validate($direction)
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...
35
    {
36 17
        $direction = strtolower($direction);
37 17
        if (!preg_match('/\A(forward|backward)\z/', $direction, $m)) {
38
            throw new BadKeywordException('Direction must be "forward" or "backward"');
39
        }
40 17
        return $m[1];
41
    }
42
43
    /**
44
     * @return string
45
     */
46 10
    public function __toString()
47
    {
48 10
        return $this->direction;
49
    }
50
51
    /**
52
     * @return bool
53
     */
54 2
    public function forward()
55
    {
56 2
        return $this->direction === static::FORWARD;
57
    }
58
59
    /**
60
     * @return bool
61
     */
62 17
    public function backward()
63
    {
64 17
        return $this->direction === static::BACKWARD;
65
    }
66
67
    /**
68
     * @return static
69
     */
70
    public function inverse()
71
    {
72
        return new static($this->direction === static::FORWARD ? static::BACKWARD : static::FORWARD);
73
    }
74
}
75