Passed
Push — master ( ad6b2a...b41ca0 )
by Caen
07:45 queued 14s
created

Features::rss()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Facades;
6
7
use Hyde\Hyde;
8
use Illuminate\Support\Str;
9
use Hyde\Pages\MarkdownPost;
10
use Hyde\Pages\DocumentationPage;
11
use Hyde\Enums\Feature;
12
use Hyde\Support\Concerns\Serializable;
13
use Hyde\Support\Contracts\SerializableContract;
14
use Illuminate\Support\Arr;
15
16
use function collect;
17
use function array_filter;
18
use function extension_loaded;
19
use function in_array;
20
use function count;
21
use function app;
22
23
/**
24
 * Allows features to be enabled and disabled in a simple object-oriented manner.
25
 */
26
class Features implements SerializableContract
27
{
28
    use Serializable;
29
30
    /**
31
     * The features that are enabled.
32
     *
33
     * @var array<\Hyde\Enums\Feature>
34
     */
35
    protected array $features = [];
36
37
    public function __construct()
38
    {
39
        $this->features = Config::getArray('hyde.features', Feature::cases());
40
    }
41
42
    /**
43
     * Determine if the given specified is enabled.
44
     */
45
    public static function has(Feature $feature): bool
46
    {
47
        return in_array($feature, Hyde::features()->features);
0 ignored issues
show
Bug introduced by
The method features() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
        return in_array($feature, Hyde::/** @scrutinizer ignore-call */ features()->features);
Loading history...
48
    }
49
50
    /**
51
     * Get all enabled features.
52
     *
53
     * @return array<string>
54
     */
55
    public static function enabled(): array
56
    {
57
        return Arr::map(Hyde::features()->features, fn (Feature $feature): string => Str::kebab($feature->name));
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Hyde\Enums\Feature.
Loading history...
58
    }
59
60
    public static function hasHtmlPages(): bool
61
    {
62
        return static::has(Feature::HtmlPages);
63
    }
64
65
    public static function hasBladePages(): bool
66
    {
67
        return static::has(Feature::BladePages);
68
    }
69
70
    public static function hasMarkdownPages(): bool
71
    {
72
        return static::has(Feature::MarkdownPages);
73
    }
74
75
    public static function hasMarkdownPosts(): bool
76
    {
77
        return static::has(Feature::MarkdownPosts);
78
    }
79
80
    public static function hasDocumentationPages(): bool
81
    {
82
        return static::has(Feature::DocumentationPages);
83
    }
84
85
    public static function hasDarkmode(): bool
86
    {
87
        return static::has(Feature::Darkmode);
88
    }
89
90
    public static function hasThemeToggleButtons(): bool
91
    {
92
        return static::hasDarkmode() && Config::getBool('hyde.theme_toggle_buttons', true);
93
    }
94
95
    /**
96
     * Can a sitemap be generated?
97
     */
98
    public static function hasSitemap(): bool
99
    {
100
        return Hyde::hasSiteUrl()
0 ignored issues
show
Bug introduced by
The method hasSiteUrl() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
        return Hyde::/** @scrutinizer ignore-call */ hasSiteUrl()
Loading history...
101
            && Config::getBool('hyde.generate_sitemap', true)
102
            && extension_loaded('simplexml');
103
    }
104
105
    /**
106
     * Can an RSS feed be generated?
107
     */
108
    public static function hasRss(): bool
109
    {
110
        return Hyde::hasSiteUrl()
111
            && static::hasMarkdownPosts()
112
            && Config::getBool('hyde.rss.enabled', true)
113
            && extension_loaded('simplexml')
114
            && count(MarkdownPost::files()) > 0;
115
    }
116
117
    /**
118
     * Should documentation search be enabled?
119
     */
120
    public static function hasDocumentationSearch(): bool
121
    {
122
        return static::has(Feature::DocumentationSearch)
123
            && static::hasDocumentationPages()
124
            && count(DocumentationPage::files()) > 0;
125
    }
126
127
    /**
128
     * Torchlight is by default enabled automatically when an API token
129
     * is set in the `.env` file but is disabled when running tests.
130
     */
131
    public static function hasTorchlight(): bool
132
    {
133
        return static::has(Feature::Torchlight)
134
            && (Config::getNullableString('torchlight.token') !== null)
135
            && (app('env') !== 'testing');
136
    }
137
138
    /**
139
     * Get an array representation of the features and their status.
140
     *
141
     * @return array<string, bool>
142
     *
143
     * @example ['html-pages' => true, 'markdown-pages' => false, ...]
144
     */
145
    public function toArray(): array
146
    {
147
        return Arr::mapWithKeys(Feature::cases(), fn (Feature $feature): array => [
148
            Str::kebab($feature->name) => static::has($feature),
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Hyde\Enums\Feature.
Loading history...
149
        ]);
150
    }
151
152
    /** @internal This method is not covered by the backward compatibility promise. */
153
    public static function mock(string $feature, ?bool $enabled = null): void
154
    {
155
        if ($enabled === true) {
156
            // Add the feature if it doesn't already exist.
157
            Hyde::features()->features[] = collect(Feature::cases())->firstOrFail(fn (Feature $search): bool => Str::kebab($search->name) === $feature);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Hyde\Enums\Feature.
Loading history...
Bug introduced by
Hyde\Enums\Feature::cases() of type array<integer,Hyde\Enums\Feature> is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
            Hyde::features()->features[] = collect(/** @scrutinizer ignore-type */ Feature::cases())->firstOrFail(fn (Feature $search): bool => Str::kebab($search->name) === $feature);
Loading history...
158
        } else {
159
            // Remove the feature if it exists.
160
            Hyde::features()->features = array_filter(Hyde::features()->features, fn (Feature $search): bool => Str::kebab($search->name) !== $feature);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist on Hyde\Enums\Feature.
Loading history...
161
        }
162
    }
163
}
164