Builder   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 13

Importance

Changes 0
Metric Value
wmc 15
cbo 13
dl 0
loc 143
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A runJobs() 0 10 2
B runJob() 0 50 6
B prepareServiceForTask() 0 26 6
1
<?php
2
3
/**
4
 * This file is part of Bldr.io
5
 *
6
 * (c) Aaron Scherer <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE
10
 */
11
12
namespace Bldr\Block\Core\Service;
13
14
use Bldr\Block\Core\Task\AbstractTask;
15
use Bldr\Definition\JobDefinition;
16
use Bldr\Definition\TaskDefinition;
17
use Bldr\Event;
18
use Bldr\Event\PreTaskEvent;
19
use Bldr\Exception\TaskRuntimeException;
20
use Bldr\Registry\JobRegistry;
21
use Bldr\Registry\TaskRegistry;
22
use Bldr\Task\TaskInterface;
23
use Symfony\Component\Console\Helper\HelperSet;
24
use Symfony\Component\Console\Output\OutputInterface;
25
use Symfony\Component\Console\Helper\FormatterHelper;
26
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
27
28
/**
29
 * @author Aaron Scherer <[email protected]>
30
 */
31
class Builder
32
{
33
    /**
34
     * @var EventDispatcherInterface $dispatcher
35
     */
36
    private $dispatcher;
37
38
    /**
39
     * @var OutputInterface $output
40
     */
41
    private $output;
42
43
    /**
44
     * @type HelperSet
45
     */
46
    private $helperSet;
47
48
    /**
49
     * @type TaskRegistry $taskRegistry
50
     */
51
    private $taskRegistry;
52
53
    /**
54
     * @param EventDispatcherInterface $dispatcher
55
     * @param OutputInterface          $output
56
     * @param HelperSet                $helperSet
57
     * @param TaskRegistry             $taskRegistry
58
     */
59
    public function __construct(
60
        EventDispatcherInterface $dispatcher,
61
        OutputInterface $output,
62
        HelperSet $helperSet,
63
        TaskRegistry $taskRegistry
64
    ) {
65
        $this->dispatcher   = $dispatcher;
66
        $this->output       = $output;
67
        $this->helperSet    = $helperSet;
68
        $this->taskRegistry = $taskRegistry;
69
    }
70
71
    /**
72
     * @param JobRegistry $registry
73
     */
74
    public function runJobs(JobRegistry $registry)
75
    {
76
        while ($registry->count() > 0) {
77
            $job = $registry->getNewJob();
78
79
            $this->runJob($job);
0 ignored issues
show
Security Bug introduced by
It seems like $job defined by $registry->getNewJob() on line 77 can also be of type false; however, Bldr\Block\Core\Service\Builder::runJob() does only seem to accept object<Bldr\Definition\JobDefinition>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
80
81
            $registry->markJobComplete($job);
0 ignored issues
show
Unused Code introduced by
The call to JobRegistry::markJobComplete() has too many arguments starting with $job.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
82
        }
83
    }
84
85
    /**
86
     * @param JobDefinition $job
87
     *
88
     * @throws \Exception
89
     */
90
    public function runJob(JobDefinition $job)
91
    {
92
        $this->output->writeln(
93
            [
94
                "\n",
95
                sprintf(
96
                    '<info>Running the %s job</info><comment>%s</comment>',
97
                    $job->getName(),
98
                    $job->getDescription() !== '' ? " > ".$job->getDescription() : ''
99
                )
100
            ]
101
        );
102
103
        foreach ($job->getTasks() as $task) {
104
            $preTaskEvent = new PreTaskEvent($job, $task);
105
            $this->dispatcher->dispatch(Event::PRE_TASK, $preTaskEvent);
106
107
            if ($preTaskEvent->isPropagationStopped()) {
108
                continue;
109
            }
110
111
            $service = $this->taskRegistry->findTaskByType($task->getType());
112
            $this->prepareServiceForTask($service, $task);
113
114
            try {
115
                $service->run($this->output);
116
            } catch (TaskRuntimeException $e) {
117
                if (!$task->continueOnError()) {
118
                    throw $e;
119
                }
120
121
                /** @var FormatterHelper $formatter */
122
                $formatter = $this->helperSet->get('formatter');
123
                $this->output->writeln(
124
                    [
125
                        '',
126
                        $formatter->formatBlock(
127
                            [
128
                                sprintf('[Warning] Task: %s', $task->getType()),
129
                                sprintf('%s', $e->getMessage())
130
                            ],
131
                            'bg=yellow;fg=black',
132
                            true
133
                        ),
134
                        ''
135
                    ]
136
                );
137
            }
138
        }
139
    }
140
141
    /**
142
     * @param TaskInterface  $service
143
     * @param TaskDefinition $task
144
     *
145
     * @return TaskInterface
146
     */
147
    private function prepareServiceForTask(TaskInterface $service, TaskDefinition $task)
148
    {
149
        $this->dispatcher->dispatch(Event::PRE_INITIALIZE_TASK, new Event\PreInitializeTaskEvent($service));
150
151
        if ($service instanceof AbstractTask) {
152
            $service->configure();
153
        }
154
155
        if (method_exists($service, 'setEventDispatcher')) {
156
            $service->setEventDispatcher($this->dispatcher);
157
        }
158
159
        if (method_exists($service, 'setHelperSet')) {
160
            $service->setHelperSet($this->helperSet);
161
        }
162
163
        foreach ($task->getParameters() as $name => $value) {
164
            $service->setParameter($name, $value);
165
        }
166
167
        if (method_exists($service, 'validate')) {
168
            $service->validate();
169
        }
170
171
        return $service;
172
    }
173
}
174