Completed
Push — develop ( 373768...b82813 )
by Mike
05:50
created

ErrorHandlingMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
ccs 16
cts 16
cp 1
wmc 4
lcom 0
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A execute() 0 18 2
A log() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of phpDocumentor.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    Mike van Riel <[email protected]>
11
 * @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com)
12
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
13
 * @link      http://phpdoc.org
14
 */
15
16
namespace phpDocumentor\Parser\Middleware;
17
18
use Exception;
19
use phpDocumentor\Reflection\Middleware\Command;
20
use phpDocumentor\Reflection\Middleware\Middleware;
21
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
22
use Psr\Log\LoggerInterface;
23
use Psr\Log\LogLevel;
24
25
final class ErrorHandlingMiddleware implements Middleware
26
{
27
    private $logger;
28
29 2
    public function __construct(LoggerInterface $logger)
30
    {
31 2
        $this->logger = $logger;
32 2
    }
33
34
    /**
35
     * @return object
36
     */
37 2
    public function execute(Command $command, callable $next)
38
    {
39 2
        assert($command instanceof CreateCommand);
40
41 2
        $filename = $command->getFile()->path();
42 2
        $this->log('Starting to parse file: ' . $filename, LogLevel::INFO);
43
44
        try {
45 2
            return $next($command);
46 1
        } catch (Exception $e) {
47 1
            $this->log(
48 1
                '  Unable to parse file "' . $filename . '", an error was detected: ' . $e->getMessage(),
49 1
                LogLevel::ALERT
50
            );
51
        }
52
53 1
        return null;
54
    }
55
56
    /**
57
     * Dispatches a logging request.
58
     */
59 2
    private function log(string $message, string $priority = LogLevel::INFO, array $parameters = [])
60
    {
61 2
        $this->logger->log($priority, $message, $parameters);
62 2
    }
63
}
64