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.

ObfuscateCommand::copyDir()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 22
rs 9.2
cc 3
eloc 11
nc 4
nop 2
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
            )->addOption(
85
                'memory_limit',
86
                null,
87
                InputOption::VALUE_REQUIRED,
88
                'Runtime memory when running the obsfucator. ' .
89
                'Example: 128M ' .
90
                'See http://php.net/manual/en/ini.core.php#ini.memory-limit'
91
            );
92
93
        $this->setContainer(new Container);
94
    }
95
96
    /**
97
     * Execute the command
98
     *
99
     * @param  InputInterface  $input
100
     * @param  OutputInterface $output
101
     * @return void
102
     **/
103
    protected function execute(InputInterface $input, OutputInterface $output)
104
    {
105
        // Finalize the container
106
        $this->finalizeContainer($input);
107
108
        // Change runtime memory
109
        if($memory = $input->getOption('memory_limit')) {
110
            ini_set("memory_limit", $memory);
111
        }
112
        // Input/output dirs
113
        $inputDirectory = $input->getArgument('input_directory');
114
        $outputDirectory = $input->getArgument('output_directory');
115
116
        if (!empty($outputDirectory)) {
117
118
            $output->writeln(sprintf(
119
                'Copying input directory <info>%s</info> to <info>%s</info>',
120
                $inputDirectory,
121
                $outputDirectory
122
            ));
123
124
            $this->copyDir($inputDirectory, $outputDirectory);
125
126
            $directory = $outputDirectory;
127
        } else {
128
            $directory = $inputDirectory;
129
        }
130
131
        // Strip whitespace?
132
        $stripWhitespace = !$input->getOption('leave_whitespace');
133
        $ignoreError = !!$input->getOption('ignore_error');
134
135
        // Show every file
136
        $this->getObfuscator()->getEventDispatcher()->addListener(
137
            'obfuscator.file',
138
            function(FileEvent $event) use ($output, $directory) {
139
                $output->writeln(sprintf(
140
                    'Obfuscating <info>%s</info>',
141
                    substr($event->getFile(), strlen($directory))
142
                ));
143
            }
144
        );
145
        // Show error processing file
146
        if($ignoreError) {
147
            $this->getObfuscator()->getEventDispatcher()->addListener(
148
                'obfuscator.file.error',
149
                function(FileErrorEvent $event) use ($output, $directory) {
150
                    $output->writeln(sprintf(
151
                        'Error obfuscating <error>%s</error>',
152
                        substr($event->getFile(), strlen($directory))
153
                    ));
154
                    $output->writeln(sprintf(
155
                        'Parsing error: <error>%s</error>', $event->getErrorMessage()
156
                    ));
157
                }
158
            );
159
        }
160
161
        // Actual obfuscation
162
        $this->getObfuscator()->obfuscate($directory, $stripWhitespace,
163
            $ignoreError);
164
    }
165
166
    /**
167
     * Get the container
168
     *
169
     * @return Container
170
     */
171
    public function getContainer()
172
    {
173
        return $this->container;
174
    }
175
176
    /**
177
     * Set the container
178
     *
179
     * @param Container $container
180
     * @return ObfuscateCommand
181
     */
182
    public function setContainer(Container $container)
183
    {
184
        $this->container = $container;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Get the obfuscator
191
     *
192
     * @return Obfuscator
193
     */
194
    public function getObfuscator()
195
    {
196
        return $this->getContainer()->getContainer()->get('obfuscator');
197
    }
198
199
    /**
200
     * Copy a directory
201
     *
202
     * @param string $from
203
     * @param string $to
204
     * @return ObfuscateCommand
205
     **/
206
    private function copyDir($from, $to)
207
    {
208
        // FIXME implement native copy
209
        $output = array();
210
        $return = 0;
211
212
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
213
            // WINDOWS
214
            $command = sprintf('XCOPY "%s" "%s" /hievry', $from, $to);
215
        } else {
216
            // *NIX
217
            $command = sprintf('cp -rf %s %s', $from, $to);
218
        }        
219
220
        exec($command, $output, $return);
221
222
        if ($return !== 0)  {
223
            throw new \Exception('Could not copy directory');
224
        }
225
226
        return $this;
227
    }
228
229
    /**
230
     * Finalize the container
231
     *
232
     * loads any given config file and compiles the container
233
     *
234
     * @return ObfuscateCommand
235
     **/
236
    private function finalizeContainer(InputInterface $input)
237
    {
238
        // Load config if given
239
        $config = $input->getOption('config');
240
        if (!empty($config)) {
241
            if (!is_readable($config)) {
242
                throw new InvalidArgumentException(sprintf(
243
                    'Can not read config file "%s"',
244
                    $config
245
                ));
246
            }
247
            $this->getContainer()->loadFile($config);
248
        }
249
250
        $this->getContainer()->getContainer()->compile();
251
252
        return $this;
253
    }
254
}
255