Passed
Pull Request — master (#3)
by Alex
02:09
created

AbstractPsrAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arp\Container\Adapter;
6
7
use Arp\Container\Adapter\Exception\AdapterException;
8
use Arp\Container\Adapter\Exception\NotFoundException;
9
use Psr\Container\ContainerExceptionInterface;
10
use Psr\Container\ContainerInterface;
11
use Psr\Container\NotFoundExceptionInterface;
12
13
/**
14
 * This class can be used base class for adapters proxying method calls to a PSR Container.
15
 * Extending classes must implement the remaining requirements of ContainerAdapterInterface.
16
 *
17
 * @author  Alex Patterson <[email protected]>
18
 * @package Arp\Container\Adapter
19
 */
20
abstract class AbstractPsrAdapter implements ContainerAdapterInterface
21
{
22
    /**
23
     * @var ContainerInterface
24
     */
25
    protected ContainerInterface $container;
26
27
    /**
28
     * @param ContainerInterface $container
29
     */
30
    public function __construct(ContainerInterface $container)
31
    {
32
        $this->container = $container;
33
    }
34
35
    /**
36
     * @param string $name The name of the service to check.
37
     *
38
     * @return bool
39
     *
40
     * @throws AdapterException If the container raises an error
41
     */
42
    public function hasService(string $name): bool
43
    {
44
        try {
45
            return $this->container->has($name);
46
        } catch (ContainerExceptionInterface $e) {
47
            throw new AdapterException($e->getMessage(), $e->getCode(), $e);
0 ignored issues
show
Bug introduced by
The method getCode() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            throw new AdapterException($e->getMessage(), $e->/** @scrutinizer ignore-call */ getCode(), $e);
Loading history...
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of said class. However, the method does not exist in Psr\Container\NotFoundExceptionInterface. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
            throw new AdapterException($e->/** @scrutinizer ignore-call */ getMessage(), $e->getCode(), $e);
Loading history...
48
        }
49
    }
50
51
    /**
52
     * @param string $name The name of the service to return
53
     *
54
     * @return mixed
55
     *
56
     * @throws NotFoundException
57
     * @throws AdapterException
58
     */
59
    public function getService(string $name)
60
    {
61
        try {
62
            return $this->container->get($name);
63
        } catch (NotFoundExceptionInterface $e) {
64
            throw new NotFoundException($e->getMessage(), $e->getCode(), $e);
65
        } catch (ContainerExceptionInterface $e) {
66
            throw new AdapterException($e->getMessage(), $e->getCode(), $e);
67
        }
68
    }
69
}
70