ContainerAwareCallbackFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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\Auth\Access\Gate;
15
use Illuminate\Contracts\Container\Container as ContainerInterface;
16
use SM\Callback\CallbackFactory;
17
use SM\Callback\CallbackInterface;
18
use SM\SMException;
19
20
class ContainerAwareCallbackFactory extends CallbackFactory
21
{
22
    /**
23
     * @var \Illuminate\Contracts\Container\Container
24
     */
25
    protected $container;
26
27
    /**
28
     * @param  string  $class  The CallbackFactory
29
     * @param  \Illuminate\Contracts\Container\Container  $container  The service container that will be used to resolve the callable
30
     *
31
     * @throws \SM\SMException if the CallbackFactory class does not exist
32
     */
33 84
    public function __construct($class, ContainerInterface $container)
34
    {
35 84
        parent::__construct($class);
36
37 84
        $this->container = $container;
38 28
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 27
    public function get(array $specs): CallbackInterface
44
    {
45 27
        if (isset($specs['can'])) {
46 18
            return new GateCallback($specs, $this->container->make(Gate::class));
47
        }
48
49 24
        if (! isset($specs['do'])) {
50 3
            throw new SMException(sprintf(
51 3
                'CallbackFactory::get needs the index "do" to be able to build a callback, array %s given.',
52 3
                json_encode($specs)
53 2
            ));
54
        }
55
56 21
        $class = $this->class;
57
58 21
        return new $class($specs, $specs['do'], $this->container);
59
    }
60
}
61