SaturateTrait   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
iocContainer() 0 1 ?
A saturate() 0 19 4
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Core\Traits;
10
11
use Interop\Container\ContainerInterface;
12
use Interop\Container\Exception\ContainerException;
13
use Spiral\Core\Exceptions\ScopeException;
14
15
/**
16
 * Saturate optional constructor or method argument (class) using internal (usually static)
17
 * container. In most of cases trait is doing nothing since spiral Container populates even
18
 * optional class dependencies.
19
 *
20
 * Avoid using this trait in custom code, it's only a development sugar.
21
 */
22
trait SaturateTrait
23
{
24
    /**
25
     * Must be used only to resolve optional constructor arguments. Use in classes which are
26
     * generally resolved using Container. Default value MUST always be supplied from outside.
27
     *
28
     * @param mixed  $default Default value.
29
     * @param string $class   Requested class.
30
     *
31
     * @return mixed|null|object
32
     *
33
     * @throws ScopeException
34
     */
35 5
    private function saturate($default, $class)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
36
    {
37 5
        if (!empty($default)) {
38 2
            return $default;
39
        }
40
41 3
        $container = $this->iocContainer();
42
43 3
        if (empty($container)) {
44 1
            throw new ScopeException("Unable to saturate '{$class}': no container available");
45
        }
46
47
        //Only when global container is set
48
        try {
49 2
            return $container->get($class);
50 1
        } catch (ContainerException $e) {
51 1
            throw new ScopeException("Unable to saturate '{$class}': {$e->getMessage()}", 0, $e);
52
        }
53
    }
54
55
    /**
56
     * Class specific container.
57
     *
58
     * @return ContainerInterface
59
     */
60
    abstract protected function iocContainer();
61
}
62