ViewCommand::getCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
1
<?php namespace Magestead\Command\Log;
2
3
use Magestead\Command\ProcessCommand;
4
use Magestead\Helper\Config;
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Class ViewCommand
12
 * @package Magestead\Command\Redis
13
 */
14 View Code Duplication
class ViewCommand extends Command
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in 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...
15
{
16
    protected $_config;
17
    protected $_projectPath;
18
19
    /**
20
     * Configure the view command
21
     */
22
    protected function configure()
23
    {
24
        $this->_projectPath = getcwd();
25
        $this->setName("log:view");
26
        $this->setDescription("View a specific server log");
27
        $this->addArgument('log', InputArgument::REQUIRED, 'access or error');
28
    }
29
30
    /**
31
     * @param InputInterface $input
32
     * @param OutputInterface $output
33
     * @return ProcessCommand
34
     */
35
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37
        $log = $input->getArgument('log');
38
39
        $output->writeln('<info>Viewing '. ucwords($log) . ' Log</info>');
40
        $command = $this->getCommand(new Config($output), $log);
41
        if (!$command) {
42
            return $output->writeln('<error>Command not available for this application</error>');
43
        }
44
45
        $pCommand = "vagrant ssh -c '". $command ."'";
46
        return new ProcessCommand($pCommand, $this->_projectPath, $output);
47
    }
48
49
    /**
50
     * @param Config $config
51
     * @param $log
52
     * @return string
53
     */
54
    private function getCommand(Config $config, $log)
55
    {
56
        $server = $config->_config['magestead']['server'];
0 ignored issues
show
Documentation introduced by
The property $_config is declared protected in Magestead\Helper\Config. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
57
        $os = $config->_config['magestead']['os'];
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $os. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
Documentation introduced by
The property $_config is declared protected in Magestead\Helper\Config. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
58
59
        $location = $this->getLogLocation($server, $os);
60
        $command = 'cat /var/log/' . $location . '/' . $config->base_url . '-' . $log . '.log';
0 ignored issues
show
Documentation introduced by
The property base_url does not exist on object<Magestead\Helper\Config>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
62
        return $command;
63
    }
64
65
    /**
66
     * @param $server
67
     * @param $os
68
     * @return string
69
     */
70
    private function getLogLocation($server, $os)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $os. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
71
    {
72
        $location = 'nginx';
73
        if ($server != 'nginx') {
74
            $location = ($os == 'ubuntu14') ? 'apache2' : 'httpd';
75
        }
76
77
        return $location;
78
    }
79
}
80