Passed
Push — master ( c6f20f...103448 )
by Robert
04:58
created

ApiConstructor::createFromSessionPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Madeline\Api;
6
7
use danog\MadelineProto\API;
8
use danog\MadelineProto\Exception;
9
use Zored\Telegram\Madeline\Config\ConfigInterface;
10
use Zored\Telegram\Madeline\Config\Extractor\ConfigExtractorInterface;
11
12
/**
13
 * @codeCoverageIgnore
14
 * - because API takes a lot of time to load.
15
 */
16
final class ApiConstructor implements ApiConstructorInterface
17
{
18
    /**
19
     * @var ConfigInterface
20
     */
21
    private $config;
22
23
    /**
24
     * @var ConfigExtractorInterface
25
     */
26
    private $configExtractor;
27
28
    public function __construct(ConfigInterface $config, ConfigExtractorInterface $configExtractor)
29
    {
30
        $this->config = $config;
31
        $this->configExtractor = $configExtractor;
32
    }
33
34
    public function create(): API
35
    {
36
        $path = $this->config->getSessionPath();
37
        if (!file_exists($path)) {
38
            return $this->createFromConfig();
39
        }
40
41
        try {
42
            return $this->createFromSessionPath($path);
43
        } catch (Exception $exception) {
44
            return $this->createFromConfig();
45
        }
46
    }
47
48
    private function createFromConfig(): API
49
    {
50
        return new API($this->configExtractor->extract($this->config));
51
    }
52
53
    /**
54
     * @throws Exception
55
     */
56
    private function     createFromSessionPath(string $path): API
57
    {
58
        return new API($path);
59
    }
60
}
61