Direction::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Baleen\Migrations\Migration\Options;
21
22
use Baleen\Migrations\Exception\InvalidArgumentException;
23
use Baleen\Migrations\Common\ValueObjectInterface;
24
25
/**
26
 * Class Direction
27
 * @author Gabriel Somoza <[email protected]>
28
 */
29
final class Direction implements ValueObjectInterface
30
{
31
    const UP = 'up';
32
    const DOWN = 'down';
33
34
    /** @var string */
35
    private $direction;
36
37
    /**
38
     * Direction constructor.
39
     * @param int|string $direction
40
     * @throws InvalidArgumentException
41
     */
42 37
    public function __construct($direction)
43
    {
44 37
        if (is_integer($direction)) {
45 2
            $direction = (int) $direction < 0 ? self::DOWN : self::UP;
46 2
        }
47 37
        $direction = (string) $direction;
48 37
        if (!in_array($direction, [self::UP, self::DOWN])) {
49 1
            throw new InvalidArgumentException(
50 1
                sprintf('Unknown direction "%s". Valid options are "up" or "down".', $direction)
51 1
            );
52
        }
53 36
        $this->direction = $direction;
54 36
    }
55
56
    /**
57
     * Returns whether the direction is up
58
     *
59
     * @return bool
60
     */
61 47
    public function isUp() {
62 47
        return $this->direction === self::UP;
63
    }
64
65
    /**
66
     * Returns whether the direction is down
67
     *
68
     * @return bool
69
     */
70 15
    public function isDown() {
71 15
        return !$this->isUp();
72
    }
73
74
    /**
75
     * Returns the direction as string
76
     *
77
     * @return string
78
     */
79 7
    public function getDirection() {
80 7
       return $this->direction;
81
    }
82
83
    /**
84
     * @inheritdoc
85
     */
86 7
    public function isSameValueAs(ValueObjectInterface $object)
87
    {
88 7
        if (!$object instanceof Direction) {
89 1
            return false;
90
        }
91
92 7
        return $this->isUp() == $object->isUp();
93
    }
94
95
    /**
96
     * Returns a string representation of the object.
97
     *
98
     * @return string
99
     */
100 5
    public function __toString()
101
    {
102 5
        return $this->getDirection();
103
    }
104
105
    /**
106
     * Returns an instance with direction UP
107
     *
108
     * @return static
109
     */
110 29
    public static function up()
111
    {
112 29
        return new static(static::UP);
113
    }
114
115
    /**
116
     * Returns an instance with direction DOWN
117
     *
118
     * @return static
119
     */
120 14
    public static function down()
121
    {
122 14
        return new static(static::DOWN);
123
    }
124
}
125