EloquentObserver::saved()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php namespace Anomaly\Streams\Platform\Model;
2
3
use Anomaly\Streams\Platform\Model\Command\CascadeDelete;
4
use Anomaly\Streams\Platform\Model\Command\CascadeRestore;
5
use Anomaly\Streams\Platform\Model\Event\ModelsWereDeleted;
6
use Anomaly\Streams\Platform\Model\Event\ModelsWereUpdated;
7
use Anomaly\Streams\Platform\Model\Event\ModelWasCreated;
8
use Anomaly\Streams\Platform\Model\Event\ModelWasDeleted;
9
use Anomaly\Streams\Platform\Model\Event\ModelWasRestored;
10
use Anomaly\Streams\Platform\Model\Event\ModelWasSaved;
11
use Anomaly\Streams\Platform\Model\Event\ModelWasUpdated;
12
use Anomaly\Streams\Platform\Support\Observer;
13
use Illuminate\Database\Eloquent\Model;
14
15
/**
16
 * Class EloquentObserver
17
 *
18
 * @link    http://pyrocms.com/
19
 * @author  PyroCMS, Inc. <[email protected]>
20
 * @author  Ryan Thompson <[email protected]>
21
 */
22
class EloquentObserver extends Observer
23
{
24
25
    /**
26
     * Run after a record is created.
27
     *
28
     * @param EloquentModel $model
29
     */
30
    public function creating(EloquentModel $model)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
    {
32
        return true;
33
    }
34
35
    /**
36
     * Run after a record is created.
37
     *
38
     * @param EloquentModel $model
39
     */
40
    public function created(EloquentModel $model)
41
    {
42
        $model->flushCache();
43
44
        $this->events->fire(new ModelWasCreated($model));
45
    }
46
47
    /**
48
     * Run after saving a record.
49
     *
50
     * @param EloquentModel $model
51
     */
52
    public function saved(EloquentModel $model)
53
    {
54
        $model->flushCache();
55
56
        $this->events->fire(new ModelWasSaved($model));
57
    }
58
59
    /**
60
     * Run after a record has been updated.
61
     *
62
     * @param EloquentModel $model
63
     */
64
    public function updated(EloquentModel $model)
65
    {
66
        $model->flushCache();
67
68
        $this->events->fire(new ModelWasUpdated($model));
69
    }
70
71
    /**
72
     * Run after multiple records have been updated.
73
     *
74
     * @param EloquentModel $model
75
     */
76
    public function updatedMultiple(EloquentModel $model)
77
    {
78
        $model->flushCache();
79
80
        $this->events->fire(new ModelsWereUpdated($model));
81
    }
82
83
    /**
84
     * Run before a record is deleted.
85
     *
86
     * @param  EloquentModel $entry
87
     * @return bool
88
     */
89
    public function deleting(EloquentModel $entry)
90
    {
91
        $this->dispatch(new CascadeDelete($entry));
92
93
        return true;
94
    }
95
96
    /**
97
     * Run after a record has been deleted.
98
     *
99
     * @param EloquentModel $model
100
     */
101
    public function deleted(EloquentModel $model)
102
    {
103
        $model->flushCache();
104
105
        /* @var Model $translation */
106
        if ($model->isTranslatable()) {
107
            foreach ($model->translations as $translation) {
0 ignored issues
show
Documentation introduced by
The property translations does not exist on object<Anomaly\Streams\P...rm\Model\EloquentModel>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
108
                $translation->delete();
109
            }
110
        }
111
112
        $this->events->fire(new ModelWasDeleted($model));
113
    }
114
115
    /**
116
     * Run after multiple records have been deleted.
117
     *
118
     * @param EloquentModel $model
119
     */
120
    public function deletedMultiple(EloquentModel $model)
121
    {
122
        $model->flushCache();
123
124
        $this->events->fire(new ModelsWereDeleted($model));
125
    }
126
127
    /**
128
     * Fired just before restoring.
129
     *
130
     * @param EloquentModel $model
131
     */
132
    public function restoring(EloquentModel $model)
0 ignored issues
show
Unused Code introduced by
The parameter $model is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
    {
134
        //
135
    }
136
137
    /**
138
     * Run after a record has been restored.
139
     *
140
     * @param EloquentModel $model
141
     */
142
    public function restored(EloquentModel $model)
143
    {
144
        $model->flushCache();
145
146
        $this->dispatch(new CascadeRestore($model));
147
148
        $this->events->fire(new ModelWasRestored($model));
149
    }
150
}
151