Passed
Push — master ( 1ebd3c...3f0509 )
by Gabor
04:18
created

ThemeCheckTrait::getSelectedThemeResourcePath()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 1
crap 3
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
29
    /**
30
     * Checks if the selected theme supports the current state and returns the correct resource path.
31
     *
32
     * @param string $selectedTheme
33
     * @return string
34
     */
35 6
    protected function getSelectedThemeResourcePath(string &$selectedTheme) : string
36
    {
37 6
        $selectedTheme = $this->environmentManager->getSelectedTheme();
38 6
        $selectedThemeResourcePath = $this->environmentManager->getResourcePath();
39
40 6
        if (!$this->configuration->has('themes/'.$selectedTheme)
41 6
            || !$this->checkSelectedThemeFeatures(
42 6
                $this->configuration->getConfig('themes/'.$selectedTheme),
43 6
                $this->environmentManager
44
            )
45
        ) {
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
     * @param ConfigInterface    $themeConfig
57
     * @param EnvironmentManager $environmentManager
58
     * @return bool
59
     */
60 6
    protected function checkSelectedThemeFeatures(
61
        ConfigInterface $themeConfig,
62
        EnvironmentManager $environmentManager
63
    ) : bool {
64 6
        $canUseThisTheme = true;
65
66
        // check the theme settings
67
        // If no theme support for the application, then use the default theme
68 6
        if (($this->isAdminApplication($environmentManager, false) && !$this->isFeatureSupported($themeConfig, 'admin'))
69
            || (// check if admin login page but no admin login support
70 6
                $this->isAdminApplication($environmentManager, true)
71 6
                && !$this->isFeatureSupported($themeConfig, 'admin_login')
72
            ) || (// check if not admin page but no website support
73 6
                !$this->isAdminApplication($environmentManager, false)
74 6
                && !$this->isFeatureSupported($themeConfig, 'website')
75
            ) || (// check if not admin login page but no website login support
76 6
                !$this->isAdminApplication($environmentManager, true)
77 6
                && !$this->isFeatureSupported($themeConfig, 'website')
78
            )
79
        ) {
80 3
            $canUseThisTheme = false;
81
        }
82
83 6
        return $canUseThisTheme;
84
    }
85
86
    /**
87
     * Checks whether the current application is the Admin(login) or not.
88
     *
89
     * @param EnvironmentManager $environmentManager
90
     * @param bool               $checkIfLogin
91
     * @return bool
92
     */
93 6
    protected function isAdminApplication(EnvironmentManager $environmentManager, bool $checkIfLogin = false) : bool
94
    {
95 6
        $isAdmin = 'admin' == $environmentManager->getSelectedApplication();
96 6
        $isLogin = strpos($environmentManager->getRequestUri(), '/auth/login') !== false;
97
98 6
        return $checkIfLogin ? $isAdmin && $isLogin : $isAdmin && !$isLogin;
99
    }
100
101
    /**
102
     * Checks the config for feature settings.
103
     *
104
     * @param ConfigInterface $themeConfig
105
     * @param string          $feature
106
     * @return bool
107
     */
108 6
    protected function isFeatureSupported(ConfigInterface $themeConfig, string $feature) : bool
109
    {
110 6
        return $themeConfig->has('features/'.$feature.'_support')
111 6
            && (bool) $themeConfig->getData('features/'.$feature.'_support')[0];
112
    }
113
}
114