Completed
Pull Request — master (#157)
by Thomas
06:34 queued 50s
created

LogfileCommand::execute()   C

Complexity

Conditions 12
Paths 91

Size

Total Lines 108
Code Lines 63

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 108
ccs 0
cts 67
cp 0
rs 5.034
cc 12
eloc 63
nc 91
nop 2
crap 156

How to fix   Long Method    Complexity   

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
 * Copyright (c) 1998-2015 Browser Capabilities Project
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the "Software"),
7
 * to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be included
13
 * in all copies or substantial portions of the Software.
14
 *
15
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
 * THE SOFTWARE.
22
 *
23
 * @category   Browscap-PHP
24
 * @package    Command
25
 * @copyright  1998-2015 Browser Capabilities Project
26
 * @license    http://www.opensource.org/licenses/MIT MIT License
27
 * @link       https://github.com/browscap/browscap-php/
28
 * @since      added with version 3.0
29
 */
30
31
namespace BrowscapPHP\Command;
32
33
use BrowscapPHP\Browscap;
34
use BrowscapPHP\Cache\BrowscapCache;
35
use BrowscapPHP\Cache\BrowscapCacheInterface;
36
use BrowscapPHP\Exception\InvalidArgumentException;
37
use BrowscapPHP\Exception\ReaderException;
38
use BrowscapPHP\Exception\UnknownBrowserException;
39
use BrowscapPHP\Exception\UnknownBrowserTypeException;
40
use BrowscapPHP\Exception\UnknownDeviceException;
41
use BrowscapPHP\Exception\UnknownEngineException;
42
use BrowscapPHP\Exception\UnknownPlatformException;
43
use BrowscapPHP\Helper\Filesystem;
44
use BrowscapPHP\Helper\IniLoader;
45
use BrowscapPHP\Helper\LoggerHelper;
46
use BrowscapPHP\Util\Logfile\ReaderCollection;
47
use BrowscapPHP\Util\Logfile\ReaderFactory;
48
use Symfony\Component\Console\Command\Command;
49
use Symfony\Component\Console\Input\InputArgument;
50
use Symfony\Component\Console\Input\InputInterface;
51
use Symfony\Component\Console\Input\InputOption;
52
use Symfony\Component\Console\Output\OutputInterface;
53
use Symfony\Component\Filesystem\Exception\IOException;
54
use Symfony\Component\Finder\Finder;
55
use Symfony\Component\Finder\SplFileInfo;
56
use WurflCache\Adapter\File;
57
58
/**
59
 * commands to parse a log file and parse the useragents in it
60
 *
61
 * @category   Browscap-PHP
62
 * @package    Command
63
 * @author     Dave Olsen, http://dmolsen.com
64
 * @author     Thomas Müller <[email protected]>
65
 * @copyright  Copyright (c) 1998-2015 Browser Capabilities Project
66
 * @version    3.0
67
 * @license    http://www.opensource.org/licenses/MIT MIT License
68
 * @link       https://github.com/browscap/browscap-php/
69
 */
