ConnectCommand::execute()   B
last analyzed

Complexity

Conditions 6
Paths 15

Size

Total Lines 45
Code Lines 29

Duplication

Lines 16
Ratio 35.56 %

Importance

Changes 0
Metric Value
dl 16
loc 45
rs 8.439
c 0
b 0
f 0
cc 6
eloc 29
nc 15
nop 2
1
<?php
2
3
/**
4
 * @file ConnectCommand.php
5
 * @brief This file contains the ConnectCommand 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\InputOption;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\OutputInterface;
18
use Symfony\Component\Console\Question\Question;
19
20
use EoC\Couch;
21
use EoC\Adapter;
22
23
24
/**
25
 * @brief Connects to CouchDB server..
26
 * @nosubgrouping
27
 * @todo Fix a bug when read map and reduce from file.
28
 */
29
class ConnectCommand extends AbstractCommand {
30
31
32
  /**
33
   * @brief Configures the command.
34
   */
35
  protected function configure() {
36
    $this->setName("connect");
37
    $this->setDescription("Connects to CouchDB server");
38
39
    $this->addArgument("user",
40
      InputArgument::REQUIRED,
41
      "The CouchDB user name to use when connecting to the server");
42
43
    $this->addOption("server",
44
      "s",
45
      InputOption::VALUE_OPTIONAL,
46
      <<<'DESC'
47
Connects to the CouchDB server on the given host
48
Server must be expressed as host:port according to RFC 3986.
49
DESC
50
      ,
51
      Adapter\AbstractAdapter::DEFAULT_SERVER);
52
  }
53
54
55
  /**
56
   * @brief Executes the command.
57
   */
58
  protected function execute(InputInterface $input, OutputInterface $output) {
0 ignored issues
show
Coding Style introduced by
execute uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
59
    $user = (string)$input->getArgument('user');
60
61
    $question = new Question('Enter password:');
62
    $question->setHidden(true);
63
    $question->setHiddenFallback(false);
64
65
    $helper = $this->getHelper('question');
66
    $password = $helper->ask($input, $output, $question);
67
68
    $server = (string)$input->getOption('server');
69
70
    $couch = new Couch(new Adapter\CurlAdapter($server, $user, $password));
71
    $couch->getServerInfo(); // Checks the connection.
72
73
    $connection = [];
74
    $connection['server'] = $server;
75
    $connection['user'] = $user;
76
    $connection['password'] = $password;
77
78
    $serialized = serialize($connection);
79
80
    // We reset the database in use.
81
    if ($shmKey = ftok($_SERVER['PHP_SELF'], 'd')) {
82
      if (@$shmId = shmop_open($shmKey, "a", 0644, 0))
83
        shmop_delete($shmId);
84
    }
85
86 View Code Duplication
    if ($shmKey = ftok($_SERVER['PHP_SELF'], 'c')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
87
88
      if (@$shmId = shmop_open($shmKey, 'a', 0644, 0))
89
        shmop_delete($shmId);
90
91
      $shmId = shmop_open($shmKey, 'n', 0644, mb_strlen($serialized));
92
93
      if ($shmId) {
94
        $shmBytesWritten = shmop_write($shmId, $serialized, 0);
0 ignored issues
show
Unused Code introduced by
$shmBytesWritten is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
95
        shmop_close($shmId);
96
      }
97
      else
98
        throw new \RuntimeException("Couldn't create shared memory segment.");
99
    }
100
    else
101
      throw new \RuntimeException("Cannot get a System V IPC key.");
102
  }
103
104
}