AbstractConfigurableCommand::notify()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 12
nc 6
nop 2
1
<?php namespace Tequilarapido\Cli\Commands\Base;
2
3
4
use Symfony\Component\Console\Input\InputArgument;
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Tequilarapido\Cli\Commands\Base\AbstractCommand;
8
use Tequilarapido\Cli\Config\Config;
9
use Tequilarapido\Helpers\MailHelper;
10
11
/**
12
 *
13
 * All commands that inherit from this class, will take json configuration
14
 * file as first argument. And configuration will be accessible from $config variable.
15
 *
16
 * Class AbstractConfigurableCommand
17
 * @package Tequilarapido\Cli\Commands
18
 */
19
abstract class AbstractConfigurableCommand extends AbstractCommand
20
{
21
    /**
22
     * @var \Tequilarapido\Cli\Config\Config
23
     */
24
    protected $config = null;
25
26
    public function __construct($name = null)
27
    {
28
        // Check that we have a schema file defined
29
        if (!defined('CLI_SCHEMA_FILE')) {
30
            throw new \LogicException('You must define a CLI_SCHEMA_FILE constant');
31
        }
32
33
        parent::__construct($name);
34
    }
35
36
37
    /**
38
     * All commands take as first argument path to conf file
39
     */
40
    protected function configure()
41
    {
42
        $this->addArgument(
43
            'config-file',
44
            InputArgument::REQUIRED,
45
            'Configuration file path',
46
            null
47
        );
48
    }
49
50
    protected function execute(InputInterface $input, OutputInterface $output)
51
    {
52
        parent::execute($input, $output);
53
54
        // Elapsed time ?
55
        $this->elapsed();
56
57
        // Config file
58
        $configFile = $this->input->getArgument('config-file');
59
        $cliFile = $this->getFileFullPath($configFile);
60
        if (!$cliFile) {
61
            throw new \LogicException("Cannot find config file. (Given : [$configFile])");
62
        }
63
64
        // Load file
65
        $this->config = new Config($configFile, CLI_SCHEMA_FILE);
66
        if (!$this->config->load()) {
67
            $this->output->error('Please fix errors in your configuration file.');
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 error() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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...
68
        }
69
    }
70
71
72
    protected function getFileFullPath($file)
73
    {
74
        // Let's try with nothing : full path ?
75
        if (is_file($file)) {
76
            return $file;
77
        }
78
79
        // Let's try current directory
80
        $cd = getcwd();
81
        $fullpath = $cd . '/' . $file;
82
        if ($cd && is_file($fullpath)) {
83
            return $fullpath;
84
        }
85
86
        // Check if it's writable
87
        if (!is_writable($fullpath)) {
88
            throw new \LogicException("Sorry but [$fullpath] is not writable!");
89
        }
90
91
        return false;
92
    }
93
94
    protected function notify($mail, $commandName = '')
95
    {
96
        // CommandName
97
        $commandName = !empty($commandName) ? $commandName : $this->getName();
98
99
100
        // Cannot notify ?
101
        if (!$this->config->isNotifyOnForCommand('replace')) {
102
            $this->output->info("No notifications sent.");
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 info() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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
            return;
104
        }
105
106
        // Notifications are On
107
        $notificationConfig = $this->config->getNotificationConfig();
108
        if (empty($notificationConfig)) {
109
            $this->output->warn("Notify is on, but notify configuration are missing.");
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 warn() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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...
110
            return;
111
        }
112
113
        // Send mail
114
        $mailHelper = new MailHelper($this->config);
115
        $mailHelper->send($mail, $commandName);
116
        $this->output->info("Notification sent.");
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 info() does only exist in the following implementations of said interface: Tequilarapido\Cli\EnhancedOutput.

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...
117
    }
118
119
120
}