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

DataObjectAnnotatorTask   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A run() 0 17 2
1
<?php
2
3
namespace SilverLeague\IDEAnnotator\Tasks;
4
5
use Psr\Container\NotFoundExceptionInterface;
6
use ReflectionException;
7
use SilverLeague\IDEAnnotator\DataObjectAnnotator;
8
use SilverLeague\IDEAnnotator\Helpers\AnnotatePermissionChecker;
9
use SilverStripe\Control\HTTPRequest;
10
use SilverStripe\Core\Injector\Injector;
11
use SilverStripe\Dev\BuildTask;
12
13
/**
14
 * Class DataObjectAnnotatorTask
15
 *
16
 * Task to add or remove annotations from a module or dataobject.
17
 *
18
 * @package IDEAnnotator/Tasks
19
 */
20
class DataObjectAnnotatorTask extends BuildTask
21
{
22
23
    /**
24
     * DataObjectAnnotatorTask constructor.
25
     * Setup default values. In this case title and description.
26
     */
27
    public function __construct()
28
    {
29
        parent::__construct();
30
        $this->title = 'DataObject annotations for specific DataObjects, Extensions or Controllers';
31
32
        $this->description = 'DataObject Annotator annotates your DO\'s if possible,' .
33
            ' helping you write better code.' .
34
            '<br />Usage: add the module or DataObject as parameter to the URL,' .
35
            ' e.g. ?module=mysite';
36
    }
37
38
    /**
39
     * @param HTTPRequest $request
40
     * @return bool
41
     * @throws ReflectionException
42
     * @throws NotFoundExceptionInterface
43
     */
44
    public function run($request)
45
    {
46
        /* @var $permissionChecker AnnotatePermissionChecker */
47
        $permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
48
49
        if (!$permissionChecker->environmentIsAllowed()) {
50
            return false;
51
        }
52
53
        /* @var $annotator DataObjectAnnotator */
54
        $annotator = DataObjectAnnotator::create();
55
56
        $annotator->annotateObject($request->getVar('object'));
57
58
        $annotator->annotateModule($request->getVar('module'));
59
60
        return true;
61
    }
62
}
63