Passed
Push — master ( af4fda...29f888 )
by Gabor
40:28
created

ThemeCheckTrait::isWebsiteApplication()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Renderer;
15
16
use WebHemi\Application\EnvironmentManager;
17
use WebHemi\Config\ConfigInterface;
18
19
/**
20
 * Class ThemeCheckTrait
21
 */
22
trait ThemeCheckTrait
23
{
24
    /** @var ConfigInterface */
25
    protected $configuration;
26
    /** @var EnvironmentManager */
27
    protected $environmentManager;
28
    /** @var ConfigInterface */
29
    private $themeConfig;
30
31
    /**
32
     * Checks if the selected theme supports the current state and returns the correct resource path.
33
     *
34
     * @param string $selectedTheme
35
     * @return string
36
     */
37 6
    protected function getSelectedThemeResourcePath(string &$selectedTheme) : string
38
    {
39 6
        $selectedTheme = $this->environmentManager->getSelectedTheme();
40 6
        $selectedThemeResourcePath = $this->environmentManager->getResourcePath();
41
42
        // Reset selected theme, if it's not found.
43 6
        if (!$this->configuration->has('themes/'.$selectedTheme)) {
44 1
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
45
        }
46
47
        // Temporary, only can access by this trait.
48 6
        $this->themeConfig = $this->configuration->getConfig('themes/'.$selectedTheme);
49
50
        // Reset selected theme, if it doesn't support the currenct application/page.
51 6
        if (!$this->checkSelectedThemeFeatures()) {
52 3
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
53 3
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
54
        }
55
56 6
        return $selectedThemeResourcePath;
57
    }
58
59
    /**
60
     * Checks if the selected theme can be used with the current application.
61
     *
62
     * @return bool
63
     */
64 6
    private function checkSelectedThemeFeatures() : bool
65
    {
66 6
        $canUseThisTheme = true;
67
68
        // check the theme settings
69
        // If no theme support for the application, then use the default theme
70 6
        if (($this->isAdminApplication() && !$this->isFeatureSupported('admin')
71
            ) || (// check if admin login page but no admin login support
72 6
                $this->isAdminLoginPage() && !$this->isFeatureSupported('admin_login')
73
            ) || (// check if not admin page but no website support
74 6
                $this->isWebsiteApplication() && !$this->isFeatureSupported('website')
75
            )
76
        ) {
77 3
            $canUseThisTheme = false;
78
        }
79
80 6
        return $canUseThisTheme;
81
    }
82
83
    /**
84
     * Checks whether the current application belongs to the Admin module and the request calls the login page.
85
     *
86
     * @return bool
87
     */
88 6
    private function isAdminLoginPage() : bool
89
    {
90 6
        return strpos($this->environmentManager->getRequestUri(), '/auth/login') !== false;
91
    }
92
93
    /**
94
     * Checks whether the current application belongs to the Admin module or not.
95
     *
96
     * @return bool
97
     */
98 6
    private function isAdminApplication() : bool
99
    {
100 6
        return !$this->isAdminLoginPage() && 'Admin' == $this->environmentManager->getSelectedModule();
101
    }
102
103
    /**
104
     * Checks whether the current application belongs to any Website module application.
105
     *
106
     * @return bool
107
     */
108 6
    private function isWebsiteApplication()
109
    {
110 6
        return 'Website' == $this->environmentManager->getSelectedModule();
111
    }
112
113
    /**
114
     * Checks the config for feature settings.
115
     *
116
     * @param string          $feature
117
     * @return bool
118
     */
119 6
    private function isFeatureSupported(string $feature) : bool
120
    {
121 6
        return $this->themeConfig->has('features/'.$feature.'_support')
122 6
            && (bool) $this->themeConfig->getData('features/'.$feature.'_support')[0];
123
    }
124
}
125