Issues (10)

src/Api/LeftAndMainDarkThemeApi.php (2 issues)

1
<?php
2
3
namespace Sunnysideup\CMSDarkTheme\Api;
4
5
use SilverStripe\Security\Security;
6
use SilverStripe\SiteConfig\SiteConfig;
0 ignored issues
show
The type SilverStripe\SiteConfig\SiteConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
class LeftAndMainDarkThemeApi
9
{
10
    /**
11
     * Dar or Light or null.
12
     *
13
     * @var null|string
14
     */
15
    protected static $darkModeValueCache;
16
17
    /**
18
     * has any value been set in the database?
19
     *
20
     * @var bool
21
     */
22
    protected static $darkModeSetInDatabaseCache = false;
23
24
    public static function get_display_mode_menu_title(): string
25
    {
26
        return self::is_dark_mode() ? '🌖 Use Light Mode' : '🌘 Use Dark Mode';
27
    }
28
29
    public static function is_dark_mode(): bool
30
    {
31
        return 'Dark' === self::get_display_mode();
32
    }
33
34
    public static function set_display_mode(string $mode): void
35
    {
36
        if (! self::is_valid_display_mode_setting($mode)) {
37
            user_error('Setting must be Dark or Light');
38
        }
39
        $member = Security::getCurrentUser();
40
        if ($member) {
0 ignored issues
show
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
41
            $member->DarkModeSetting = $mode;
42
            $member->write();
43
        } else {
44
            user_error('Could not set mode.');
45
        }
46
    }
47
48
    public static function get_display_mode(): string
49
    {
50
        if (null === self::$darkModeValueCache) {
51
            $member = Security::getCurrentUser();
52
            if (self::is_valid_display_mode_setting((string) $member->DarkModeSetting)) {
53
                self::$darkModeSetInDatabaseCache = true;
54
                self::$darkModeValueCache = (string) $member->DarkModeSetting;
55
            }
56
            $config = SiteConfig::current_site_config();
57
            if (self::is_valid_display_mode_setting((string) $config->DarkModeSetting)) {
58
                self::$darkModeSetInDatabaseCache = true;
59
                if (! self::$darkModeValueCache) {
60
                    self::$darkModeValueCache = (string) $config->DarkModeSetting;
61
                }
62
            }
63
            if (! self::is_valid_display_mode_setting((string) self::$darkModeValueCache)) {
64
                self::$darkModeValueCache = '';
65
            }
66
        }
67
68
        return (string) self::$darkModeValueCache;
69
    }
70
71
    /**
72
     * has any relevant value been set in the database?
73
     *
74
     * @var bool
75
     */
76
    public static function get_is_dark_mode_set_in_database(): bool
77
    {
78
        return self::$darkModeSetInDatabaseCache;
79
    }
80
81
    public static function is_valid_display_mode_setting(string $mode): bool
82
    {
83
        return in_array($mode, ['Dark', 'Light'], true);
84
    }
85
}
86