Psr11Container::get()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 22
Code Lines 13

Duplication

Lines 7
Ratio 31.82 %

Importance

Changes 0
Metric Value
dl 7
loc 22
c 0
b 0
f 0
rs 8.6737
cc 5
eloc 13
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Fundic;
6
7
use Fundic\DataStructure\Dictionary;
8
use Fundic\DataStructure\Maybe\Just;
9
use Fundic\DataStructure\Maybe\Nothing;
10
use Fundic\Exception\ContainerException;
11
use Fundic\Exception\NotFoundException;
12
use Fundic\Factory\ValueFactory;
13
use Psr\Container\NotFoundExceptionInterface;
14
15
final class Psr11Container implements Container
16
{
17
    /**
18
     * @var Dictionary
19
     */
20
    private $values = [];
21
22
    private function __construct(Dictionary $values)
23
    {
24
        $this->values = $values;
25
    }
26
27
    public static function create() : self
28
    {
29
        return new self(Dictionary::empty());
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function has($id) : bool
36
    {
37
        return $this->values->has($id);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function get($id)
44
    {
45
        $maybeFactory = $this->values->get($id);
46
47
        $container = $this;
48
49
        try {
50 View Code Duplication
            switch (true) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
                case ($maybeFactory instanceof Just):
52
                    /** @var Just $maybeFactory */
53
                    return ($maybeFactory->get())($container, $id);
54
                case ($maybeFactory instanceof Nothing):
55
                    throw NotFoundException::forKey($id);
56
            }
57
        } catch (\Throwable $e) {
58
            if ($e instanceof NotFoundExceptionInterface) {
59
                throw $e;
60
            }
61
62
            throw ContainerException::forKeyWithInner($id, $e);
63
        }
64
    }
65
66
    public function add(string $id, ValueFactory $factory) : Container
67
    {
68
        return new self($this->values->add($id, $factory));
69
    }
70
}
71