Passed
Push — master ( 75dbb3...82aa87 )
by Stephen
52s queued 12s
created

AppInfo::isEnvTesting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Sfneal\Helpers\Laravel;
4
5
use ErrorException;
6
use Illuminate\Support\Facades\Cache;
7
use Sfneal\Helpers\Strings\StringHelpers;
8
9
class AppInfo
10
{
11
    /**
12
     * Retrieve the Application's version.
13
     *
14
     * @return string
15
     */
16
    public static function version(): string
17
    {
18
        return Cache::rememberForever(
19
            // Cache key
20
            self::cacheKey('version'),
21
22
            // Value to cache
23
            function () {
24
                return trim(config('app-info.version')).(self::isEnvDevelopment() ? ' (dev)' : '');
25
            }
26
        );
27
    }
28
29
    /**
30
     * Retrieve an array of changes made to a particular application version.
31
     *
32
     * @param string|null $version
33
     * @return array|null
34
     */
35
    public static function versionChanges(string $version = null): ?array
36
    {
37
        // Get current version changes if $version wasn't passed
38
        $version = $version ?? self::version();
39
40
        return Cache::rememberForever(
41
            // Cache key
42
            self::cacheKey('changelog', $version),
43
44
            // Value to cache
45
            function () use ($version) {
46
                try {
47
                    return self::changelog()[$version];
48
                } catch (ErrorException $exception) {
49
                    return null;
50
                }
51
            }
52
        );
53
    }
54
55
    /**
56
     * Retrieve the Application's changelog.
57
     *
58
     * @return array
59
     */
60
    public static function changelog(): array
61
    {
62
        return Cache::rememberForever(self::cacheKey('changelog'), function () {
63
            // Read the changelog
64
            $file_lines = file(config('app-info.changelog_path'));
65
            $changes = [];
66
67
            for ($row = 0; $row < count($file_lines); $row++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
68
                // Check if line starts with 'version'
69
                if (substr(strtolower($file_lines[$row]), 0, strlen('version')) == 'version') {
70
                    $version_date = explode(', ', $file_lines[$row]);
71
72
                    // Extract version and date
73
                    $date = $version_date[1];
74
                    $version = explode(' ', $version_date[0])[1];
75
                    $changes[$version] = ['date' => trim($date), 'changes' => []];
76
77
                    // Skip ahead two lines to skip sep line
78
                    $row += 2;
79
80
                    // Keep iterating over rows until we get to a blank line
81
                    while ($row < count($file_lines) && strlen(trim($file_lines[$row])) > 1) {
82
                        if (substr(trim($file_lines[$row]), 0, 1) == '-') {
83
                            $changes[$version]['changes'][] = str_replace('- ', '', trim($file_lines[$row]));
84
                            $row++;
85
                        } else {
86
                            break;
87
                        }
88
                    }
89
                }
90
            }
91
92
            return $changes;
93
        });
94
    }
95
96
    /**
97
     * Determine if a particular Version is running.
98
     *
99
     * @param string $version
100
     *
101
     * @return mixed
102
     */
103
    public static function isVersion(string $version): bool
104
    {
105
        return self::version() == $version;
106
    }
107
108
    /**
109
     * Determine if a particular Version is running.
110
     *
111
     * @param string $version
112
     *
113
     * @return mixed
114
     */
115
    public static function isNotVersion(string $version): bool
116
    {
117
        return ! self::isVersion($version);
118
    }
119
120
    /**
121
     * Determine if a 'beta' version is running.
122
     *
123
     * @return mixed
124
     */
125
    public static function isVersionTagBeta(): bool
126
    {
127
        return self::isVersionTag('beta');
128
    }
129
130
    /**
131
     * Determine if a 'dev' version is running.
132
     *
133
     * @return mixed
134
     */
135
    public static function isVersionTagDev(): bool
136
    {
137
        return self::isVersionTag('dev');
138
    }
139
140
    /**
141
     * Determine if a particular tag version is running (beta, dev, staging, etc..).
142
     *
143
     * @param string $tag
144
     *
145
     * @return mixed
146
     */
147
    public static function isVersionTag(string $tag): bool
148
    {
149
        return Cache::rememberForever(
150
            // Cache key
151
            self::cacheKey('version', "is-{$tag}"),
152
153
            // Value to cache
154
            function () use ($tag) {
155
                return (new StringHelpers(self::version()))->inString($tag);
156
            }
157
        );
158
    }
159
160
    /**
161
     * Determine if the Application is running in a 'production' environment.
162
     *
163
     * @return bool
164
     */
165
    public static function isEnvProduction(): bool
166
    {
167
        return self::isEnv('production');
168
    }
169
170
    /**
171
     * Determine if the Application is running in a 'development' environment.
172
     *
173
     * @return bool
174
     */
175
    public static function isEnvDevelopment(): bool
176
    {
177
        return self::isEnv('development');
178
    }
179
180
    /**
181
     * Determine if the Application is running in a 'testing' environment.
182
     *
183
     * @return bool
184
     */
185
    public static function isEnvTesting(): bool
186
    {
187
        return self::isEnv('testing');
188
    }
189
190
    /**
191
     * Determine if the application is in a particular environment.
192
     *
193
     * @param string $env
194
     * @return bool
195
     */
196
    public static function isEnv(string $env): bool
197
    {
198
        return self::env() == $env;
199
    }
200
201
    /**
202
     * Retrieve the application's environment.
203
     *
204
     * @return string|null
205
     */
206
    public static function env(): ?string
207
    {
208
        return config('app.env');
209
    }
210
211
    /**
212
     * Retrieve a cache key for a particular service item.
213
     *
214
     * @param string      $item
215
     * @param string|null $identifier
216
     *
217
     * @return string
218
     */
219
    private static function cacheKey(string $item, string $identifier = null): string
220
    {
221
        return config('app-info.cache_prefix').':'.$item.(isset($identifier) ? '#'.$identifier : '');
222
    }
223
}
224