Test Failed
Push — master ( 3f0509...af4fda )
by Gabor
04:12
created

ThemeCheckTrait   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 1
dl 0
loc 98
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSelectedThemeResourcePath() 0 15 3
B checkSelectedThemeFeatures() 0 18 7
A isAdminLoginPage() 0 4 1
A isAdminApplication() 0 4 2
A isWebsiteApplication() 0 4 1
A isFeatureSupported() 0 5 2
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 6
     * @return string
36
     */
37 6
    protected function getSelectedThemeResourcePath(string &$selectedTheme) : string
38 6
    {
39
        $selectedTheme = $this->environmentManager->getSelectedTheme();
40 6
        $selectedThemeResourcePath = $this->environmentManager->getResourcePath();
41 6
42 6
        // Temporary, only can access by this trait.
43 6
        $this->themeConfig = $this->configuration->getConfig('themes/'.$selectedTheme);
44
45
        if (!$this->configuration->has('themes/'.$selectedTheme) || !$this->checkSelectedThemeFeatures()) {
46 4
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
47 4
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
48
        }
49
50 6
        return $selectedThemeResourcePath;
51
    }
52
53
    /**
54
     * Checks if the selected theme can be used with the current application.
55
     *
56
     * @return bool
57
     */
58
    private function checkSelectedThemeFeatures() : bool
59
    {
60 6
        $canUseThisTheme = true;
61
62
        // check the theme settings
63
        // If no theme support for the application, then use the default theme
64 6
        if (($this->isAdminApplication() && !$this->isFeatureSupported($this->themeConfig, 'admin')
65
            ) || (// check if admin login page but no admin login support
66
                $this->isAdminLoginPage() && !$this->isFeatureSupported($this->themeConfig, 'admin_login')
67
            ) || (// check if not admin page but no website support
68 6
                $this->isWebsiteApplication() && !$this->isFeatureSupported($this->themeConfig, 'website')
69
            )
70 6
        ) {
71 6
            $canUseThisTheme = false;
72
        }
73 6
74 6
        return $canUseThisTheme;
75
    }
76 6
77 6
    /**
78
     * Checks whether the current application belongs to the Admin module and the request calls the login page.
79
     *
80 3
     * @return bool
81
     */
82
    private function isAdminLoginPage() : bool
83 6
    {
84
        return strpos($this->environmentManager->getRequestUri(), '/auth/login') !== false;
85
    }
86
87
    /**
88
     * Checks whether the current application belongs to the Admin module or not.
89
     *
90
     * @return bool
91
     */
92
    private function isAdminApplication() : bool
93 6
    {
94
        return !$this->isAdminLoginPage() && 'Admin' == $this->environmentManager->getSelectedModule();
95 6
    }
96 6
97
    /**
98 6
     * Checks whether the current application belongs to any Website module application.
99
     *
100
     * @return bool
101
     */
102
    private function isWebsiteApplication()
103
    {
104
        return 'Website' == $this->environmentManager->getSelectedModule();
105
    }
106
107
    /**
108 6
     * Checks the config for feature settings.
109
     *
110 6
     * @param ConfigInterface $themeConfig
111 6
     * @param string          $feature
112
     * @return bool
113
     */
114
    private function isFeatureSupported(ConfigInterface $themeConfig, string $feature) : bool
115
    {
116
        return $themeConfig->has('features/'.$feature.'_support')
117
            && (bool) $themeConfig->getData('features/'.$feature.'_support')[0];
118
    }
119
}
120