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

LaravelContainer::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
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
}