Completed
Push — master ( 748604...d4ad18 )
by Marcel
02:15
created

LaravelContainer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 52
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 7 2
A has() 0 12 4
1
<?php
2
3
namespace BotMan\BotMan\Container;
4
5
use ReflectionException;
6
use Psr\Container\ContainerInterface;
7
use Illuminate\Contracts\Container\Container;
8
use Psr\Container\NotFoundExceptionInterface;
9
use Psr\Container\ContainerExceptionInterface;
10
use Illuminate\Container\EntryNotFoundException;
11
12
class LaravelContainer implements ContainerInterface {
13
14
    /** @var Container */
15
    protected $container;
16
17
    public function __construct(Container $container)
18
    {
19
        $this->container = $container;
20
    }
21
22
    /**
23
     * Finds an entry of the container by its identifier and returns it.
24
     *
25
     * @param string $id Identifier of the entry to look for.
26
     *
27
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier.
28
     * @throws ContainerExceptionInterface Error while retrieving the entry.
29
     *
30
     * @return mixed Entry.
31
     */
32
    public function get($id)
33
    {
34
        if ($this->has($id)) {
35
            return $this->container->make($id);
36
        }
37
        throw new EntryNotFoundException;
38
    }
39
40
    /**
41
     * Returns true if the container can return an entry for the given identifier.
42
     * Returns false otherwise.
43
     *
44
     * `has($id)` returning true does not mean that `get($id)` will not throw an exception.
45
     * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`.
46
     *
47
     * @param string $id Identifier of the entry to look for.
48
     *
49
     * @return bool
50
     */
51
    public function has($id)
52
    {
53
        if ($this->container->bound($id) || $this->container->resolved($id)) {
54
            return true;
55
        }
56
        try {
57
            $this->container->make($id);
58
            return true;
59
        } catch (ReflectionException $e) {
60
            return false;
61
        }
62
    }
63
}