Passed
Push — master ( 86c3d2...1ebd3c )
by Gabor
07:26
created

ThemeCheckTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 0
loc 63
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
D checkSelectedThemeFeatures() 0 25 9
A isAdminApplication() 0 7 4
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
    /**
25
     * Checks if the selected theme can be used with the current application.
26
     *
27
     * @param ConfigInterface    $themeConfig
28
     * @param EnvironmentManager $environmentManager
29
     * @return bool
30
     */
31 6
    protected function checkSelectedThemeFeatures(
32
        ConfigInterface $themeConfig,
33
        EnvironmentManager $environmentManager
34
    ) : bool {
35 6
        $canUseThisTheme = true;
36
37
        // check the theme settings
38
        // If no theme support for the application, then use the default theme
39 6
        if (($this->isAdminApplication($environmentManager, false) && !$this->isFeatureSupported($themeConfig, 'admin'))
40
            || (// check if admin login page but no admin login support
41 6
                $this->isAdminApplication($environmentManager, true)
42 6
                && !$this->isFeatureSupported($themeConfig, 'admin_login')
43
            ) || (// check if not admin page but no website support
44 6
                !$this->isAdminApplication($environmentManager, false)
45 6
                && !$this->isFeatureSupported($themeConfig, 'website')
46
            ) || (// check if not admin login page but no website login support
47 6
                !$this->isAdminApplication($environmentManager, true)
48 6
                && !$this->isFeatureSupported($themeConfig, 'website')
49
            )
50
        ) {
51 3
            $canUseThisTheme = false;
52
        }
53
54 6
        return $canUseThisTheme;
55
    }
56
57
    /**
58
     * Checks whether the current application is the Admin(login) or not.
59
     *
60
     * @param EnvironmentManager $environmentManager
61
     * @param bool               $checkIfLogin
62
     * @return bool
63
     */
64 6
    protected function isAdminApplication(EnvironmentManager $environmentManager, bool $checkIfLogin = false) : bool
65
    {
66 6
        $isAdmin = 'admin' == $environmentManager->getSelectedApplication();
67 6
        $isLogin = strpos($environmentManager->getRequestUri(), '/auth/login') !== false;
68
69 6
        return $checkIfLogin ? $isAdmin && $isLogin : $isAdmin && !$isLogin;
70
    }
71
72
    /**
73
     * Checks the config for feature settings.
74
     *
75
     * @param ConfigInterface $themeConfig
76
     * @param string          $feature
77
     * @return bool
78
     */
79 6
    protected function isFeatureSupported(ConfigInterface $themeConfig, string $feature) : bool
80
    {
81 6
        return $themeConfig->has('features/'.$feature.'_support')
82 6
            && (bool) $themeConfig->getData('features/'.$feature.'_support')[0];
83
    }
84
}
85