SymfonyInteropContainer::getConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php declare(strict_types = 1);
2
3
namespace TomCizek\SymfonyInteropContainer;
4
5
use Psr\Container\ContainerInterface;
6
use Symfony\Component\DependencyInjection\Container;
7
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
8
use Symfony\Component\HttpFoundation\ParameterBag;
9
use Throwable;
10
11
class SymfonyInteropContainer implements ContainerInterface
12
{
13
	/**
14
	 * @var ParameterBag
15
	 */
16
	private $config;
17
18
	/**
19
	 * @var Container
20
	 */
21
	private $container;
22
23 9
	public function __construct(Container $container)
24
	{
25 9
		$this->container = $container;
26 9
		$this->config = new ParameterBag();
27 9
	}
28
29 4
	public function get($key)
30
	{
31 4
		return $this->tryToGetByKey($key);
32
	}
33
34 8
	private function tryToGetByKey($key)
35
	{
36
		try {
37 8
			if ($this->isKeyConfig($key)) {
38 2
				return $this->getConfig();
39
			}
40
41 4
			return $this->container->get($key);
42 4
		} catch (ServiceNotFoundException $e) {
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Depend...erviceNotFoundException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
43 2
			throw new NotFoundException($e->getMessage());
44 2
		} catch (Throwable $e) {
45 2
			throw new ContainerException($e->getMessage());
46
		}
47
	}
48
49 6
	private function isKeyConfig(string $key): bool
50
	{
51 6
		return $key === 'config';
52
	}
53
54 2
	private function getConfig()
55
	{
56 2
		return $this->config->all();
57
	}
58
59
	/**
60
	 * {@inheritdoc}
61
	 */
62 4
	public function has($key)
63
	{
64
		try {
65 4
			return (bool)$this->tryToGetByKey($key);
66 2
		} catch (NotFoundException $e) {
67 1
			return false;
68
		}
69
70
	}
71
72 8
	public function mergeOverConfigOnKey(string $key, array $config)
73
	{
74 8
		$this->config->add([$key => $config]);
75 8
	}
76
}
77