Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.65% covered (success)
95.65%
22 / 23
88.89% covered (warning)
88.89%
8 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Environment
95.65% covered (success)
95.65%
22 / 23
88.89% covered (warning)
88.89%
8 / 9
15
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isProduction
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDevelopment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isTest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRevision
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getVariable
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 setVariable
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
4.07
1<?php
2
3declare(strict_types=1);
4
5namespace PeServer\Core;
6
7use PeServer\Core\Throws\ArgumentException;
8use PeServer\Core\Throws\Throws;
9use PeServer\Core\Throws\EnvironmentException;
10
11/**
12 * 環境情報。
13 */
14class Environment
15{
16    #region function
17
18    public function __construct(
19        string $locale,
20        string $language,
21        string $timezone,
22        private string $environment,
23        private string $revision
24    ) {
25        setlocale(LC_ALL, $locale);
26        mb_language($language);
27        date_default_timezone_set($timezone);
28    }
29
30    public function get(): string
31    {
32        return $this->environment;
33    }
34
35    public function is(string $environment): bool
36    {
37        return $this->environment === $environment;
38    }
39
40    public function isProduction(): bool
41    {
42        return $this->is('production');
43    }
44    public function isDevelopment(): bool
45    {
46        return $this->is('development');
47    }
48    public function isTest(): bool
49    {
50        return $this->is('test');
51    }
52
53    public function getRevision(): string
54    {
55        if ($this->isProduction()) {
56            return $this->revision;
57        }
58
59        return (string)time();
60    }
61
62    /**
63     * 環境変数取得。
64     *
65     * `getenv` ラッパー。
66     *
67     * @param non-empty-string $name
68     * @return string|null 環境変数の値。取得できなかった場合に null。
69     * @see https://www.php.net/manual/function.getenv.php
70     */
71    public static function getVariable(string $name): ?string
72    {
73        if (Text::isNullOrWhiteSpace($name)) { //@phpstan-ignore-line [DOCTYPE]
74            throw new ArgumentException($name);
75        }
76
77        $result = getenv($name);
78        if ($result === false) {
79            return null;
80        }
81
82        return $result;
83    }
84
85    /**
86     * 環境変数設定。
87     *
88     * `putenv` ラッパー。
89     *
90     * @param non-empty-string $name
91     * @param string $value
92     * @see https://www.php.net/manual/function.putenv.php
93     */
94    public static function setVariable(string $name, string $value): void
95    {
96        if (Text::isNullOrWhiteSpace($name)) { //@phpstan-ignore-line [DOCTYPE]
97            throw new ArgumentException($name);
98        }
99        if (Text::contains($name, '=', false)) {
100            throw new EnvironmentException("\$name: $name");
101        }
102
103        if (!putenv($name . '=' . $value)) {
104            // 上の対応で多分ここには来ないんじゃないかなぁと思っている
105            throw new EnvironmentException("putenv: $name=$value");
106        }
107    }
108
109    #endregion
110}