SymfonyInteropContainer   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 66
ccs 25
cts 25
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 4 1
A tryToGetByKey() 0 14 4
A isKeyConfig() 0 4 1
A getConfig() 0 4 1
A has() 0 9 2
A mergeOverConfigOnKey() 0 4 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