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

AssistedManager::assistantsAsClassNames()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 $assistantKey
16
     * @return bool
17
     */
18 105
    public function isAssistedBy(string $assistantKey): bool
19
    {
20 105
        return !! $this->getAssistantClass($assistantKey);
21
    }
22
23 110
    /**
24
     * @param bool $asInstances
25 110
     * @return array
26
     * @throws \Exception
27 110
     */
28 84
    public function assistants($asInstances = true): array
29
    {
30
        $assistants = [];
31 110
32
        foreach ($this->assistants as $assistant) {
33
            $assistants[] = $asInstances ? $this->assistant($assistant) : $assistant;
34
        }
35
36
        return $assistants;
37
    }
38
39
    public function assistantsAsClassNames()
40
    {
41 99
        return $this->assistants(false);
42
    }
43 99
44 1
    /**
45
     * Instantiate the assistant
46
     *
47 98
     * @param string $assistantKey
48 98
     * @return Assistant
49
     * @throws \Exception
50 98
     */
51
    public function assistant(string $assistantKey): Assistant
52
    {
53 105
        if (! $this->isAssistedBy($assistantKey)) {
54
            throw new MissingAssistant('No assistant [' . $assistantKey . '] registered on manager ' . get_class($this));
55 105
        }
56 84
57
        $instance = app($this->getAssistantClass($assistantKey));
58
        $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

58
        $instance->/** @scrutinizer ignore-call */ 
59
                   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...
59 24
60 18
        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...
61 18
    }
62
63
    /**
64
     * Get assistant class by key or assistant classname
65 6
     *
66
     * @param string $assistantKey
67
     * @return string|null
68
     */
69
    private function getAssistantClass(string $assistantKey): ?string
70
    {
71
        foreach ($this->assistants as $class) {
72
            if ($assistantKey == $class::key()) {
73
                return $class;
74
            }
75
        }
76
77
        // Alternatively, check if the passed argument is the assistant class name
78
        if (in_array($assistantKey, $this->assistants)) {
79
            return $assistantKey;
80
        }
81
82
        return null;
83
    }
84
85
    public function addAssistant(string $assistantClass)
86
    {
87
        if(false === array_search($assistantClass, $this->assistants)) {
88
            $this->assistants[] = $assistantClass;
89
        }
90
91
        return $this;
92
    }
93
}
94