PrefixedContainer   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A fetch() 0 4 1
A __get() 0 8 1
1
<?php
2
3
namespace EntWeChat\Support\Traits;
4
5
use EntWeChat\Support\Str;
6
use Pimple\Container;
7
8
/**
9
 * Trait PrefixedContainer.
10
 */
11
trait PrefixedContainer
12
{
13
    /**
14
     * Container.
15
     *
16
     * @var \Pimple\Container
17
     */
18
    protected $container;
19
20
    /**
21
     * ContainerAccess constructor.
22
     *
23
     * @param \Pimple\Container $container
24
     */
25
    public function __construct(Container $container)
26
    {
27
        $this->container = $container;
28
    }
29
30
    /**
31
     * Fetches from pimple container.
32
     *
33
     * @param string $key
34
     *
35
     * @return mixed
36
     */
37
    public function fetch($key)
38
    {
39
        return $this->$key;
40
    }
41
42
    /**
43
     * Gets a parameter or an object from pimple container.
44
     *
45
     * Get the `class basename` of the current class.
46
     * Convert `class basename` to snake-case and concatenation with dot notation.
47
     *
48
     * E.g. Class 'EntWechat', $key foo -> 'ent_wechat.foo'
49
     *
50
     * @param string $key The unique identifier for the parameter or object
51
     *
52
     * @throws \InvalidArgumentException If the identifier is not defined
53
     *
54
     * @return mixed The value of the parameter or an object
55
     */
56
    public function __get($key)
57
    {
58
        $className = basename(str_replace('\\', '/', static::class));
59
60
        $name = Str::snake($className).'.'.$key;
61
62
        return $this->container->offsetGet($name);
63
    }
64
}
65