Test Failed
Pull Request — master (#448)
by
unknown
05:18
created

MageApplication   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 111
Duplicated Lines 18.02 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 11
dl 20
loc 111
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 19 1
B configure() 0 33 11
A loadBuiltInCommands() 20 20 5
A instantiateRuntime() 0 4 1
A getRuntime() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of the Magallanes package.
4
 *
5
 * (c) Andrés Montañez <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Mage;
12
13
use Mage\Command\AbstractCommand;
14
use Mage\Runtime\Runtime;
15
use Symfony\Component\Finder\Finder;
16
use Symfony\Component\Finder\SplFileInfo;
17
use Monolog\Logger;
18
use Monolog\Handler\StreamHandler;
19
use Symfony\Component\EventDispatcher\EventDispatcher;
20
use Symfony\Component\Console\Event\ConsoleErrorEvent;
21
use Symfony\Component\Console\ConsoleEvents;
22
use Symfony\Component\Console\Application;
23
use Symfony\Component\Yaml\Parser;
24
use Symfony\Component\Yaml\Exception\ParseException;
25
use ReflectionClass;
26
use Mage\Runtime\Exception\RuntimeException;
27
28
/**
29
 * The Console Application for launching the Mage command in a standalone instance
30
 *
31
 * @author Andrés Montañez <[email protected]>
32
 */
33
class MageApplication extends Application
34
{
35
    protected $runtime;
36
    protected $file;
37
38
    /**
39
     * @param string $file The YAML file from which to read the configuration
40
     */
41
    public function __construct($file)
42
    {
43
        parent::__construct('Magallanes', Mage::VERSION);
44
45
        $this->file = $file;
46
        $dispatcher = new EventDispatcher();
47
        $this->setDispatcher($dispatcher);
48
49
        $dispatcher->addListener(ConsoleEvents::ERROR, function (ConsoleErrorEvent $event) {
50
            $output = $event->getOutput();
51
            $command = $event->getCommand();
52
            $output->writeln(sprintf('Oops, exception thrown while running command <info>%s</info>', $command->getName()));
53
            $exitCode = $event->getExitCode();
54
            $event->setError(new \LogicException('Caught exception', $exitCode, $event->getError()));
55
        });
56
57
        $this->runtime = $this->instantiateRuntime();
58
        $this->loadBuiltInCommands();
59
    }
60
61
    /**
62
     * Configure the Magallanes Application
63
     *
64
     * @throws RuntimeException
65
     */
66
    public function configure()
67
    {
68
        if (!file_exists($this->file) || !is_readable($this->file)) {
69
            throw new RuntimeException(sprintf('The file "%s" does not exists or is not readable.', $this->file));
70
        }
71
72
        try {
73
            $parser = new Parser();
74
            $config = $parser->parse(file_get_contents($this->file));
75
        } catch (ParseException $exception) {
76
            throw new RuntimeException(sprintf('Error parsing the file "%s".', $this->file));
77
        }
78
79
        if (array_key_exists('magephp', $config) && is_array($config['magephp'])) {
80
            $logger = null;
81
            if (array_key_exists('log_dir', $config['magephp']) && file_exists($config['magephp']['log_dir']) && is_dir($config['magephp']['log_dir'])) {
82
                $logfile = sprintf('%s/%s.log', $config['magephp']['log_dir'], date('Ymd_His'));
83
                $config['magephp']['log_file'] = $logfile;
84
85
                $logger = new Logger('magephp');
86
                $logger->pushHandler(new StreamHandler($logfile));
87
88
            } elseif (array_key_exists('log_dir', $config['magephp']) && !is_dir($config['magephp']['log_dir'])) {
89
                throw new RuntimeException(sprintf('The configured log_dir "%s" does not exists or is not a directory.', $config['magephp']['log_dir']));
90
            }
91
92
            $this->runtime->setConfiguration($config['magephp']);
93
            $this->runtime->setLogger($logger);
94
            return;
95
        }
96
97
        throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $this->file));
98
    }
99
100
    /**
101
     * Loads the BuiltIn Commands
102
     */
103 View Code Duplication
    protected function loadBuiltInCommands()
0 ignored issues
show
Duplication introduced by
This method 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...
104
    {
105
        $finder = new Finder();
106
        $finder->files()->in(__DIR__ . '/Command/BuiltIn')->name('*Command.php');
107
108
        /** @var SplFileInfo $file */
109
        foreach ($finder as $file) {
110
            $class = substr('\\Mage\\Command\\BuiltIn\\' . str_replace('/', '\\', $file->getRelativePathname()), 0, -4);
111
            if (class_exists($class)) {
112
                $reflex = new ReflectionClass($class);
113
                if ($reflex->isInstantiable()) {
114
                    $command = new $class();
115
                    if ($command instanceof AbstractCommand) {
116
                        $command->setRuntime($this->runtime);
117
                        $this->add($command);
118
                    }
119
                }
120
            }
121
        }
122
    }
123
124
    /**
125
     * Gets the Runtime instance to use
126
     *
127
     * @return Runtime
128
     */
129
    protected function instantiateRuntime()
130
    {
131
        return new Runtime();
132
    }
133
134
    /**
135
     * Get the Runtime instance
136
     *
137
     * @return Runtime
138
     */
139
    public function getRuntime()
140
    {
141
        return $this->runtime;
142
    }
143
}
144