Test Failed
Push — master ( c4aa6a...a5e42e )
by Mathieu
03:19
created

RevisionableTrait::getRevisionEnabled()   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->objectRevisionClass());
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 objectRevisionClass()
89
    {
90
        return $this->objectRevisionClass;
91
    }
92
93
    /**
94
     * @return ObjectRevision
95
     * @see \Charcoal\Object\ObjectRevision::create_fromObject()
96
     */
97
    public function generateRevision()
98
    {
99
        $rev = $this->createRevisionObject();
100
101
        $rev->createFromObject($this);
102
        if (!empty($rev->dataDiff())) {
103
            $rev->save();
104
        }
105
106
        return $rev;
107
    }
108
109
    /**
110
     * @return ObjectRevision
111
     * @see \Charcoal\Object\ObejctRevision::lastObjectRevision
112
     */
113
    public function latestRevision()
114
    {
115
        $rev = $this->createRevisionObject();
116
        $rev = $rev->lastObjectRevision($this);
117
118
        return $rev;
119
    }
120
121
    /**
122
     * @param integer $revNum The revision number.
123
     * @return ObjectRevision
124
     * @see \Charcoal\Object\ObejctRevision::objectRevisionNum
125
     */
126
    public function revisionNum($revNum)
127
    {
128
        $rev = $this->createRevisionObject();
129
        $rev = $rev->objectRevisionNum($this, intval($revNum));
130
131
        return $rev;
132
    }
133
134
    /**
135
     * Retrieves all revisions for the current objet
136
     *
137
     * @param callable $callback Optional object callback.
138
     * @return array
139
     */
140
    public function allRevisions(callable $callback = null)
141
    {
142
        $loader = new CollectionLoader([
143
            '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...
144
            'factory'   => $this->modelFactory()
145
        ]);
146
        $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...
147
        $loader->addFilter('target_type', $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...
148
        $loader->addFilter('target_id', $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...
149
        $loader->addOrder('rev_ts', 'desc');
150
        if ($callback !== null) {
151
            $loader->setCallback($callback);
152
        }
153
        $revisions = $loader->load();
154
155
        return $revisions->objects();
156
    }
157
158
    /**
159
     * @param integer $revNum The revision number to revert to.
160
     * @throws InvalidArgumentException If revision number is invalid.
161
     * @return boolean Success / Failure.
162
     */
163
    public function revertToRevision($revNum)
164
    {
165
        if (!$revNum) {
166
            throw new InvalidArgumentException(
167
                'Invalid revision number'
168
            );
169
        }
170
171
        $rev = $this->revisionNum(intval($revNum));
172
173
        if (!$rev->id()) {
174
            return false;
175
        }
176
        if (is_callable([$this, 'setLastModifiedBy'])) {
177
            $this->setLastModifiedBy($rev->revUser());
0 ignored issues
show
Bug introduced by
It seems like setLastModifiedBy() 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...
178
        }
179
        $this->setData($rev->dataObj());
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...
180
        $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...
181
182
        return true;
183
    }
184
185
    /**
186
     * Retrieve the object model factory.
187
     *
188
     * @return \Charcoal\Factory\FactoryInterface
189
     */
190
    abstract public function modelFactory();
191
192
    /**
193
     * @return \Charcoal\Model\MetadataInterface
194
     */
195
    abstract public function metadata();
196
}
197