Completed
Branch master (6ff579)
by Tomasz
06:50 queued 03:50
created

Container::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 1
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
namespace Helpers;
4
5
use Helpers\Repository\DomainRepository;
6
use Hexarchium\CoreDomain\Model\Domain\Repository\DomainRepositoryInterface;
7
use Hexarchium\CoreDomain\UseCase\CreateDomain\UseCase;
8
use Interop\Container\ContainerInterface;
9
10
/**
11
 * Copyright
12
 */
13
class Container extends \ArrayObject implements ContainerInterface
14
{
15
    public function factory()
16
    {
17
        $container = new Container();
18
19
        $container->offsetSet(
20
            DomainRepositoryInterface::class,
21
            new DomainRepository()
22
        );
23
24
        $container->offsetSet(
25
            UseCase::class,
26
            new UseCase(
27
                $container->get(DomainRepositoryInterface::class)
28
            )
29
        );
30
31
        return $container;
32
    }
33
34
    /**
35
     * Finds an entry of the container by its identifier and returns it.
36
     *
37
     * @param string $id Identifier of the entry to look for.
38
     * @return mixed No entry was found for this identifier.
39
     * @throws \Exception
40
     */
41
    public function get($id)
42
    {
43
        if (!$this->has($id)) {
44
            throw new \Exception(sprintf("Given service '%s' don't exist", $id));
45
        }
46
47
        return $this->offsetGet($id);
48
    }
49
50
    /**
51
     * Returns true if the container can return an entry for the given identifier.
52
     * Returns false otherwise.
53
     *
54
     * @param string $id Identifier of the entry to look for.
55
     *
56
     * @return boolean
57
     */
58
    public function has($id)
59
    {
60
        return $this->offsetExists($id);
61
    }
62
}