ContainerMagicTrait   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 0

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 11
lcom 3
cbo 0
dl 0
loc 93
ccs 24
cts 27
cp 0.8889
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B extend() 0 19 5
A factory() 0 6 1
A keys() 0 4 1
A protect() 0 6 1
A raw() 0 10 3
1
<?php
2
declare(strict_types = 1);
3
/**
4
 * Contains trait ContainerMagicTrait.
5
 *
6
 * PHP version 7.0+
7
 *
8
 * LICENSE:
9
 * This file is part of Yet Another Php Eve Api Library also know as Yapeal
10
 * which can be used to access the Eve Online API data and place it into a
11
 * database.
12
 * Copyright (C) 2016-2017 Michael Cummings
13
 *
14
 * This program is free software: you can redistribute it and/or modify it
15
 * under the terms of the GNU Lesser General Public License as published by the
16
 * Free Software Foundation, either version 3 of the License, or (at your
17
 * option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful, but WITHOUT
20
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
22
 * for more details.
23
 *
24
 * You should have received a copy of the GNU Lesser General Public License
25
 * along with this program. If not, see
26
 * <http://spdx.org/licenses/LGPL-3.0.html>.
27
 *
28
 * You should be able to find a copy of this license in the COPYING-LESSER.md
29
 * file. A copy of the GNU GPL should also be available in the COPYING.md file.
30
 *
31
 * @author    Michael Cummings <[email protected]>
32
 * @copyright 2016-2017 Michael Cummings
33
 * @license   LGPL-3.0+
34
 */
35
namespace Yapeal\Container;
36
37
/**
38
 * Trait ContainerMagicTrait.
39
 *
40
 * @method bool offsetExists($id)
41
 * @property array $values
42
 * @property \SplObjectStorage $factories
43
 * @property bool[] $keys
44
 * @property \SplObjectStorage $protected
45
 * @property mixed[] $raw
46
 */
47
trait ContainerMagicTrait
48
{
49
    /**
50
     * Extends an object definition.
51
     *
52
     * Useful when you want to extend an existing object definition,
53
     * without necessarily loading that object.
54
     *
55
     * @param string|int $id       The unique identifier for the object
56
     * @param callable   $callable A service definition to extend the original
57
     *
58
     * @return callable The wrapped callable
59
     *
60
     * @throws \InvalidArgumentException if the identifier is not defined or not a service definition
61
     */
62 5
    public function extend($id, callable $callable): callable
63
    {
64 5
        if (!$this->offsetExists($id)) {
65 1
            throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
66
        }
67 4
        if (!is_object($this->values[$id]) || !method_exists($this->values[$id], '__invoke')) {
68 2
            throw new \InvalidArgumentException(sprintf('Identifier "%s" does not contain an object definition.', $id));
69
        }
70 3
        $factory = $this->values[$id];
71 3
        $extended = function ($c) use ($callable, $factory) {
72
            /** @noinspection PhpMethodParametersCountMismatchInspection */
73 3
            return $callable($factory($c), $c);
74 3
        };
75 3
        if (isset($this->factories[$factory])) {
76
            $this->factories->detach($factory);
77
            $this->factories->attach($extended);
78
        }
79 3
        return $this[$id] = $extended;
80
    }
81
    /**
82
     * Marks a callable as being a factory service.
83
     *
84
     * @param callable $callable A service definition to be used as a factory
85
     *
86
     * @return callable The passed callable
87
     */
88 2
    public function factory(callable $callable): callable
89
    {
90
        /** @noinspection PhpParamsInspection */
91 2
        $this->factories->attach($callable);
92 2
        return $callable;
93
    }
94
    /**
95
     * Returns all defined value names.
96
     *
97
     * @return array An array of value names
98
     */
99 23
    public function keys(): array
100
    {
101 23
        return array_keys($this->keys);
102
    }
103
    /**
104
     * Protects a callable from being interpreted as a service.
105
     *
106
     * This is useful when you want to store a callable as a parameter.
107
     *
108
     * @param callable $callable A callable to protect from being evaluated
109
     *
110
     * @return callable The passed callable
111
     *
112
     * @throws \InvalidArgumentException Service definition has to be a closure of an invokable object
113
     */
114 1
    public function protect(callable $callable): callable
115
    {
116
        /** @noinspection PhpParamsInspection */
117 1
        $this->protected->attach($callable);
118 1
        return $callable;
119
    }
120
    /**
121
     * Gets a parameter or the closure defining an object.
122
     *
123
     * @param string $id The unique identifier for the parameter or object
124
     *
125
     * @return mixed The value of the parameter or the closure defining an object
126
     *
127
     * @throws \InvalidArgumentException if the identifier is not defined
128
     */
129 3
    public function raw(string $id)
130
    {
131 3
        if (!$this->offsetExists($id)) {
132 1
            throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id));
133
        }
134 2
        if (array_key_exists($id, $this->raw)) {
135
            return $this->raw[$id];
136
        }
137 2
        return $this->values[$id];
138
    }
139
}
140