Passed
Push — master ( fd6438...0db353 )
by Shahrad
02:02
created

TelegramTrait   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 14
c 1
b 0
f 0
dl 0
loc 62
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getEnvToken() 0 4 2
A getEnvFilePath() 0 15 3
A getAdminId() 0 3 1
A setAdminId() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TelegramBot\Traits;
5
6
/**
7
 * TelegramTrait class
8
 *
9
 * @link    https://github.com/telegram-bot-php/core
10
 * @author  Shahrad Elahi (https://github.com/shahradelahi)
11
 * @license https://github.com/telegram-bot-php/core/blob/master/LICENSE (MIT License)
12
 */
13
trait TelegramTrait
14
{
15
16
    /**
17
     * Admin chat id
18
     *
19
     * @var int
20
     */
21
    private static int $adminId = -1;
22
23
    /**
24
     * Get env file path and return it
25
     *
26
     * @return string
27
     */
28
    private function getEnvFilePath(): string
29
    {
30
        $defaultEnvPaths = [
31
            $_SERVER['DOCUMENT_ROOT'] . '/.env',
32
            getcwd() . '/../.env',
33
            getcwd() . '/.env',
34
        ];
35
36
        foreach ($defaultEnvPaths as $path) {
37
            if (file_exists($path)) {
38
                return $path;
39
            }
40
        }
41
42
        return '';
43
    }
44
45
    /**
46
     * Get token from env file.
47
     *
48
     * @param string $file
49
     * @return string|null
50
     */
51
    protected function getEnvToken(string $file): string|null
52
    {
53
        if (!file_exists($file)) return null;
54
        return $_ENV['TELEGRAM_BOT_TOKEN'] ?? null;
55
    }
56
57
    /**
58
     * Set the admin chat id
59
     *
60
     * @return int
61
     */
62
    public static function getAdminId(): int
63
    {
64
        return static::$adminId;
0 ignored issues
show
Bug introduced by
Since $adminId is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $adminId to at least protected.
Loading history...
65
    }
66
67
    /**
68
     * Set the admin chat id
69
     *
70
     * @param int $adminId
71
     */
72
    public static function setAdminId(int $adminId): void
73
    {
74
        static::$adminId = $adminId;
0 ignored issues
show
Bug introduced by
Since $adminId is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $adminId to at least protected.
Loading history...
75
    }
76
77
}