EnvConfigFactory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 10 1
A setAuth() 0 5 1
A setDefaultAuth() 0 18 3
A setType() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Zored\Telegram\Madeline\Config\Builder;
6
7
use Zored\Telegram\Madeline\Auth\Prompt\PromptInterface;
8
use Zored\Telegram\Madeline\Auth\Prompt\ReadlinePrompt;
9
use Zored\Telegram\Madeline\Config\Auth\AbstractAuthConfig;
10
use Zored\Telegram\Madeline\Config\Auth\AuthConfigInterface;
11
use Zored\Telegram\Madeline\Config\Auth\AuthType;
12
use Zored\Telegram\Madeline\Config\Auth\BotAuth;
13
use Zored\Telegram\Madeline\Config\Auth\ClientAuth;
14
use Zored\Telegram\Madeline\Config\Config;
15
use Zored\Telegram\Madeline\Config\ConfigInterface;
16
17
/**
18
 * @codeCoverageIgnore
19
 */
20
class EnvConfigFactory implements ConfigFactoryInterface
21
{
22
    /**
23
     * @var string
24
     */
25
    private $type = AuthType::BOT;
26
27
    /**
28
     * @var AuthConfigInterface
29
     */
30
    private $auth;
31
32
    /**
33
     * @var PromptInterface|null
34
     */
35
    private $prompt;
36
37
    public function create(): ConfigInterface
38
    {
39
        $this->setDefaultAuth();
40
41
        return (new Config(
42
            (int) getenv('TELEGRAM_API_ID'),
43
            (string) getenv('TELEGRAM_API_HASH'),
44
            $this->auth,
45
            getenv('TELEGRAM_SESSION') . '_' . $this->type
46
        ))->setLogLevel((int) getenv('TELEGRAM_LOG_LEVEL'));
47
    }
48
49
    public function setAuth(AbstractAuthConfig $auth): self
50
    {
51
        $this->auth = $auth;
52
53
        return $this;
54
    }
55
56
    public function setType(string $type): self
57
    {
58
        $this->type = $type;
59
60
        return $this;
61
    }
62
63
    private function setDefaultAuth(): void
64
    {
65
        if ($this->auth) {
66
            return;
67
        }
68
        if (AuthType::CLIENT === $this->type) {
69
            $this->auth = new ClientAuth(
70
                getenv('TELEGRAM_PHONE'),
71
                null,
72
                null,
73
                $this->prompt ?? new ReadlinePrompt()
74
            );
75
76
            return;
77
        }
78
79
        $this->auth = (new BotAuth(getenv('TELEGRAM_BOT_TOKEN')))
80
            ->setHandleUpdates(true);
81
    }
82
}
83