BuilderSubscriber::onBuilderPostBuild()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 6
nc 4
nop 1
crap 12
1
<?php
2
3
/*
4
 * This file is part of the Conveyor package.
5
 *
6
 * (c) Jeroen Fiege <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webcreate\Conveyor\Subscriber;
13
14
use Symfony\Component\EventDispatcher\Event;
15
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16
use Symfony\Component\EventDispatcher\GenericEvent;
17
use Webcreate\Conveyor\Event\BuilderEvents;
18
use Webcreate\Conveyor\IO\IOInterface;
19
20
class BuilderSubscriber implements EventSubscriberInterface
21
{
22
    protected $io;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
23
    protected $showProgress = false;
24
    protected $needsNewline = false;
25
26 1
    public function __construct(IOInterface $io)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
27
    {
28 1
        $this->io = $io;
29 1
    }
30
31 1
    public static function getSubscribedEvents()
32
    {
33
        return array(
34 1
            BuilderEvents::BUILDER_PRE_BUILD  => array('onBuilderPreBuild'),
35
            BuilderEvents::BUILDER_POST_BUILD => array('onBuilderPostBuild'),
36
            BuilderEvents::BUILDER_PRE_TASK   => array('onBuilderPreTask'),
37
            BuilderEvents::BUILDER_POST_TASK  => array('onBuilderPostTask'),
38
        );
39
    }
40
41
    public function onBuilderPreBuild(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
42
    {
43
        if (true === $this->showProgress) {
44
            $this->io->write(sprintf('- Executing tasks'));
45
            $this->io->increaseIndention(2);
46
47
            $this->io->write(sprintf('Progress: <comment>%d%%</comment>', 0), false);
48
        }
49
    }
50
51
    public function onBuilderPostBuild(Event $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
52
    {
53
        if (true === $this->needsNewline) {
54
            $this->io->write('');
55
            $this->needsNewline = false;
56
        }
57
58
        if (true === $this->showProgress) {
59
            $this->io->decreaseIndention(2);
60
        }
61
    }
62
63 View Code Duplication
    public function onBuilderPreTask(GenericEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
64
    {
65
        if (true === $this->showProgress) {
66
            return;
67
        }
68
69
        $task = $event->getSubject();
70
        $io   = $this->io;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $io. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
71
72
        $self = $this;
73
74
        $task->setOutput(function ($output) use ($io, $self) {
75
            if ($io->isVerbose()) {
76
                $io->write(sprintf('%s', $output));
77
                $self->needsNewline = false;
78
            } else {
79
                $io->overwrite(sprintf('%s', $output), false);
80
                $self->needsNewline = true;
81
            }
82
        });
83
84
        if ($event->getArgument('index') > 0) {
85
            if (true === $this->needsNewline) {
86
                $this->io->write('');
87
                $this->needsNewline = false;
88
            }
89
            $this->io->write('');
90
        }
91
92
        $this->io->write(sprintf('- Executing task <info>%s</info>', get_class($task)));
93
        $this->io->increaseIndention(2);
94
    }
95
96
    public function onBuilderPostTask(GenericEvent $event)
97
    {
98
        if (true === $this->showProgress) {
99
            $index = $event->getArgument('index');
100
            $total = $event->getArgument('total');
101
            $percentage = (++$index * 100 / $total);
102
103
            $this->io->overwrite(sprintf('Progress: <comment>%d%%</comment>', $percentage), false);
104
105
            $this->needsNewline = true;
106
        } else {
107
            $this->io->decreaseIndention(2);
108
        }
109
    }
110
}
111