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

StaticApiFactory::createApi()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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