Completed
Pull Request — master (#14)
by Pavel
03:32
created

StaticApiFactory::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
crap 3.2098
1
<?php
2
3
namespace Bankiru\Api\Doctrine\ApiFactory;
4
5
use Bankiru\Api\Doctrine\ApiFactoryInterface;
6
use Bankiru\Api\Doctrine\Mapping\ApiMetadata;
7
use ScayTrase\Api\Rpc\RpcClientInterface;
8
9
final class StaticApiFactory implements ApiFactoryInterface
10
{
11
    /** @var string */
12
    private $name;
13
14
    /**
15
     * StaticApiFactory constructor.
16
     *
17
     * @param string $name
18
     *
19
     * @throws \LogicException
20
     */
21 17
    public function __construct($name)
22
    {
23 17
        $this->name = (string)$name;
24 17
        if (!class_exists($this->name)) {
25
            throw new \LogicException(sprintf('Class %s does not exist', $name));
26
        }
27 17
        if (!in_array(StaticApiFactoryInterface::class, class_implements($name), true)) {
28
            throw new \LogicException(sprintf('Class %s is not a static factory', $name));
29
        }
30 17
    }
31
32 17
    public function createApi(RpcClientInterface $client, ApiMetadata $metadata)
33
    {
34
        /** @var StaticApiFactoryInterface $class */
35 17
        $class = $this->name;
36
37 17
        return $class::createApi($client, $metadata);
38
    }
39
}
40