Test Failed
Push — ft/states ( d4f7ca...0e1fde )
by Ben
09:10
created

AbstractStateAssistant   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
dl 0
loc 34
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A guard() 0 7 2
A manager() 0 7 2
A can() 0 3 1
1
<?php
2
3
namespace Thinktomorrow\Chief\States;
4
5
use Thinktomorrow\Chief\Management\Manager;
6
use Thinktomorrow\Chief\States\State\StatefulContract;
7
use Thinktomorrow\Chief\Management\Assistants\Assistant;
8
use Thinktomorrow\Chief\Management\Exceptions\NotAllowedManagerRoute;
9
10
abstract class AbstractStateAssistant
11
{
12
    /** @var Manager */
13
    protected $manager;
14
15
    /** @var StatefulContract $model */
16
    protected $model;
17
18
    public function manager(Manager $manager)
19
    {
20
        $this->manager  = $manager;
21
        $this->model    = $manager->model();
22
23
        if(!$this->model instanceof StatefulContract){
24
            throw new \InvalidArgumentException(static::class . ' requires the model to implement the ' . StatefulContract::class);
25
        }
26
    }
27
28
    abstract public static function key(): string;
29
30
    abstract public function route($verb): ?string;
31
32
    public function can($verb): bool
33
    {
34
        return !is_null($this->route($verb));
35
    }
36
37
    public function guard($verb): Assistant
38
    {
39
        if (! $this->can($verb)) {
40
            NotAllowedManagerRoute::notAllowedVerb($verb, $this->manager);
41
        }
42
43
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type Thinktomorrow\Chief\States\AbstractStateAssistant which is incompatible with the type-hinted return Thinktomorrow\Chief\Mana...nt\Assistants\Assistant.
Loading history...
44
    }
45
}
46