Passed
Pull Request — master (#107)
by Simon
01:33
created

Annotatable   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
B afterCallActionHandler() 0 18 5
B displayMessage() 0 12 6
A getPermissionChecker() 0 3 1
A getAnnotator() 0 3 1
A setUp() 0 4 1
1
<?php
2
3
namespace SilverLeague\IDEAnnotator\Extensions;
4
5
use Psr\Container\NotFoundExceptionInterface;
6
use ReflectionException;
7
use SilverLeague\IDEAnnotator\DataObjectAnnotator;
8
use SilverLeague\IDEAnnotator\Helpers\AnnotatePermissionChecker;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Extension;
12
use SilverStripe\Core\Injector\Injector;
13
14
/**
15
 * Class Annotatable
16
 *
17
 * Annotate extension for the provided DataObjects for autocompletion purposes.
18
 * Start annotation, if skipannotation is not set and the annotator is enabled.
19
 *
20
 * @package IDEAnnotator/Extensions
21
 * @property \SilverStripe\Dev\DevBuildController|\SilverLeague\IDEAnnotator\Annotatable $owner
22
 */
23
class Annotatable extends Extension
24
{
25
26
    /**
27
     * Keep track ot the annotation actions for extensions
28
     * An Extension can belong to many DataObjects.
29
     * This prevents that an Extension is ran twice on dev/build
30
     * @var array
31
     */
32
    public static $annotated_extensions = [];
33
    /**
34
     * @var DataObjectAnnotator
35
     */
36
    protected $annotator;
37
    /**
38
     * @var AnnotatePermissionChecker
39
     */
40
    protected $permissionChecker;
41
42
    /**
43
     * Annotated Controllers and Extensions
44
     * @throws NotFoundExceptionInterface
45
     * @throws ReflectionException
46
     */
47
    public function afterCallActionHandler()
48
    {
49
        $envIsAllowed = Director::isDev() && DataObjectAnnotator::config()->get('enabled');
50
        $skipAnnotation = $this->owner->getRequest()->getVar('skipannotation');
51
52
        // Only instatiate things when we want to run it, this is for when the module is accidentally installed
53
        // on non-dev environments for example
54
        if ($skipAnnotation === null && $envIsAllowed) {
55
            $this->setUp();
56
57
            $this->displayMessage("Generating class docblocks", true, false);
58
59
            $modules = $this->permissionChecker->enabledModules();
60
            foreach ($modules as $module) {
61
                $this->annotator->annotateModule($module);
62
            }
63
64
            $this->displayMessage("Docblock generation finished!", true, true);
65
        }
66
    }
67
68
    /**
69
     * Annotatable setup.
70
     * This is theoretically a constructor, but to save memory we're using setup
71
     * called from {@see afterCallActionHandler}
72
     * @throws NotFoundExceptionInterface
73
     */
74
    public function setUp()
75
    {
76
        $this->annotator = Injector::inst()->get(DataObjectAnnotator::class);
77
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
78
    }
79
80
    /**
81
     * @param $message
82
     */
83
    public function displayMessage($message, $heading = false, $end = false)
84
    {
85
        if ($heading) {
86
            if (!$end) {
87
                echo Director::is_cli() ?
88
                    strtoupper("\n" . $message . "\n\n") :
89
                    "<div class='build'><p><b>$message</b><ul>";
90
            } else {
91
                echo Director::is_cli() ? strtoupper("\n" . $message) : "</ul><p><b>$message</b></b></div>";
92
            }
93
        } else {
94
            echo Director::is_cli() ? "\n" . $message . "\n\n" : "<li>$message</li>";
95
        }
96
    }
97
98
    /**
99
     * @return DataObjectAnnotator
100
     */
101
    public function getAnnotator()
102
    {
103
        return $this->annotator;
104
    }
105
106
    /**
107
     * @return AnnotatePermissionChecker
108
     */
109
    public function getPermissionChecker()
110
    {
111
        return $this->permissionChecker;
112
    }
113
}
114