Completed
Push — master ( c9de9a...8fa92c )
by Joschi
06:37
created

IterableTrait   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 71
ccs 19
cts 19
cp 1
rs 10
wmc 8
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A current() 0 4 1
A next() 0 7 2
A valid() 0 5 1
A rewind() 0 6 2
A count() 0 4 1
A key() 0 4 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\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\Revision;
43
44
/**
45
 * Iterable trait
46
 *
47
 * @package Apparat\Object\Domain
48
 * @property Revision $currentRevision
49
 * @property Revision $latestRevision
50
 * @property AbstractObject|\Countable $this
51
 */
52
trait IterableTrait
53
{
54
    /**
55
     * Revision iterator
56
     *
57
     * @var Revision
58
     */
59
    protected $nextRevision;
60
61
    /**
62
     * Return the current revision
63
     *
64
     * @return ObjectInterface Self reference
65
     */
66 2
    public function current()
67
    {
68 2
        return $this->useRevision($this->nextRevision);
0 ignored issues
show
Bug introduced by
It seems like useRevision() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
69
    }
70
71
    /**
72
     * Forward to the next revision
73
     */
74 2
    public function next()
75
    {
76 2
        $this->nextRevision = $this->nextRevision->increment();
77 2
        if ($this->nextRevision->getRevision() == $this->latestRevision->getRevision()) {
78 1
            $this->nextRevision = $this->nextRevision->setDraft($this->latestRevision->isDraft());
79 1
        }
80 2
    }
81
82
    /**
83
     * Return the current revision number
84
     *
85
     * @return Revision Current (next) revision
86
     */
87 1
    public function key()
88
    {
89 1
        return $this->nextRevision;
90
    }
91
92
    /**
93
     * Return whether the current revision is valid
94
     *
95
     * @return bool Current revision is valid
96
     */
97 2
    public function valid()
98
    {
99
        /** @var AbstractObject $this */
100 2
        return $this->nextRevision->getRevision() <= count($this);
0 ignored issues
show
Bug introduced by
The property nextRevision cannot be accessed from this context as it is declared protected in class Apparat\Object\Domain\Mo...ct\Traits\IterableTrait.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
101
    }
102
103
    /**
104
     * Rewind to the first revision
105
     */
106 2
    public function rewind()
107
    {
108 2
        $this->nextRevision = ($this->latestRevision->getRevision() > 1) ?
109 2
            Kernel::create(Revision::class, [1]) : $this->latestRevision;
110 2
        $this->useRevision($this->nextRevision);
0 ignored issues
show
Bug introduced by
It seems like useRevision() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
111 2
    }
112
113
    /**
114
     * Return the number of available revisions
115
     *
116
     * @return int Number of available revisions
117
     */
118 2
    public function count()
119
    {
120 2
        return $this->latestRevision->getRevision();
121
    }
122
}