Passed
Push — master ( 0f66d0...a5c111 )
by Gabor
16:59 queued 08:35
created

isFeatureSupported()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
crap 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 GetSelectedThemeResourcePathTrait
21
 */
22
trait GetSelectedThemeResourcePathTrait
23
{
24
    /** @var EnvironmentManager */
25
    private $environment;
26
    /** @var ConfigInterface */
27
    private $themeConfig;
28
29
    /**
30
     * Checks if the selected theme supports the current state and returns the correct resource path.
31
     *
32
     * @param string $selectedTheme
33
     * @param ConfigInterface $configuration
34
     * @param EnvironmentManager $environmentManager
35
     * @return string
36
     */
37 10
    protected function getSelectedThemeResourcePath(
38
        string &$selectedTheme,
39
        ConfigInterface $configuration,
40
        EnvironmentManager $environmentManager
41
    ) : string {
42 10
        $this->environment = $environmentManager;
43
44
        //$selectedTheme = $this->environment->getSelectedTheme();
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
45 10
        $selectedThemeResourcePath = $this->environment->getResourcePath();
46
47
        // Reset selected theme, if it's not found.
48 10
        if (!$configuration->has('themes/'.$selectedTheme)) {
49 2
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
50 2
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
51
        }
52
53
        // Temporary, only can access by this trait.
54 10
        $this->themeConfig = $configuration->getConfig('themes/'.$selectedTheme);
55
56
        // Reset selected theme, if it doesn't support the currenct application/page.
57 10
        if (!$this->checkSelectedThemeFeatures()) {
58 4
            $selectedTheme = EnvironmentManager::DEFAULT_THEME;
59 4
            $selectedThemeResourcePath = EnvironmentManager::DEFAULT_THEME_RESOURCE_PATH;
60
        }
61
62 10
        return $selectedThemeResourcePath;
63
    }
64
65
    /**
66
     * Checks if the selected theme can be used with the current application.
67
     *
68
     * @return bool
69
     */
70 10
    private function checkSelectedThemeFeatures() : bool
71
    {
72 10
        $canUseThisTheme = true;
73
74
        // check the theme settings
75
        // If no theme support for the application, then use the default theme
76 10
        if (($this->isAdminApplication() && !$this->isFeatureSupported('admin')
77
            ) || (// check if admin login page but no admin login support
78 10
                $this->isAdminLoginPage() && !$this->isFeatureSupported('admin_login')
79
            ) || (// check if not admin page but no website support
80 10
                $this->isWebsiteApplication() && !$this->isFeatureSupported('website')
81
            )
82
        ) {
83 4
            $canUseThisTheme = false;
84
        }
85
86 10
        return $canUseThisTheme;
87
    }
88
89
    /**
90
     * Checks whether the current application belongs to the Admin module and the request calls the login page.
91
     *
92
     * @return bool
93
     */
94 10
    private function isAdminLoginPage() : bool
95
    {
96 10
        return strpos($this->environment->getRequestUri(), '/auth/login') !== false;
97
    }
98
99
    /**
100
     * Checks whether the current application belongs to the Admin module or not.
101
     *
102
     * @return bool
103
     */
104 10
    private function isAdminApplication() : bool
105
    {
106 10
        return !$this->isAdminLoginPage() && 'Admin' == $this->environment->getSelectedModule();
107
    }
108
109
    /**
110
     * Checks whether the current application belongs to any Website module application.
111
     *
112
     * @return bool
113
     */
114 10
    private function isWebsiteApplication()
115
    {
116 10
        return 'Website' == $this->environment->getSelectedModule();
117
    }
118
119
    /**
120
     * Checks the config for feature settings.
121
     *
122
     * @param string          $feature
123
     * @return bool
124
     */
125 10
    private function isFeatureSupported(string $feature) : bool
126
    {
127 10
        return $this->themeConfig->has('features/'.$feature.'_support')
128 10
            && (bool) $this->themeConfig->getData('features/'.$feature.'_support')[0];
129
    }
130
}
131