Completed
Push — master ( 91ba10...42ada5 )
by Joschi
02:37
created

Revision::increment()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 2
eloc 2
nc 2
nop 0
crap 2
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 49
    public function __construct($revision)
68
    {
69
        // If the revision number is invalid
70 49
        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 48
        $this->revision = $revision;
78 48
    }
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 53
    public static function isValidRevision($revision)
87
    {
88 53
        return ($revision === self::CURRENT) || (is_int($revision) && ($revision >= 1));
89
    }
90
91
    /**
92
     * Unserialize the string representation of this property
93
     *
94
     * @param string $str Serialized property
95
     * @return Revision Revision property
96
     */
97 18
    public static function unserialize($str)
98
    {
99 18
        return new static(intval($str));
100
    }
101
102
    /**
103
     * Test whether this is the current revision
104
     *
105
     * @return bool Is current revision
106
     */
107 16
    public function isCurrent()
108
    {
109 16
        return $this->revision === self::CURRENT;
110
    }
111
112
    /**
113
     * Serialize the property
114
     *
115
     * @return mixed Property serialization
116
     */
117 2
    public function serialize()
118
    {
119 2
        return $this->getRevision();
120
    }
121
122
    /**
123
     * Return the object revision number
124
     *
125
     * @return int Object revision number
126
     */
127 23
    public function getRevision()
128
    {
129 23
        return $this->revision;
130
    }
131
132
    /**
133
     * Return an incremented revision
134
     *
135
     * @return Revision Incremented revision
136
     */
137 1
    public function increment() {
138 1
        return ($this->revision === self::CURRENT) ? $this : new static($this->revision + 1);
139
    }
140
}
141