DeleteCommand   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 4
dl 0
loc 44
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 23 5
1
<?php
2
3
/**
4
 * @file DeleteCommand.php
5
 * @brief This file contains the DeleteCommand class.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace EoC\CLI\Command;
12
13
14
use Symfony\Component\Console\Input\InputArgument;
15
use Symfony\Component\Console\Input\InputInterface;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
19
20
/**
21
 * @brief Deletes the PitPress database.
22
 * @nosubgrouping
23
 */
24
class DeleteCommand extends AbstractCommand {
25
26
27
  /**
28
   * @brief Configures the command.
29
   */
30
  protected function configure() {
31
    $this->setName("delete");
32
    $this->setDescription("Deletes the specified database, if not in use");
33
34
    $this->addArgument("database",
35
      InputArgument::REQUIRED,
36
      "The CouchDB database name to delete");
37
  }
38
39
40
  /**
41
   * @brief Executes the command.
42
   */
43
  protected function execute(InputInterface $input, OutputInterface $output) {
44
    $database = (string)$input->getArgument('database');
45
46
    $couch = $this->getConnection();
47
48
    // In case there is a database in use, we select it, because you the selected database can't be deleted.
49
    try {
50
      $selected = $this->getDatabase();
51
    }
52
    catch (\RuntimeException $e) {
53
      $selected = NULL;
54
    }
55
56
    if ($database === $selected)
57
      throw new \RuntimeException('You cannot delete the selected database.');
58
59
    $question = new ConfirmationQuestion('Are you sure you want delete the database? [Y/n]', FALSE);
60
61
    $helper = $this->getHelper('question');
62
63
    if ($helper->ask($input, $output, $question) || $input->getOption('no-interaction'))
64
      $couch->deleteDb($database);
65
  }
66
67
}