Passed
Push — master ( d7ad74...0c97da )
by Artem
41s queued 12s
created

ContainerAwareResolver::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Prozorov\Repositories\Resolvers;
6
7
use Prozorov\Repositories\Contracts\{RepositoryInterface, ResolverInterface};
8
use Prozorov\Repositories\Exceptions\CouldNotResolve;
9
use Psr\Container\{ContainerInterface, ContainerExceptionInterface};
10
use InvalidArgumentException;
11
12
class ContainerAwareResolver implements ResolverInterface
13
{
14
    /**
15
     * @var ContainerInterface $container
16
     */
17
    protected $container;
18
19
    public function __construct(ContainerInterface $container)
20
    {
21
        $this->container = $container;
22
    }
23
24
    /**
25
     * @inheritDoc
26
     */
27
    public function resolve(string $className): RepositoryInterface
28
    {
29
        try {
30
            $resolved = $this->container->get($className);
31
32
            if (! ($resolved instanceof RepositoryInterface)) {
33
                $class = get_class($resolved);
34
35
                $message = $class . ' does not implements RepositoryInterface. Refer to documentation';
0 ignored issues
show
Unused Code introduced by
The assignment to $message is dead and can be removed.
Loading history...
36
                $this->fail('Container must return ');
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return Prozorov\Repositories\Co...cts\RepositoryInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
37
            }
38
        } catch (ContainerExceptionInterface $exception) {
39
            $this->fail($exception->getMessage());
0 ignored issues
show
Bug introduced by
The method getMessage() does not exist on Psr\Container\ContainerExceptionInterface. It seems like you code against a sub-type of Psr\Container\ContainerExceptionInterface such as Illuminate\Contracts\Con...dingResolutionException. ( Ignorable by Annotation )

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

39
            $this->fail($exception->/** @scrutinizer ignore-call */ getMessage());
Loading history...
40
        }
41
    }
42
43
    /**
44
     * fail.
45
     *
46
     * @access	protected
47
     * @param	string	$className	
48
     * @return	void
49
     */
50
    protected function fail(string $className, string $message = ''): void
51
    {
52
        $exception = new CouldNotResolve($message);
53
        $exception->setRepositoryCode($className);
54
55
        throw $exception;
56
    }
57
}
58