Completed
Push — master ( acf07d...e75790 )
by Joschi
02:54
created

Revision::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Domain\Model\Object;
38
39
use Apparat\Object\Domain\Contract\SerializablePropertyInterface;
40
41
/**
42
 * Object revision
43
 *
44
 * @package Apparat\Object
45
 * @subpackage Apparat\Object\Domain
46
 */
47
class Revision implements SerializablePropertyInterface
48
{
49
    /**
50
     * Current revision
51
     *
52
     * @var null
53
     */
54
    const CURRENT = null;
55
    /**
56
     * Object revision number
57
     *
58
     * @var int
59
     */
60
    protected $revision = null;
61
62
    /**
63
     * Revision constructor
64
     *
65
     * @param int $revision Object revision number
66
     */
67 52
    public function __construct($revision)
68
    {
69
        // If the revision number is invalid
70 52
        if (!self::isValidRevision($revision)) {
71 1
            throw new InvalidArgumentException(
72 1
                sprintf('Invalid object revision number "%s"', $revision),
73 1
                InvalidArgumentException::INVALID_OBJECT_REVISION
74
            );
75
        }
76
77 51
        $this->revision = $revision;
78 51
    }
79
80
    /**
81
     * Test whether a revision number is valid
82
     *
83
     * @param int|NULL $revision Revision number
84
     * @return bool Is valid revision
85
     */
86 56
    public static function isValidRevision($revision)
87
    {
88 56
        return ($revision === self::CURRENT) || (is_int($revision) && ($revision >= 1));
89
    }
90
91
    /**
92
     * Return a current revision instance
93
     *
94
     * @return Revision Current revision instance
95
     */
96 24
    public static function current() {
97 24
        return new static(self::CURRENT);
98
    }
99
100
    /**
101
     * Unserialize the string representation of this property
102
     *
103
     * @param string $str Serialized property
104
     * @return Revision Revision property
105
     */
106 20
    public static function unserialize($str)
107
    {
108 20
        return new static(intval($str));
109
    }
110
111
    /**
112
     * Test whether this is the current revision
113
     *
114
     * @return bool Is current revision
115
     */
116 18
    public function isCurrent()
117
    {
118 18
        return $this->revision === self::CURRENT;
119
    }
120
121
    /**
122
     * Serialize the property
123
     *
124
     * @return mixed Property serialization
125
     */
126 2
    public function serialize()
127
    {
128 2
        return $this->getRevision();
129
    }
130
131
    /**
132
     * Return the object revision number
133
     *
134
     * @return int Object revision number
135
     */
136 25
    public function getRevision()
137
    {
138 25
        return $this->revision;
139
    }
140
141
    /**
142
     * Return an incremented revision
143
     *
144
     * @return Revision Incremented revision
145
     */
146 3
    public function increment() {
147 3
        return ($this->revision === self::CURRENT) ? $this : new static($this->revision + 1);
148
    }
149
}
150