Completed
Push — master ( 5cd7db...4c6168 )
by Sebastien
02:32
created

CerbereConsoleListener::onCerberePostAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4286
cc 1
eloc 6
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 CerbereConsoleListener
12
 * @package Cerbere\Event
13
 */
14
class CerbereConsoleListener 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\Helper\ProgressBar|null $progress
29
     * @param \Symfony\Component\Console\Output\OutputInterface|null $output
30
     */
31
    public function __construct(ProgressBar $progress = null, OutputInterface $output = null)
0 ignored issues
show
Unused Code introduced by
The parameter $progress 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...
32
    {
33
        if (null === $output) {
34
            $output = new ConsoleOutput();
35
        }
36
37
        $this->output = $output;
38
    }
39
40
    /**
41
     * @return OutputInterface
42
     */
43
    public function getOutput()
44
    {
45
        return $this->output;
46
    }
47
48
    /**
49
     * @param OutputInterface $output
50
     */
51
    public function setOutput($output)
52
    {
53
        $this->output = $output;
54
    }
55
56
    /**
57
     * Returns an array of event names this subscriber wants to listen to.
58
     *
59
     * The array keys are event names and the value can be:
60
     *
61
     *  * The method name to call (priority defaults to 0)
62
     *  * An array composed of the method name to call and the priority
63
     *  * An array of arrays composed of the method names to call and respective
64
     *    priorities, or 0 if unset
65
     *
66
     * For instance:
67
     *
68
     *  * array('eventName' => 'methodName')
69
     *  * array('eventName' => array('methodName', $priority))
70
     *  * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
71
     *
72
     * @return array The event names to listen to
73
     */
74
    public static function getSubscribedEvents()
75
    {
76
        return array(
77
          CerbereEvents::CERBERE_FILE_DISCOVERED => array('onCerbereFileDiscovered', 0),
78
          CerbereEvents::CERBERE_PRE_ACTION      => array('onCerberePreAction', 0),
79
          CerbereEvents::CERBERE_DO_ACTION       => array('onCerbereDoAction', 0),
80
          CerbereEvents::CERBERE_POST_ACTION     => array('onCerberePostAction', 0),
81
        );
82
    }
83
84
    /**
85
     * @param \Cerbere\Event\CerbereDoActionEvent $event
86
     */
87
    public function onCerbereDoAction(CerbereDoActionEvent $event)
88
    {
89
        $this->progress->setMessage($event->getProject()->getName(), 'project');
90
        $this->progress->advance();
91
    }
92
93
    /**
94
     * @param \Cerbere\Event\CerbereFileDiscoverEvent $event
95
     */
96
    public function onCerbereFileDiscovered(CerbereFileDiscoverEvent $event)
97
    {
98
        $this->output->getErrorOutput()->writeln($event->getFilename());
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...
99
    }
100
101
    /**
102
     * @param CerberePostActionEvent $event
103
     */
104
    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...
105
    {
106
        $this->progress->setMessage('Action ended');
107
        $this->progress->setMessage('', 'project');
108
        $this->progress->finish();
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
        $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...
113
    }
114
115
    /**
116
     * @param CerberePreActionEvent $event
117
     */
118
    public function onCerberePreAction(CerberePreActionEvent $event)
119
    {
120
        // Returns and jump new line.
121
        $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...
122
123
        $format = " Project: %project%\n";
124
        $format.= ProgressBar::getFormatDefinition('debug');
125
126
        $progress = new ProgressBar($this->output, count($event->getProjects()));
127
        $progress->setFormat($format);
128
        $progress->setRedrawFrequency(1);
129
        $progress->setMessage('Action starts');
130
131
        $this->progress = $progress;
132
    }
133
}
134