Completed
Push — master ( 6c4efe...df4b6e )
by Tobias
08:17
created

FakeIpPlugin   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handleQuery() 0 13 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the BazingaGeocoderBundle 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 Bazinga\GeocoderBundle\Plugin;
14
15
use Geocoder\Plugin\Plugin;
16
use Geocoder\Query\GeocodeQuery;
17
use Geocoder\Query\Query;
18
19
/**
20
 * Replace local IP with something better.
21
 *
22
 * @author Tobias Nyholm <[email protected]>
23
 */
24
class FakeIpPlugin implements Plugin
25
{
26
    /**
27
     * @var string
28
     */
29
    private $needle;
30
31
    /**
32
     * @var string
33
     */
34
    private $replacement;
35
36
    /**
37
     * @param string $needle
38
     * @param string $replacement
39
     */
40
    public function __construct(string $needle, string $replacement)
41
    {
42
        $this->needle = $needle;
43
        $this->replacement = $replacement;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49
    public function handleQuery(Query $query, callable $next, callable $first)
50
    {
51
        if (!$query instanceof GeocodeQuery) {
52
            return $next($query);
53
        }
54
55
        $text = str_replace($this->needle, $this->replacement, $query->getText(), $count);
56
        if ($count > 0) {
57
            $query = $query->withText($text);
58
        }
59
60
        return $next($query);
61
    }
62
}
63