Completed
Push — master ( e81718...2850eb )
by Chauncey
02:26
created

RevisionableTrait::getObjectRevisionClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Charcoal\Object;
4
5
use InvalidArgumentException;
6
7
// From 'charcoal-core'
8
use Charcoal\Loader\CollectionLoader;
9
10
// From 'charcoal-object'
11
use Charcoal\Object\ObjectRevision;
12
use Charcoal\Object\ObjectRevisionInterface;
13
14
/**
15
 *
16
 */
17
trait RevisionableTrait
18
{
19
    /**
20
     * @var boolean $revisionEnabled
21
     */
22
    protected $revisionEnabled = true;
23
24
    /**
25
     * The class name of the object revision model.
26
     *
27
     * Must be a fully-qualified PHP namespace and an implementation of
28
     * {@see \Charcoal\Object\ObjectRevisionInterface}. Used by the model factory.
29
     *
30
     * @var string
31
     */
32
    private $objectRevisionClass = ObjectRevision::class;
33
34
    /**
35
     * @param  boolean $enabled The (revision) enabled flag.
36
     * @return RevisionableInterface Chainable
37
     */
38
    public function setRevisionEnabled($enabled)
39
    {
40
        $this->revisionEnabled = !!$enabled;
41
        return $this;
42
    }
43
44
    /**
45
     * @return boolean
46
     */
47
    public function getRevisionEnabled()
48
    {
49
        return $this->revisionEnabled;
50
    }
51
52
    /**
53
     * Create a revision object.
54
     *
55
     * @return ObjectRevisionInterface
56
     */
57
    public function createRevisionObject()
58
    {
59
        $rev = $this->modelFactory()->create($this->getObjectRevisionClass());
60
61
        return $rev;
62
    }
63
64
    /**
65
     * Set the class name of the object revision model.
66
     *
67
     * @param  string $className The class name of the object revision model.
68
     * @throws InvalidArgumentException If the class name is not a string.
69
     * @return AbstractPropertyDisplay Chainable
70
     */
71
    protected function setObjectRevisionClass($className)
72
    {
73
        if (!is_string($className)) {
74
            throw new InvalidArgumentException(
75
                'Route class name must be a string.'
76
            );
77
        }
78
79
        $this->objectRevisionClass = $className;
80
        return $this;
81
    }
82
83
    /**
84
     * Retrieve the class name of the object revision model.
85
     *
86
     * @return string
87
     */
88
    public function getObjectRevisionClass()
89
    {
90
        return $this->objectRevisionClass;
91
    }
92
93
    /**
94
     * Alias of {@see self::getObjectRevisionClass()}.
95
     *
96
     * @return string
97
     */
98
    public function objectRevisionClass()
99
    {
100
        return $this->getObjectRevisionClass();
101
    }
102
103
    /**
104
     * @see \Charcoal\Object\ObjectRevision::create_fromObject()
105
     * @return ObjectRevision
106
     */
107
    public function generateRevision()
108
    {
109
        $rev = $this->createRevisionObject();
110
111
        $rev->createFromObject($this);
112
        if (!empty($rev->getDataDiff())) {
113
            $rev->save();
114
        }
115
116
        return $rev;
117
    }
118
119
    /**
120
     * @see \Charcoal\Object\ObejctRevision::lastObjectRevision
121
     * @return ObjectRevision
122
     */
123
    public function latestRevision()
124
    {
125
        $rev = $this->createRevisionObject();
126
        $rev = $rev->lastObjectRevision($this);
127
128
        return $rev;
129
    }
130
131
    /**
132
     * @see \Charcoal\Object\ObejctRevision::objectRevisionNum
133
     * @param  integer $revNum The revision number.
134
     * @return ObjectRevision
135
     */
136
    public function revisionNum($revNum)
137
    {
138
        $rev = $this->createRevisionObject();
139
        $rev = $rev->objectRevisionNum($this, intval($revNum));
140
141
        return $rev;
142
    }
143
144
    /**
145
     * Retrieves all revisions for the current objet
146
     *
147
     * @param  callable $callback Optional object callback.
148
     * @return array
149
     */
150
    public function allRevisions(callable $callback = null)
151
    {
152
        $loader = new CollectionLoader([
153
            'logger'  => $this->logger,
0 ignored issues
show
Bug introduced by
The property logger does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
154
            'factory' => $this->modelFactory(),
155
        ]);
156
        $loader->setModel($this->createRevisionObject());
0 ignored issues
show
Documentation introduced by
$this->createRevisionObject() is of type object<Charcoal\Object\ObjectRevisionInterface>, but the function expects a string|object<Charcoal\Model\ModelInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
157
        $loader->addFilter('targetType', $this->objType());
0 ignored issues
show
Bug introduced by
It seems like objType() 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...
158
        $loader->addFilter('targetId', $this->id());
0 ignored issues
show
Bug introduced by
It seems like id() 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...
159
        $loader->addOrder('revTs', 'desc');
160
        if ($callback !== null) {
161
            $loader->setCallback($callback);
162
        }
163
        $revisions = $loader->load();
164
165
        return $revisions->objects();
166
    }
167
168
    /**
169
     * @param  integer $revNum The revision number to revert to.
170
     * @throws InvalidArgumentException If revision number is invalid.
171
     * @return boolean Success / Failure.
172
     */
173
    public function revertToRevision($revNum)
174
    {
175
        if (!$revNum) {
176
            throw new InvalidArgumentException(
177
                'Invalid revision number'
178
            );
179
        }
180
181
        $rev = $this->revisionNum(intval($revNum));
182
183
        if (!$rev->id()) {
184
            return false;
185
        }
186
187
        if (isset($obj['lastModifiedBy'])) {
0 ignored issues
show
Bug introduced by
The variable $obj seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
188
            $obj['lastModifiedBy'] = $rev->getRevUser();
189
        }
190
191
        $this->setData($rev->getDataObj());
0 ignored issues
show
Bug introduced by
It seems like setData() 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...
192
        $this->update();
0 ignored issues
show
Bug introduced by
It seems like update() 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...
193
194
        return true;
195
    }
196
197
    /**
198
     * Retrieve the object model factory.
199
     *
200
     * @return \Charcoal\Factory\FactoryInterface
201
     */
202
    abstract public function modelFactory();
203
204
    /**
205
     * @return \Charcoal\Model\MetadataInterface
206
     */
207
    abstract public function metadata();
208
}
209