Passed
Push — master ( f59959...13fcd7 )
by Shahrad
12:47
created

EnvironmentsTrait::getAdminId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace TelegramBot\Traits;
5
6
/**
7
 * EnvironmentsTrait 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 EnvironmentsTrait
14
{
15
16
    /**
17
     * Admin chat id
18
     *
19
     * @var int
20
     */
21
    protected static int $adminId = -1;
22
23
    /**
24
     * Get env file path and return it
25
     *
26
     * @return string
27
     */
28
    public static function getEnvFilePath(): string
29
    {
30
        $defaultEnvPaths = [
31
            getcwd() . '/.env',
32
            getcwd() . '/../.env',
33
            $_SERVER['DOCUMENT_ROOT'] . '/.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
     * Set the admin chat id
47
     *
48
     * @return int
49
     */
50
    public static function getAdminId(): int
51
    {
52
        return static::$adminId;
53
    }
54
55
    /**
56
     * Set the admin chat id
57
     *
58
     * @param int $adminId
59
     */
60
    public static function setAdminId(int $adminId): void
61
    {
62
        static::$adminId = $adminId;
63
    }
64
65
    /**
66
     * Get token from env file.
67
     *
68
     * @param string $file
69
     * @return string|null
70
     */
71
    protected function getEnvToken(string $file): string|null
72
    {
73
        if (!file_exists($file)) return null;
74
        return $_ENV['TELEGRAM_BOT_TOKEN'] ?? null;
75
    }
76
77
}