Passed
Branch master (608a5d)
by Simon
01:54
created

AnnotatePermissionChecker::classNameIsAllowed()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 9
nc 3
nop 1
dl 0
loc 17
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace SilverLeague\IDEAnnotator;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Extension;
9
use SilverStripe\ORM\DataExtension;
10
use SilverStripe\ORM\DataObject;
11
12
/**
13
 * Class AnnotatePermissionChecker
14
 *
15
 * Helperclass to check if the current environment, class or module is allowed to be annotated.
16
 * This is abstracted from @see DataObjectAnnotator to separate and clean up.
17
 *
18
 * @package IDEAnnotator/Helpers
19
 */
20
class AnnotatePermissionChecker
21
{
22
23
    /**
24
     * In the future we will support other Classes as well.
25
     * We list the core classes, but in fact only it's subclasses are supported
26
     * @see AnnotatePermissionChecker::classNameIsSupported();
27
     */
28
    protected $supportedParentClasses = [
29
        DataObject::class,
30
        DataExtension::class,
31
        Controller::class,
32
        Extension::class
33
    ];
34
35
    /**
36
     * @return bool
37
     */
38
    public function environmentIsAllowed()
39
    {
40
        if (!$this->isEnabled()) {
41
            return false;
42
        }
43
44
        return Director::isDev();
45
    }
46
47
    /**
48
     * @return bool
49
     */
50
    public function isEnabled()
51
    {
52
        return (bool)Config::inst()->get(DataObjectAnnotator::class, 'enabled');
53
    }
54
55
    /**
56
     * @return array
57
     */
58
    public function getSupportedParentClasses()
59
    {
60
        return $this->supportedParentClasses;
61
    }
62
63
    /**
64
     * Check if a DataObject or DataExtension subclass is allowed by checking if the file
65
     * is in the $allowed_modules array
66
     * The permission is checked by matching the filePath and modulePath
67
     *
68
     * @param $className
69
     *
70
     * @return bool
71
     * @throws \ReflectionException
72
     */
73
    public function classNameIsAllowed($className)
74
    {
75
        if ($this->classNameIsSupported($className)) {
76
            $classInfo = new AnnotateClassInfo($className);
77
            $filePath = $classInfo->getClassFilePath();
78
79
            $allowedModules = (array)Config::inst()->get(DataObjectAnnotator::class, 'enabled_modules');
80
81
            foreach ($allowedModules as $moduleName) {
82
                $modulePath = BASE_PATH . DIRECTORY_SEPARATOR . $moduleName;
83
                if (false !== strpos($filePath, $modulePath)) {
84
                    return true;
85
                }
86
            }
87
        }
88
89
        return false;
90
    }
91
92
    /**
93
     * Check if a (subclass of ) class is a supported
94
     *
95
     * @param $className
96
     * @return bool
97
     */
98
    public function classNameIsSupported($className)
99
    {
100
        foreach ($this->supportedParentClasses as $supportedParent) {
101
            if (is_subclass_of($className, $supportedParent)) {
102
                return true;
103
            }
104
        }
105
106
        return false;
107
    }
108
109
    /**
110
     * Check if a module is in the $allowed_modules array
111
     * Required for the buildTask.
112
     *
113
     * @param string $moduleName
114
     *
115
     * @return bool
116
     */
117
    public function moduleIsAllowed($moduleName)
118
    {
119
        return in_array($moduleName, $this->enabledModules(), null);
120
    }
121
122
    /**
123
     * @return array
124
     */
125
    public function enabledModules()
126
    {
127
        $enabled = (array)Config::inst()->get(DataObjectAnnotator::class, 'enabled_modules');
128
129
        // modules might be enabled more then once.
130
        return array_combine($enabled, $enabled);
131
    }
132
}
133