Container::has()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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