Completed
Push — master ( e4c933...dc074b )
by Robbie
28s queued 11s
created

Annotatable::annotateModules()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 0
dl 0
loc 23
rs 9.6111
c 0
b 0
f 0
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\Extension;
11
use SilverStripe\Core\Injector\Injector;
12
use SilverStripe\Dev\DevBuildController;
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 DevBuildController|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
        $this->annotateModules();
50
    }
51
52
    /**
53
     * Conditionally annotate this project's modules if enabled and not skipped
54
     *
55
     * @return bool Return true if annotation was successful
56
     * @throws NotFoundExceptionInterface
57
     * @throws ReflectionException
58
     */
59
    public function annotateModules(): bool
60
    {
61
        $envIsAllowed = Director::isDev() && DataObjectAnnotator::config()->get('enabled');
62
        $skipAnnotation = $this->owner->getRequest()->getVar('skipannotation');
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on SilverLeague\IDEAnnotator\Extensions\Annotatable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        $skipAnnotation = $this->owner->/** @scrutinizer ignore-call */ getRequest()->getVar('skipannotation');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63
64
        // Only instatiate things when we want to run it, this is for when the module is accidentally installed
65
        // on non-dev environments for example
66
        if ($skipAnnotation === null && $envIsAllowed) {
67
            $this->setUp();
68
69
            $this->displayMessage('Generating class docblocks', true, false);
70
71
            $modules = $this->permissionChecker->enabledModules();
72
            foreach ($modules as $module) {
73
                $this->annotator->annotateModule($module);
74
            }
75
76
            $this->displayMessage('Docblock generation finished!', true, true);
77
78
            return true;
79
        }
80
81
        return false;
82
    }
83
84
    /**
85
     * Annotatable setup.
86
     * This is theoretically a constructor, but to save memory we're using setup
87
     * called from {@see afterCallActionHandler}
88
     * @throws NotFoundExceptionInterface
89
     */
90
    public function setUp()
91
    {
92
        $this->annotator = Injector::inst()->get(DataObjectAnnotator::class);
93
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
94
    }
95
96
    /**
97
     * @param string $message
98
     * @param bool   $heading
99
     * @param bool   $end
100
     */
101
    public function displayMessage($message, $heading = false, $end = false)
102
    {
103
        if ($heading) {
104
            if (!$end) {
105
                echo Director::is_cli() ?
106
                    strtoupper("\n$message\n\n") :
107
                    "<div class='build'><p><b>$message</b><ul>";
108
            } else {
109
                echo Director::is_cli() ? strtoupper("\n" . $message) : "</ul><p><b>$message</b></b></div>";
110
            }
111
        } else {
112
            echo Director::is_cli() ? "\n$message\n\n" : "<li>$message</li>";
113
        }
114
    }
115
116
    /**
117
     * @return DataObjectAnnotator
118
     */
119
    public function getAnnotator()
120
    {
121
        return $this->annotator;
122
    }
123
124
    /**
125
     * @return AnnotatePermissionChecker
126
     */
127
    public function getPermissionChecker()
128
    {
129
        return $this->permissionChecker;
130
    }
131
}
132