Completed
Pull Request — master (#14)
by Pavel
28:57
created

StaticApiFactory::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 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
    public function __construct($name)
22
    {
23
        $this->name = (string)$name;
24
        if (!class_exists($this->name)) {
25
            throw new \LogicException(sprintf('Class %s does not exist', $name));
26
        }
27
        if (!in_array(StaticApiFactoryInterface::class, class_implements($name), true)) {
28
            throw new \LogicException(sprintf('Class %s is not a static factory', $name));
29
        }
30
    }
31
32
    public function createApi(RpcClientInterface $client, ApiMetadata $metadata)
33
    {
34
        /** @var StaticApiFactoryInterface $class */
35
        $class = $this->name;
36
37
        return $class::createApi($client, $metadata);
38
    }
39
}
40