Passed
Pull Request — master (#107)
by Simon
02:00
created

Annotatable::afterCallActionHandler()   B

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 8.5906
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
     * @return bool // Return a boolean for test-purposes
45
     * @throws NotFoundExceptionInterface
46
     * @throws ReflectionException
47
     */
48
    public function afterCallActionHandler()
49
    {
50
        $envIsAllowed = Director::isDev() && DataObjectAnnotator::config()->get('enabled');
51
        $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

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