Test Failed
Push — ft/states ( 0e1fde...7e5f34 )
by Ben
08:31
created

ManagesAssistants   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A assistantsAsClassNames() 0 3 1
A isAssistedBy() 0 3 1
A getAssistantClass() 0 14 4
A assistants() 0 9 3
A assistant() 0 10 2
1
<?php
2
3
4
namespace Thinktomorrow\Chief\Management\Assistants;
5
6
use Thinktomorrow\Chief\Management\Exceptions\MissingAssistant;
7
8
trait ManagesAssistants
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
    public function isAssistedBy(string $assistantKey): bool
19
    {
20
        return !!$this->getAssistantClass($assistantKey);
21
    }
22
23
    /**
24
     * @param bool $asInstances
25
     * @return array
26
     * @throws \Exception
27
     */
28
    public function assistants($asInstances = true): array
29
    {
30
        $assistants = [];
31
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
        return $this->assistants(false);
42
    }
43
44
    /**
45
     * Instantiate the assistant
46
     *
47
     * @param string $assistantKey
48
     * @return Assistant
49
     * @throws \Exception
50
     */
51
    public function assistant(string $assistantKey): Assistant
52
    {
53
        if (!$this->isAssistedBy($assistantKey)) {
54
            throw new MissingAssistant('No assistant [' . $assistantKey . '] registered on manager ' . static::class);
55
        }
56
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
60
        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
    }
62
63
    /**
64
     * Get assistant class by key or assistant classname
65
     *
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