IterableTrait::next()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
ccs 5
cts 5
cp 1
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\Traits;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Domain\Model\Object\AbstractObject;
41
use Apparat\Object\Domain\Model\Object\ObjectInterface;
42
use Apparat\Object\Domain\Model\Object\OutOfBoundsException;
43
use Apparat\Object\Domain\Model\Object\Revision;
44
45
/**
46
 * Iterable trait
47
 *
48
 * @package Apparat\Object\Domain
49
 * @property Revision $currentRevision
50
 * @property Revision $latestRevision
51
 * @property AbstractObject|\Countable $this
52
 */
53
trait IterableTrait
54
{
55
    /**
56
     * Revision iterator
57
     *
58
     * @var Revision
59
     */
60
    protected $nextRevision;
61
62
    /**
63
     * Return the current revision
64
     *
65
     * @return ObjectInterface Self reference
66
     */
67 2
    public function current()
68
    {
69 2
        return $this->useRevision($this->nextRevision);
70
    }
71
72
    /**
73
     * Forward to the next revision
74
     */
75 2
    public function next()
76
    {
77 2
        $this->nextRevision = $this->nextRevision->increment();
78 2
        if ($this->nextRevision->getRevision() == $this->latestRevision->getRevision()) {
79 1
            $this->nextRevision = $this->nextRevision->setDraft($this->latestRevision->isDraft());
80
        }
81 2
    }
82
83
    /**
84
     * Return the current revision number
85
     *
86
     * @return Revision Current (next) revision
87
     */
88 1
    public function key()
89
    {
90 1
        return $this->nextRevision;
91
    }
92
93
    /**
94
     * Return whether the current revision is valid
95
     *
96
     * @return bool Current revision is valid
97
     */
98 2
    public function valid()
99
    {
100
        /** @var AbstractObject $that */
101 2
        $that =& $this;
102 2
        return $this->nextRevision->getRevision() <= count($that);
103
    }
104
105
    /**
106
     * Rewind to the first revision
107
     */
108 2
    public function rewind()
109
    {
110 2
        $this->nextRevision = ($this->latestRevision->getRevision() > 1) ?
111 2
            Kernel::create(Revision::class, [1]) : $this->latestRevision;
112 2
        $this->useRevision($this->nextRevision);
113 2
    }
114
115
    /**
116
     * Return the number of available revisions
117
     *
118
     * @return int Number of available revisions
119
     */
120 2
    public function count()
121
    {
122 2
        return $this->latestRevision->getRevision();
123
    }
124
125
    /**
126
     * Use a specific object revision
127
     *
128
     * @param Revision $revision Revision to be used
129
     * @return ObjectInterface Object
130
     * @throws OutOfBoundsException If a revision beyond the latest one is requested
131
     */
132
    abstract public function useRevision(Revision $revision);
133
}
134