LaravelContainerAdapter::get()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (c) 2015 Juan José Torroglosa Ramón
4
 *
5
 * This file is part of the Cliphar package.
6
 *
7
 * For the full copyright and license information, please view
8
 * the LICENSE file that was distributed with this source code.
9
 */
10
11
namespace Cliphar\Container\Laravel;
12
13
use Cliphar\Container\Exception\ContainerException;
14
use Cliphar\Container\Exception\NotFoundException;
15
use Illuminate\Container\Container as LaravelContainerInterface;
16
use Interop\Container\ContainerInterface as AcclimateContainerInterface;
17
18
/**
19
 * An adapter from a Laravel Container to the standardized ContainerInterface
20
 */
21
class LaravelContainerAdapter implements AcclimateContainerInterface
22
{
23
    /**
24
     * @var LaravelContainerInterface A Laravel Container
25
     */
26
    private $container;
27
28
    /**
29
     * @param LaravelContainerInterface $container A Laravel Container
30
     */
31
    public function __construct(LaravelContainerInterface $container)
32
    {
33
        $this->container = $container;
34
    }
35
36
    public function get($id)
37
    {
38
        if ($this->has($id)) {
39
            try {
40
                return $this->container->make($id);
41
            } catch (\Exception $prev) {
42
                throw new ContainerException(sprintf("An exception occurred trying to resolve %s, see previous", $id), 0, $prev);
43
            }
44
        } else {
45
            throw new NotFoundException(sprintf('There is no entry found in the container for %s and it is not an instantiable class', $id));
46
        }
47
    }
48
49
    public function has($id)
50
    {
51
        return $this->container->bound($id) || class_exists($id);
52
    }
53
}