Container   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 40
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

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