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
Pull Request — master (#34)
by
unknown
03:22
created

ObfuscateCommand::copyDirectory()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 16
rs 8.8571
cc 5
eloc 10
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
            );
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
        $this->copyDirectory($from, $to);
198
        
199
        if (!is_dir($to))  {
200
            throw new \Exception('Could not copy directory');
201
        }
202
        
203
        return $this;
204
    }
205
    
206
    /**
207
     * Recursively copy a directory
208
     *
209
     * @param string $src
210
     * @param string $dst
211
     * @return void
212
     **/
213
    private function copyDirectory($src,$dst)
214
    {
215
        $dir = opendir($src);
216
        @mkdir($dst);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
217
        while(false !== ( $file = readdir($dir)) ) {
218
            if (( $file != '.' ) && ( $file != '..' )) {
219
                if ( is_dir($src . '/' . $file) ) {
220
                    $this->copyDirectory($src . '/' . $file,$dst . '/' . $file);
221
                }
222
                else {
223
                    copy($src . '/' . $file,$dst . '/' . $file);
224
                }
225
            }
226
        }
227
        closedir($dir);
228
    }
229
230
    /**
231
     * Finalize the container
232
     *
233
     * loads any given config file and compiles the container
234
     *
235
     * @return ObfuscateCommand
236
     **/
237
    private function finalizeContainer(InputInterface $input)
238
    {
239
        // Load config if given
240
        $config = $input->getOption('config');
241
        if (!empty($config)) {
242
            if (!is_readable($config)) {
243
                throw new InvalidArgumentException(sprintf(
244
                    'Can not read config file "%s"',
245
                    $config
246
                ));
247
            }
248
            $this->getContainer()->loadFile($config);
249
        }
250
251
        $this->getContainer()->getContainer()->compile();
252
253
        return $this;
254
    }
255
}
256