Completed
Push — 0.3 ( da43ab...625b56 )
by Ben
96:17 queued 52:29
created

AssistedManager::getAssistantClass()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
3
4
namespace Thinktomorrow\Chief\Management\Assistants;
5
6
use Thinktomorrow\Chief\Management\Exceptions\MissingAssistant;
7
8
trait AssistedManager
9
{
10
    protected $assistants = [];
11
12
    /**
13
     * Check if this manager is assisted by a certain assistant
14
     *
15
     * @param string $assistant
16
     * @return bool
17
     */
18 78
    public function isAssistedBy(string $assistant): bool
19
    {
20 78
        return !! $this->getAssistantClass($assistant);
21
    }
22
23 91
    public function assistants(): array
24
    {
25 91
        $assistants = [];
26
27 91
        foreach ($this->assistants as $assistant) {
28 67
            $assistants[] = $this->assistant($assistant);
29
        }
30
31 91
        return $assistants;
32
    }
33
34
    /**
35
     * Instantiate the assistant
36
     *
37
     * @param string $assistant
38
     * @return Assistant
39
     * @throws \Exception
40
     */
41 76
    public function assistant(string $assistant): Assistant
42
    {
43 76
        if (! $this->isAssistedBy($assistant)) {
44 1
            throw new MissingAssistant('No assistant [' . $assistant . '] present on manager ' . get_class($this));
45
        }
46
47 75
        $instance = app($this->getAssistantClass($assistant));
48 75
        $instance->manager($this);
0 ignored issues
show
Bug introduced by
The method manager() does not exist on Illuminate\Contracts\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

48
        $instance->/** @scrutinizer ignore-call */ 
49
                   manager($this);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
50 75
        return $instance;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $instance could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the type-hinted return Thinktomorrow\Chief\Mana...nt\Assistants\Assistant. Consider adding an additional type-check to rule them out.
Loading history...
51
    }
52
53 78
    private function getAssistantClass($assistant): ?string
54
    {
55 78
        if (in_array($assistant, $this->assistants)) {
56 67
            return $assistant;
57
        }
58
59 57
        foreach ($this->assistants as $class) {
60 54
            if ($assistant == $class::key()) {
61 54
                return $class;
62
            }
63
        }
64
65 3
        return null;
66
    }
67
}
68