Completed
Push — master ( 8e94f8...744b49 )
by P.R.
04:26
created

ConfigCommand::setTableParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 4
crap 2
1
<?php
2
//----------------------------------------------------------------------------------------------------------------------
3
namespace SetBased\Audit\Command;
4
5
use SetBased\Stratum\Style\StratumStyle;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Question\ConfirmationQuestion;
10
use Symfony\Component\Console\Question\Question;
11
12
//----------------------------------------------------------------------------------------------------------------------
13
/**
14
 * Command for editing data in config file.
15
 */
16
class ConfigCommand extends BaseCommand
17
{
18
  //--------------------------------------------------------------------------------------------------------------------
19
  /**
20
   * The helper instance by name.
21
   */
22
  private $helper;
23
24
  //--------------------------------------------------------------------------------------------------------------------
25
  /**
26
   * {@inheritdoc}
27
   */
28
  protected function configure()
29
  {
30
    $this->setName('config')
31
         ->setDescription('Create or edit config file')
32
         ->addArgument('config file', InputArgument::OPTIONAL, 'The audit configuration file', 'etc/audit.json');
33
  }
34
35
  //--------------------------------------------------------------------------------------------------------------------
36
  /**
37
   * {@inheritdoc}
38
   */
39
  protected function execute(InputInterface $input, OutputInterface $output)
40
  {
41
    $this->io = new StratumStyle($input, $output);
42
43
    $this->configFileName = $input->getArgument('config file');
44
    $this->readConfigFile();
45
    $this->configFileName = sprintf('%s%s', $input->getArgument('config file'), '.new');
46
47
    $this->helper = $this->getHelper('question');
48
49
    foreach ($this->config as $configPart => $partData)
50
    {
51
      $this->setConfigPart($input, $output, $configPart);
52
    }
53
54
    $this->writeTwoPhases($this->configFileName, json_encode($this->config, JSON_PRETTY_PRINT));
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
  /**
59
   * Controls config parts.
60
   *
61
   * @param InputInterface  $input
62
   * @param OutputInterface $output
63
   * @param string          $configPart Part of config file.
64
   */
65
  protected function setConfigPart(InputInterface $input, OutputInterface $output, $configPart)
66
  {
67
    switch ($configPart)
68
    {
69
      case 'database':
70
        $this->setDatabasePart($input, $output, $configPart);
71
        break;
72
      case 'tables':
73
        foreach ($this->config[$configPart] as $tableName => $tableData)
74
        {
75
          $this->setTableParams($input, $output, $configPart, $tableName);
76
        }
77
        break;
78
    }
79
  }
80
81
  //--------------------------------------------------------------------------------------------------------------------
82
  /**
83
   * Configure each table for audit or not.
84
   *
85
   * @param InputInterface  $input
86
   * @param OutputInterface $output
87
   * @param string          $configPart Part of config file.
88
   * @param string          $tableName  The table name.
89
   */
90
  protected function setTableParams(InputInterface $input, OutputInterface $output, $configPart, $tableName)
91
  {
92
    $this->io->logInfo('Please input data for table <dbo>\'%s\'</dbo>.', $tableName);
93
    $question = sprintf('Audit table \'%s\' or not (y|(n)): ', $tableName);
94
    $question = new ConfirmationQuestion($question, false);
95
96
    $this->config[$configPart][$tableName]['audit'] = $this->helper->ask($input, $output, $question);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

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...
97
  }
98
99
  //--------------------------------------------------------------------------------------------------------------------
100
  /**
101
   * Configure database part.
102
   *
103
   * @param InputInterface  $input
104
   * @param OutputInterface $output
105
   * @param string          $configPart Part of config file.
106
   */
107
  protected function setDatabasePart(InputInterface $input, OutputInterface $output, $configPart)
108
  {
109
    $this->io->logInfo('Please input data for <dbo>\'%s\'</dbo> part.', $configPart);
110
    foreach ($this->config[$configPart] as $parameter => $value)
111
    {
112
      $question                              = sprintf('Please enter the \'%s\' (%s): ', $parameter, $value);
113
      $question                              = new Question($question, $value);
114
      $this->config[$configPart][$parameter] = $this->helper->ask($input, $output, $question);
1 ignored issue
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method ask() does only exist in the following implementations of said interface: Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

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...
115
    }
116
  }
117
118
  //--------------------------------------------------------------------------------------------------------------------
119
}
120
121
//----------------------------------------------------------------------------------------------------------------------
122