Completed
Pull Request — master (#107)
by Simon
01:50
created

Annotatable::afterCallActionHandler()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 6
nop 0
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SilverLeague\IDEAnnotator;
4
5
use Psr\Container\NotFoundExceptionInterface;
6
use ReflectionException;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Core\Config\Config;
9
use SilverStripe\Core\Extension;
10
use SilverStripe\Core\Injector\Injector;
11
12
/**
13
 * Class Annotatable
14
 *
15
 * Annotate extension for the provided DataObjects for autocompletion purposes.
16
 * Start annotation, if skipannotation is not set and the annotator is enabled.
17
 *
18
 * @package IDEAnnotator/Extensions
19
 * @property \SilverStripe\Dev\DevBuildController|\SilverLeague\IDEAnnotator\Annotatable $owner
20
 */
21
class Annotatable extends Extension
22
{
23
24
    /**
25
     * Keep track ot the annotation actions for extensions
26
     * An Extension can belong to many DataObjects.
27
     * This prevents that an Extension is ran twice on dev/build
28
     * @var array
29
     */
30
    public static $annotated_extensions = [];
31
    /**
32
     * @var DataObjectAnnotator
33
     */
34
    protected $annotator;
35
    /**
36
     * @var AnnotatePermissionChecker
37
     */
38
    protected $permissionChecker;
39
40
    /**
41
     * Annotated Controllers and Extensions
42
     * @throws NotFoundExceptionInterface
43
     * @throws ReflectionException
44
     */
45
    public function afterCallActionHandler()
46
    {
47
        $envIsAllowed = Director::isDev() && Config::inst()->get(DataObjectAnnotator::class, 'enabled');
48
        $skipAnnotation = $this->owner->getRequest()->getVar('skipannotation');
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on SilverLeague\IDEAnnotator\Annotatable. ( Ignorable by Annotation )

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

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