Completed
Push — master ( dcb869...651674 )
by Dmitry
02:44
created

Trigger::run()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4.0092

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
ccs 11
cts 12
cp 0.9167
rs 8.9197
cc 4
eloc 11
nc 4
nop 0
crap 4.0092
1
<?php
2
3
namespace Basis\Job\Module;
4
5
use Basis\Job;
6
use Exception;
7
8
class Trigger extends Job
9
{
10
    public $space;
11
    public $id;
12
    public $type;
13
14 1
    public function run()
15
    {
16 1
        $this->require('space');
17 1
        $this->require('id');
18
19 1
        if (!in_array($this->type, ['create', 'update', 'remove'])) {
20
            throw new Exception("Invalid type $this->type");
21
        }
22
23 1
        $method = 'after'.ucfirst($this->type);
24
25 1
        $entity = $this->findOrFail($this->space, $this->id);
26
27
        // afterUpdate trigger will be called on save
28 1
        if ($this->type != 'update') {
29 1
            if (method_exists($entity, $method)) {
30 1
                $entity->$method();
31
            }
32
        }
33
34 1
        $entity->save();
35 1
    }
36
}
37