AssignmentObserver   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A creating() 0 4 1
A created() 0 9 1
A updating() 0 6 1
A updated() 0 9 1
A saved() 0 7 1
A deleted() 0 10 1
1
<?php namespace Anomaly\Streams\Platform\Assignment;
2
3
use Anomaly\Streams\Platform\Assignment\Event\AssignmentWasSaved;
4
use Anomaly\Streams\Platform\Assignment\Event\AssignmentWasUpdated;
5
use Anomaly\Streams\Platform\Assignment\Event\AssignmentWasCreated;
6
use Anomaly\Streams\Platform\Assignment\Event\AssignmentWasDeleted;
7
use Anomaly\Streams\Platform\Assignment\Command\AddAssignmentColumn;
8
use Anomaly\Streams\Platform\Assignment\Contract\AssignmentInterface;
9
use Anomaly\Streams\Platform\Assignment\Command\BackupAssignmentData;
10
use Anomaly\Streams\Platform\Assignment\Command\DropAssignmentColumn;
11
use Anomaly\Streams\Platform\Assignment\Command\MoveAssignmentColumn;
12
use Anomaly\Streams\Platform\Assignment\Command\RestoreAssignmentData;
13
use Anomaly\Streams\Platform\Assignment\Command\UpdateAssignmentColumn;
14
use Anomaly\Streams\Platform\Assignment\Command\DeleteAssignmentTranslations;
15
use Anomaly\Streams\Platform\Support\Observer;
16
17
class AssignmentObserver extends Observer
18
{
19
20
    /**
21
     * Fired before creating an assignment.
22
     *
23
     * @param AssignmentInterface|AssignmentModel $model
24
     */
25
    public function creating(AssignmentInterface $model)
26
    {
27
        $model->sort_order = $model->newQuery()->count('id') + 1;
0 ignored issues
show
Bug introduced by
Accessing sort_order on the interface Anomaly\Streams\Platform...act\AssignmentInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
28
    }
29
30
    /**
31
     * Run after a record is created.
32
     *
33
     * @param AssignmentInterface $model
34
     */
35
    public function created(AssignmentInterface $model)
36
    {
37
        $model->flushCache();
38
        $model->compileStream();
39
40
        $this->dispatch(new AddAssignmentColumn($model));
41
42
        $this->events->fire(new AssignmentWasCreated($model));
43
    }
44
45
    /**
46
     * Run before a record is updated.
47
     *
48
     * @param AssignmentInterface $model
49
     */
50
    public function updating(AssignmentInterface $model)
51
    {
52
        $this->dispatch(new BackupAssignmentData($model));
53
        $this->dispatch(new MoveAssignmentColumn($model));
54
        $this->dispatch(new RestoreAssignmentData($model));
55
    }
56
57
    /**
58
     * Run after a record is updated.
59
     *
60
     * @param AssignmentInterface $model
61
     */
62
    public function updated(AssignmentInterface $model)
63
    {
64
        $model->flushCache();
65
        $model->compileStream();
66
67
        $this->dispatch(new UpdateAssignmentColumn($model));
68
69
        $this->events->fire(new AssignmentWasUpdated($model));
70
    }
71
72
    /**
73
     * Run after saving a record.
74
     *
75
     * @param AssignmentInterface $model
76
     */
77
    public function saved(AssignmentInterface $model)
78
    {
79
        $model->flushCache();
80
        $model->compileStream();
81
82
        $this->events->fire(new AssignmentWasSaved($model));
83
    }
84
85
    /**
86
     * Run after a record has been deleted.
87
     *
88
     * @param AssignmentInterface $model
89
     */
90
    public function deleted(AssignmentInterface $model)
91
    {
92
        $model->flushCache();
93
        $model->compileStream();
94
95
        $this->dispatch(new DropAssignmentColumn($model));
96
        $this->dispatch(new DeleteAssignmentTranslations($model));
97
98
        $this->events->fire(new AssignmentWasDeleted($model));
99
    }
100
}
101