QueryBuilderResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace W2w\Laravel\Apie\Plugins\Illuminate\Eloquent;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Psr\Container\ContainerInterface;
7
use W2w\Lib\Apie\Exceptions\BadConfigurationException;
8
use W2w\Lib\Apie\Exceptions\InvalidClassTypeException;
9
10
class QueryBuilderResolver
11
{
12
    /**
13
     * @var ContainerInterface
14
     */
15
    private $container;
16
17
    public function __construct(ContainerInterface $container)
18
    {
19
        $this->container = $container;
20
    }
21
22
    public function resolveFromString(string $input): Builder
23
    {
24
25
        if (!preg_match('/^(?<service>[a-z0-9A-Z_\\\\]+)@(?<method>[a-z0-9A-Z_-]+)$/', $input, $matches)) {
26
            throw new BadConfigurationException('"' . $input . '" is not in the format <service>@<method>');
27
        }
28
        $service = $this->container->get($matches['service']);
29
        $method = $matches['method'];
30
        if (!is_callable([$service, $method])) {
31
            throw new BadConfigurationException('"' . $method . '" is not callable in service "' . $matches['service'] . '"');
32
        }
33
34
        $result = $service->$method();
35
        if (!($result instanceof Builder)) {
36
            throw new InvalidClassTypeException('queryBuilder', Builder::class);
37
        }
38
        return $result;
39
    }
40
}
41