Completed
Push — master ( e9ab60...4fa3cf )
by Sebastien
15:37 queued 05:47
created

CerbereProgressBarListener::setOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Cerbere\Event;
4
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Symfony\Component\Console\Output\ConsoleOutput;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
/**
11
 * Class CerbereProgressBarListener
12
 * @package Cerbere\Event
13
 */
14
class CerbereProgressBarListener implements EventSubscriberInterface
15
{
16
    /**
17
     * @var OutputInterface
18
     */
19
    protected $output;
20
21
    /**
22
     * @var ProgressBar
23
     */
24
    protected $progress;
25
26
    /**
27
     * CerbereConsoleListener constructor.
28
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
29
     */
30 View Code Duplication
    public function __construct(OutputInterface $output = null)
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...
31
    {
32
        if (null === $output) {
33
            $output = new ConsoleOutput();
34
        }
35
36
        $this->output = $output;
37
    }
38
39
    /**
40
     * @return OutputInterface
41
     */
42
    public function getOutput()
43
    {
44
        return $this->output;
45
    }
46
47
    /**
48
     * @param OutputInterface $output
49
     */
50
    public function setOutput($output)
51
    {
52
        $this->output = $output;
53
    }
54
55
    /**
56
     * Returns an array of event names this subscriber wants to listen to.
57
     *
58
     * The array keys are event names and the value can be:
59
     *
60
     *  * The method name to call (priority defaults to 0)
61
     *  * An array composed of the method name to call and the priority
62
     *  * An array of arrays composed of the method names to call and respective
63
     *    priorities, or 0 if unset
64
     *
65
     * For instance:
66
     *
67
     *  * array('eventName' => 'methodName')
68
     *  * array('eventName' => array('methodName', $priority))
69
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
70
     *
71
     * @return array The event names to listen to
72
     */
73
    public static function getSubscribedEvents()
74
    {
75
        return array(
76
          CerbereEvents::CERBERE_PRE_ACTION      => array('onCerberePreAction', 0),
77
          CerbereEvents::CERBERE_DO_ACTION       => array('onCerbereDoAction', 0),
78
          CerbereEvents::CERBERE_POST_ACTION     => array('onCerberePostAction', 0),
79
        );
80
    }
81
82
    /**
83
     * @param \Cerbere\Event\CerbereDoActionEvent $event
84
     */
85
    public function onCerbereDoAction(CerbereDoActionEvent $event)
86
    {
87
        $this->progress->setMessage($event->getProject()->getName(), 'project');
88
        $this->progress->advance();
89
    }
90
91
    /**
92
     * @param CerberePostActionEvent $event
93
     */
94
    public function onCerberePostAction(CerberePostActionEvent $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...
95
    {
96
        $this->progress->setMessage('Action ended');
97
        $this->progress->setMessage('', 'project');
98
        $this->progress->finish();
99
100
        // Returns and jump new line.
101
        $this->output->getErrorOutput()->writeln('');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method getErrorOutput() does only exist in the following implementations of said interface: Psy\Output\ShellOutput, Symfony\Component\Console\Output\ConsoleOutput.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
102
        $this->output->getErrorOutput()->writeln('');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method getErrorOutput() does only exist in the following implementations of said interface: Psy\Output\ShellOutput, Symfony\Component\Console\Output\ConsoleOutput.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
103
    }
104
105
    /**
106
     * @param CerberePreActionEvent $event
107
     */
108
    public function onCerberePreAction(CerberePreActionEvent $event)
109
    {
110
        // Returns and jump new line.
111
        $this->output->getErrorOutput()->writeln('');
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Output\OutputInterface as the method getErrorOutput() does only exist in the following implementations of said interface: Psy\Output\ShellOutput, Symfony\Component\Console\Output\ConsoleOutput.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
112
113
        $format = " Project: %project%\n";
114
        $format .= ProgressBar::getFormatDefinition('debug');
115
116
        $progress = new ProgressBar($this->output, count($event->getProjects()));
117
        $progress->setFormat($format);
118
        $progress->setRedrawFrequency(1);
119
        $progress->setMessage('Action starts');
120
121
        $this->progress = $progress;
122
    }
123
}
124