Completed
Push — master ( f93b5f...ceb78f )
by Tobias
01:19
created

LoggerPlugin   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 0
cbo 3
dl 0
loc 33
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handleQuery() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Plugin\Plugin;
14
15
use Geocoder\Collection;
16
use Geocoder\Exception\Exception;
17
use Geocoder\Query\Query;
18
use Psr\Log\LoggerInterface;
19
20
/**
21
 * Log all queries and the result/failure.
22
 *
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
class LoggerPlugin
26
{
27
    /**
28
     * @var LoggerInterface
29
     */
30
    private $logger;
31
32
    /**
33
     * @param LoggerInterface $logger
34
     */
35 2
    public function __construct(LoggerInterface $logger)
36
    {
37 2
        $this->logger = $logger;
38 2
    }
39
40 2
    public function handleQuery(Query $query, callable $next, callable $first)
0 ignored issues
show
Unused Code introduced by
The parameter $first is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42 2
        $startTime = microtime(true);
43 2
        $logger = $this->logger;
0 ignored issues
show
Unused Code introduced by
$logger 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...
44
45
        return $next($query)->then(function (Collection $result) use ($query, $startTime) {
46 1
            $duration = (microtime(true) - $startTime) * 1000;
47 1
            $this->logger->info(sprintf('[Geocoder] Got %d results in %0.2f ms for query %s', count($result), $duration, $query->__toString()));
48
49 1
            return $result;
50
        }, function (Exception $exception) use ($query, $startTime) {
51 1
            $duration = (microtime(true) - $startTime) * 1000;
52 1
            $this->logger->error(sprintf('[Geocoder] Failed with %s after %0.2f ms for query %s', get_class($exception), $duration, $query->__toString()));
53
54 1
            throw $exception;
55 2
        });
56
    }
57
}
58