ContainerAwareCallback   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 52
ccs 15
cts 15
cp 1
rs 10
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isCallableWithAtSign() 0 3 2
A call() 0 22 5
A __construct() 0 5 1
1
<?php
2
3
/*
4
 * This file is part of the StateMachine package.
5
 *
6
 * (c) Alexandre Bacco
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sebdesign\SM\Callback;
13
14
use Illuminate\Contracts\Container\Container as ContainerInterface;
15
use SM\Callback\Callback;
16
use SM\Event\TransitionEvent;
17
18
class ContainerAwareCallback extends Callback
19
{
20
    /**
21
     * @var \Illuminate\Contracts\Container\Container
22
     */
23
    protected $container;
24
25
    /**
26
     * @param  array  $specs  Specification for the callback to be called
27
     * @param  mixed  $callable  Closure, callable or string that will be called if specifications pass
28
     * @param  \Illuminate\Contracts\Container\Container  $container  The service container that will be used to resolve the callable
29
     */
30 24
    public function __construct(array $specs, $callable, ContainerInterface $container)
31
    {
32 24
        parent::__construct($specs, $callable);
33
34 24
        $this->container = $container;
35 8
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 9
    public function call(TransitionEvent $event)
41
    {
42
        // Load the services only now (when the callback is actually called)
43
44
        // Resolve 'Class@method' callables
45 9
        if ($this->isCallableWithAtSign()) {
46 3
            $this->callable = explode('@', $this->callable);
47
        }
48
49
        // Resolve ['Class', 'method'] callables from the container
50 9
        if (is_array($this->callable) && is_string($this->callable[0])) {
51 9
            $id = $this->callable[0];
52
53
            // If the class has no bindings in the container, call method statically.
54 9
            if (! $this->container->bound($id)) {
55 3
                return parent::call($event);
56
            }
57
58 6
            $this->callable[0] = $this->container->make($id);
59
        }
60
61 6
        return parent::call($event);
62
    }
63
64
    /**
65
     * Determine if the given string is in Class@method syntax.
66
     */
67 9
    protected function isCallableWithAtSign(): bool
68
    {
69 9
        return is_string($this->callable) && strpos($this->callable, '@') !== false;
70
    }
71
}
72