GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 072bc6...f2b09a )
by Maurice
02:08
created

ObfuscateCommand::execute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 58
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 58
rs 9.639
cc 3
eloc 32
nc 4
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * ObfuscateCommand.php
4
 *
5
 * @package         Obfuscator
6
 * @subpackage      Console
7
 */
8
9
namespace Naneau\Obfuscator\Console\Command;
10
11
use Naneau\Obfuscator\Container;
12
13
use Naneau\Obfuscator\Obfuscator;
14
use Naneau\Obfuscator\Obfuscator\Event\File as FileEvent;
15
use Naneau\Obfuscator\Obfuscator\Event\FileError as FileErrorEvent;
16
17
use Symfony\Component\Console\Command\Command;
18
use Symfony\Component\Console\Input\InputArgument;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Input\InputOption;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
use \InvalidArgumentException;
24
25
/**
26
 * ObfuscateCommand
27
 *
28
 * Obfuscating command
29
 *
30
 * @category        Naneau
31
 * @package         Obfuscator
32
 * @subpackage      Console
33
 */
34
class ObfuscateCommand extends Command
35
{
36
    /**
37
     * the obfuscator
38
     *
39
     * @var Obfuscator
40
     */
41
    private $obfuscator;
42
43
    /**
44
     * the container
45
     *
46
     * @var Container
47
     */
48
    private $container;
49
50
    /**
51
     * Configure the command
52
     *
53
     * @return void
54
     **/
55
    protected function configure()
56
    {
57
        $this
58
            ->setName('obfuscate')
59
            ->setDescription('Obfuscate a directory of PHP files')
60
            ->addArgument(
61
                'input_directory',
62
                InputArgument::REQUIRED,
63
                'Directory of source files, if no output directory is given, it will be overwritten'
64
            )
65
            ->addArgument(
66
                'output_directory',
67
                InputArgument::OPTIONAL,
68
                'Output directory'
69
            )->addOption(
70
                'leave_whitespace',
71
                null,
72
                InputOption::VALUE_NONE,
73
                'Leave whitespace in output?'
74
            )->addOption(
75
                'ignore_error',
76
                null,
77
                InputOption::VALUE_NONE,
78
                'Continue processing the next file when error is encountered'
79
            )->addOption(
80
                'config',
81
                null,
82
                InputOption::VALUE_REQUIRED,
83
                'Configuration file to use'
84
            );
85
86
        $this->setContainer(new Container);
87
    }
88
89
    /**
90
     * Execute the command
91
     *
92
     * @param  InputInterface  $input
93
     * @param  OutputInterface $output
94
     * @return void
95
     **/
96
    protected function execute(InputInterface $input, OutputInterface $output)
97
    {
98
        // Finalize the container
99
        $this->finalizeContainer($input);
100
101
        // Input/output dirs
102
        $inputDirectory = $input->getArgument('input_directory');
103
        $outputDirectory = $input->getArgument('output_directory');
104
105
        if (!empty($outputDirectory)) {
106
107
            $output->writeln(sprintf(
108
                'Copying input directory <info>%s</info> to <info>%s</info>',
109
                $inputDirectory,
110
                $outputDirectory
111
            ));
112
113
            $this->copyDir($inputDirectory, $outputDirectory);
114
115
            $directory = $outputDirectory;
116
        } else {
117
            $directory = $inputDirectory;
118
        }
119
120
        // Strip whitespace?
121
        $stripWhitespace = !$input->getOption('leave_whitespace');
122
        $ignoreError = !!$input->getOption('ignore_error');
123
124
        // Show every file
125
        $this->getObfuscator()->getEventDispatcher()->addListener(
126
            'obfuscator.file',
127
            function(FileEvent $event) use ($output, $directory) {
128
                $output->writeln(sprintf(
129
                    'Obfuscating <info>%s</info>',
130
                    substr($event->getFile(), strlen($directory))
131
                ));
132
            }
133
        );
134
        // Show error processing file
135
        if($ignoreError) {
136
            $this->getObfuscator()->getEventDispatcher()->addListener(
137
                'obfuscator.file.error',
138
                function(FileErrorEvent $event) use ($output, $directory) {
139
                    $output->writeln(sprintf(
140
                        'Error obfuscating <error>%s</error>',
141
                        substr($event->getFile(), strlen($directory))
142
                    ));
143
                    $output->writeln(sprintf(
144
                        'Parsing error: <error>%s</error>', $event->getErrorMessage()
145
                    ));
146
                }
147
            );
148
        }
149
150
        // Actual obfuscation
151
        $this->getObfuscator()->obfuscate($directory, $stripWhitespace,
152
            $ignoreError);
153
    }
154
155
    /**
156
     * Get the container
157
     *
158
     * @return Container
159
     */
160
    public function getContainer()
161
    {
162
        return $this->container;
163
    }
164
165
    /**
166
     * Set the container
167
     *
168
     * @param Container $container
169
     * @return ObfuscateCommand
170
     */
171
    public function setContainer(Container $container)
172
    {
173
        $this->container = $container;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Get the obfuscator
180
     *
181
     * @return Obfuscator
182
     */
183
    public function getObfuscator()
184
    {
185
        return $this->getContainer()->getContainer()->get('obfuscator');
186
    }
187
188
    /**
189
     * Copy a directory
190
     *
191
     * @param string $from
192
     * @param string $to
193
     * @return ObfuscateCommand
194
     **/
195
    private function copyDir($from, $to)
196
    {
197
        // FIXME implement native copy
198
        $output = array();
199
        $return = 0;
200
        $command = sprintf('cp -rf %s %s', $from, $to);
201
202
        exec($command, $output, $return);
203
204
        if ($return !== 0)  {
205
            throw new \Exception('Could not copy directory');
206
        }
207
208
        return $this;
209
    }
210
211
    /**
212
     * Finalize the container
213
     *
214
     * loads any given config file and compiles the container
215
     *
216
     * @return ObfuscateCommand
217
     **/
218
    private function finalizeContainer(InputInterface $input)
219
    {
220
        // Load config if given
221
        $config = $input->getOption('config');
222
        if (!empty($config)) {
223
            if (!is_readable($config)) {
224
                throw new InvalidArgumentException(sprintf(
225
                    'Can not read config file "%s"',
226
                    $config
227
                ));
228
            }
229
            $this->getContainer()->loadFile($config);
230
        }
231
232
        $this->getContainer()->getContainer()->compile();
233
234
        return $this;
235
    }
236
}
237