70
class LogfileCommand extends Command
71
{
72
    /**
73
     * @var array
74
     */
75
    private $undefinedClients = array();
76
77
    private $uas         = array();
78
    private $uasWithType = array();
79
80
    private $countOk = 0;
81
    private $countNok = 0;
82
    private $totalCount = 0;
83
84
    /**
85
     * @var BrowscapCacheInterface
86
     */
87
    private $cache = null;
88
89
    /**
90
     * @var string
91
     */
92
    private $defaultCacheFolder;
93
94
    /**
95
     * @param string $defaultCacheFolder
96
     */
97 1
    public function __construct($defaultCacheFolder)
98
    {
99 1
        $this->defaultCacheFolder = $defaultCacheFolder;
100
101 1
        parent::__construct();
102 1
    }
103
104
    /**
105
     * @param \BrowscapPHP\Cache\BrowscapCacheInterface $cache
106
     *
107
     * @return $this
108
     */
109 1
    public function setCache(BrowscapCacheInterface $cache)
110
    {
111 1
        $this->cache = $cache;
112
113 1
        return $this;
114
    }
115
116
    /**
117
     * Configures the current command.
118
     */
119 1
    protected function configure()
120
    {
121 1
        $this
122 1
            ->setName('browscap:log')
123 1
            ->setDescription('Parses the supplied webserver log file.')
124 1
            ->addArgument(
125 1
                'output',
126 1
                InputArgument::REQUIRED,
127 1
                'Path to output log file',
128
                null
129 1
            )
130 1
            ->addOption(
131 1
                'log-file',
132 1
                'f',
133 1
                InputOption::VALUE_REQUIRED,
134
                'Path to a webserver log file'
135 1
            )
136 1
            ->addOption(
137 1
                'log-dir',
138 1
                'd',
139 1
                InputOption::VALUE_REQUIRED,
140
                'Path to webserver log directory'
141 1
            )
142 1
            ->addOption(
143 1
                'include',
144 1
                'i',
145 1
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
146 1
                'Include glob expressions for log files in the log directory',
147 1
                array('*.log', '*.log*.gz', '*.log*.bz2')
148 1
            )
149 1
            ->addOption(
150 1
                'exclude',
151 1
                'e',
152 1
                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED,
153 1
                'Exclude glob expressions for log files in the log directory',
154 1
                array('*error*')
155 1
            )
156 1
            ->addOption(
157 1
                'debug',
158 1
                null,
159 1
                InputOption::VALUE_NONE,
160
                'Should the debug mode entered?'
161 1
            )
162 1
            ->addOption(
163 1
                'cache',
164 1
                'c',
165 1
                InputOption::VALUE_OPTIONAL,
166 1
                'Where the cache files are located',
167 1
                $this->defaultCacheFolder
168 1
            )
169
        ;
170 1
    }
171
172
    /**
173
     * @param InputInterface  $input
174
     * @param OutputInterface $output
175
     *
176
     * @throws \UnexpectedValueException
177
     * @throws \BrowscapPHP\Exception\InvalidArgumentException
178
     * @return int|null|void
179
     */
180
    protected function execute(InputInterface $input, OutputInterface $output)
181
    {
182
        if (!$input->getOption('log-file') && !$input->getOption('log-dir')) {
183
            throw InvalidArgumentException::oneOfCommandArguments('log-file', 'log-dir');
184
        }
185
186
        $loggerHelper = new LoggerHelper();
187
        $logger       = $loggerHelper->create($input->getOption('debug'));
188
189
        $browscap   = new Browscap();
190
        $loader     = new IniLoader();
0 ignored issues
show
Unused Code introduced by
$loader is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
191
        $collection = ReaderFactory::factory();
192
        $fs         = new Filesystem();
193
194
        $browscap
195
            ->setLogger($logger)
196
            ->setCache($this->getCache($input))
197
        ;
198
199
        /** @var $file \Symfony\Component\Finder\SplFileInfo */
200
        foreach ($this->getFiles($input) as $file) {
201
            $this->uas = array();
202
            $path      = $this->getPath($file);
203
204
            $this->countOk  = 0;
205
            $this->countNok = 0;
206
207
            $logger->info('Analyzing file "'.$file->getPathname().'"');
208
209
            $lines = file($path);
210
211
            if (empty($lines)) {
212
                $logger->info('Skipping empty file "'.$file->getPathname().'"');
213
                continue;
214
            }
215
216
            $this->totalCount = count($lines);
217
218
            foreach ($lines as $line) {
219
                $this->handleLine(
220
                    $output,
221
                    $collection,
222
                    $browscap,
223
                    $line
224
                );
225
            }
226
227
            $this->outputProgress($output, '', true);
228
229
            arsort($this->uas, SORT_NUMERIC);
230
231
            try {
232
                $fs->dumpFile(
233
                    $input->getArgument('output').'/output.txt',
234
                    implode(PHP_EOL, array_unique($this->undefinedClients))
235
                );
236
            } catch (IOException $e) {
237
                // do nothing
238
            }
239
240
            try {
241
                $fs->dumpFile(
242
                    $input->getArgument('output').'/output-with-amount.txt',
243
                    $this->createAmountContent()
244
                );
245
            } catch (IOException $e) {
246
                // do nothing
247
            }
248
249
            try {
250
                $fs->dumpFile(
251
                    $input->getArgument('output').'/output-with-amount-and-type.txt',
252
                    $this->createAmountTypeContent()
253
                );
254
            } catch (IOException $e) {
255
                // do nothing
256
            }
257
        }
258
259
        $outputFile = $input->getArgument('output').'/output.txt';
260
261
        try {
262
            $fs->dumpFile(
263
                $outputFile,
264
                implode(PHP_EOL, array_unique($this->undefinedClients))
265
            );
266
        } catch (IOException $e) {
267
            throw new \UnexpectedValueException('writing to file "'.$outputFile.'" failed', 0, $e);
268
        }
269
270
        try {
271
            $fs->dumpFile(
272
                $input->getArgument('output').'/output-with-amount.txt',
273
                $this->createAmountContent()
274
            );
275
        } catch (IOException $e) {
276
            // do nothing
277
        }
278
279
        try {
280
            $fs->dumpFile(
281
                $input->getArgument('output').'/output-with-amount-and-type.txt',
282
                $this->createAmountTypeContent()
283
            );
284
        } catch (IOException $e) {
285
            // do nothing
286
        }
287
    }
288
289
    private function createAmountContent()
290
    {
291
        $counts = array();
292
293
        foreach ($this->uasWithType as $uas) {
294
            foreach ($uas as $userAgentString => $count) {
295
                if (isset($counts[$userAgentString])) {
296
                    $counts[$userAgentString] += $count;
297
                } else {
298
                    $counts[$userAgentString] = $count;
299
                }
300
            }
301
        }
302
303
        $content = '';
304
305
        arsort($counts, SORT_NUMERIC);
306
307
        foreach ($counts as $agentOfLine => $count) {
308
            $content .= "$count\t$agentOfLine\n";
309
        }
310
311
        return $content;
312
    }
313
314
    private function createAmountTypeContent()
315
    {
316
        $content = '';
317
        $types   = array('B', 'T', 'P', 'D', 'N', 'U');
318
319
        foreach ($types as $type) {
320
            if (!isset($this->uasWithType[$type])) {
321
                continue;
322
            }
323
324
            arsort($this->uasWithType[$type], SORT_NUMERIC);
325
326
            foreach ($this->uasWithType[$type] as $agentOfLine => $count) {
327
                $content .= "$type\t$count\t$agentOfLine\n";
328
            }
329
        }
330
331
        return $content;
332
    }
333
334
    /**
335
     * @param \Symfony\Component\Console\Output\OutputInterface $output
336
     * @param \BrowscapPHP\Util\Logfile\ReaderCollection        $collection
337
     * @param \BrowscapPHP\Browscap                             $browscap
338
     * @param integer                                           $line
339
     *
340
     * @throws UnknownBrowserException
341
     * @throws UnknownBrowserTypeException
342
     * @throws UnknownDeviceException
343
     * @throws UnknownEngineException
344
     * @throws UnknownPlatformException
345
     * @throws \Exception
346
     */
347
    private function handleLine(OutputInterface $output, ReaderCollection $collection, Browscap $browscap, $line)
348
    {
349
        $userAgentString = '';
350
351
        try {
352
            $userAgentString = $collection->read($line);
353
354
            try {
355
                $this->getResult($browscap->getBrowser($userAgentString));
356
            } catch (\Exception $e) {
357
                $this->undefinedClients[] = $userAgentString;
358
359
                throw $e;
360
            }
361
362
            $type = '.';
363
            $this->countOk++;
364
        } catch (ReaderException $e) {
365
            $type = 'E';
366
            $this->countNok++;
367
        } catch (UnknownBrowserTypeException $e) {
368
            $type = 'T';
369
            $this->countNok++;
370
        } catch (UnknownBrowserException $e) {
371
            $type = 'B';
372
            $this->countNok++;
373
        } catch (UnknownPlatformException $e) {
374
            $type = 'P';
375
            $this->countNok++;
376
        } catch (UnknownDeviceException $e) {
377
            $type = 'D';
378
            $this->countNok++;
379
        } catch (UnknownEngineException $e) {
380
            $type = 'N';
381
            $this->countNok++;
382
        } catch (\Exception $e) {
383
            $type = 'U';
384
            $this->countNok++;
385
        }
386
387
        $this->outputProgress($output, $type);
388
389
        // count all useragents
390
        if (isset($this->uas[$userAgentString])) {
391
            $this->uas[$userAgentString]++;
392
        } else {
393
            $this->uas[$userAgentString] = 1;
394
        }
395
396
        if ('.' !== $type && 'E' !== $type) {
397
            // count all undetected useragents grouped by detection error
398
            if (!isset($this->uasWithType[$type])) {
399
                $this->uasWithType[$type] = array();
400
            }
401
402
            if (isset($this->uasWithType[$type][$userAgentString])) {
403
                $this->uasWithType[$type][$userAgentString]++;
404
            } else {
405
                $this->uasWithType[$type][$userAgentString] = 1;
406
            }
407
        }
408
    }
409
410
    /**
411
     * @param \Symfony\Component\Console\Output\OutputInterface $output
412
     * @param string                                            $result
413
     * @param bool                                              $end
414
     *
415
     * @return int
416
     */
417
    private function outputProgress(OutputInterface $output, $result, $end = false)
418
    {
419
        if (($this->totalCount % 70) === 0 || $end) {
420
            $formatString = '  %'.strlen($this->countOk).'d OK, %'.strlen($this->countNok).'d NOK, Summary %'
421
                .strlen($this->totalCount).'d';
422
423
            if ($end) {
424
                $result = str_pad($result, 70 - ($this->totalCount % 70), ' ', STR_PAD_RIGHT);
425
            }
426
427
            $endString = sprintf($formatString, $this->countOk, $this->countNok, $this->totalCount);
428
429
            $output->writeln($result.$endString);
430
431
            return;
432
        }
433
434
        $output->write($result);
435
    }
436
437
    /**
438
     * @param \stdClass $result
439
     *
440
     * @return string
441
     */
442
    private function getResult(\stdClass $result)
443
    {
444
        if ('Default Browser' === $result->browser) {
445
            throw new UnknownBrowserException('unknwon browser found');
446
        }
447
448
        if ('unknown' === $result->browser_type) {
449
            throw new UnknownBrowserTypeException('unknwon browser type found');
450
        }
451
452
        if (in_array($result->browser_type, array('Bot/Crawler', 'Library'))) {
453
            return '.';
454
        }
455
456
        if ('unknown' === $result->platform) {
457
            throw new UnknownPlatformException('unknown platform found');
458
        }
459
460
        if ('unknown' === $result->device_type) {
461
            throw new UnknownDeviceException('unknwon device type found');
462
        }
463
464
        if ('unknown' === $result->renderingengine_name) {
465
            throw new UnknownEngineException('unknown rendering engine found');
466
        }
467
468
        return '.';
469
    }
470
471
    /**
472
     * @param \Symfony\Component\Console\Input\InputInterface $input
473
     *
474
     * @return \Symfony\Component\Finder\Finder
475
     */
476
    private function getFiles(InputInterface $input)
477
    {
478
        $finder = Finder::create();
479
480
        if ($input->getOption('log-file')) {
481
            $file = $input->getOption('log-file');
482
            $finder->append(Finder::create()->in(dirname($file))->name(basename($file)));
483
        }
484
485
        if ($input->getOption('log-dir')) {
486
            $dirFinder = Finder::create()
487
                ->in($input->getOption('log-dir'));
488
            array_map(array($dirFinder, 'name'), $input->getOption('include'));
489
            array_map(array($dirFinder, 'notName'), $input->getOption('exclude'));
490
491
            $finder->append($dirFinder);
492
        }
493
494
        return $finder;
495
    }
496
497
    /**
498
     * @param \Symfony\Component\Finder\SplFileInfo $file
499
     *
500
     * @return string
501
     */
502
    private function getPath(SplFileInfo $file)
503
    {
504
        switch ($file->getExtension()) {
505
            case 'gz':
506
                $path = 'compress.zlib://'.$file->getPathname();
507
                break;
508
            case 'bz2':
509
                $path = 'compress.bzip2://'.$file->getPathname();
510
                break;
511
            default:
512
                $path = $file->getPathname();
513
                break;
514
        }
515
516
        return $path;
517
    }
518
519
    /**
520
     * @param InputInterface $input
521
     *
522
     * @return BrowscapCacheInterface
523
     */
524 View Code Duplication
    private function getCache(InputInterface $input)
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...
525
    {
526
        if (null === $this->cache) {
527
            $cacheAdapter = new File(array(File::DIR => $input->getOption('cache')));
528
            $this->cache  = new BrowscapCache($cacheAdapter);
529
        }
530
531
        return $this->cache;
532
    }
533
}
534