ApiFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Madeline;
6
7
use danog\MadelineProto\API;
8
use danog\MadelineProto\MTProto;
9
use Zored\Telegram\Madeline\Api\ApiConstructorInterface;
10
use Zored\Telegram\Madeline\Auth\Handler\AuthHandlerCollection;
11
use Zored\Telegram\Madeline\Auth\Handler\AuthHandlerCollectionInterface;
12
use Zored\Telegram\Madeline\Auth\Handler\BotAuthHandler;
13
use Zored\Telegram\Madeline\Auth\Handler\ClientAuthHandler;
14
use Zored\Telegram\Madeline\Auth\Handler\Exception\AuthHandlerException;
15
use Zored\Telegram\Madeline\Config\ConfigInterface;
16
17
class ApiFactory implements ApiFactoryInterface
18
{
19
    /**
20
     * @var AuthHandlerCollectionInterface
21
     */
22
    private $handlers;
23
24
    public function __construct(AuthHandlerCollectionInterface $handlers = null)
25
    {
26
        $this->handlers = $handlers ?? new AuthHandlerCollection([
27
            new ClientAuthHandler(),
28
            new BotAuthHandler(),
29
        ]);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function create(
36
        ConfigInterface $config,
37
        ApiConstructorInterface $apiConstructor
38
    ): API {
39
        $api = $apiConstructor->create();
40
        $api->session = $config->getSessionPath();
41
        $this->auth($config, $api->API);
42
43
        return $api;
44
    }
45
46
    /**
47
     * @throws AuthHandlerException
48
     */
49
    private function auth(ConfigInterface $config, MTProto $proto): void
50
    {
51
        $this->handlers->get($config->getAuth())->auth($proto);
52
    }
53
}
54