Passed
Push — master ( d644e9...e142f8 )
by Dispositif
15:55
created

LocationHandler::findFrenchTranslation()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
c 0
b 0
f 0
dl 0
loc 11
rs 9.6111
cc 5
nc 3
nop 1
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\WikiOptimizer\Handlers;
11
12
use App\Domain\InfrastructurePorts\PageListInterface;
13
use App\Domain\Models\Wiki\OuvrageTemplate;
14
use App\Domain\OptiStatus;
15
use App\Domain\Utils\TextUtil;
16
use App\Domain\Utils\WikiTextUtil;
17
use Exception;
18
19
/**
20
 * TODO Fix unit test depending on CSV file.
21
 */
22
class LocationHandler extends AbstractOuvrageHandler
23
{
24
    final public const TRANSLATE_CITY_FR = __DIR__ . '/../../resources/traduction_ville.csv';
25
26
    public function __construct(OuvrageTemplate $ouvrage, OptiStatus $optiStatus, protected PageListInterface $pageListManager)
27
    {
28
        parent::__construct($ouvrage, $optiStatus);
29
    }
30
31
    /**
32
     * Todo: injection dep.
33
     * Todo : "[s. l.]" sans lieu "s.l.n.d." sans lieu ni date.
34
     * @throws Exception
35
     */
36
    public function handle(): void
37
    {
38
        $location = $this->getParam('lieu');
39
        if (empty($location)) {
40
            return;
41
        }
42
        $memo = $location;
43
44
        $location = WikiTextUtil::unWikify($location);
45
        $location = TextUtil::mb_ucfirst($location);
46
        $location = $this->keepOnlyFirstCity($location);
47
        $location = $this->findFrenchTranslation($location);
48
        $location = trim($location);
49
50
        if ($memo !== $location) {
51
            $this->setParam('lieu', $location);
52
            $this->addSummaryLog('±lieu');
53
            $this->optiStatus->setNotCosmetic(true);
54
        }
55
    }
56
57
    protected function keepOnlyFirstCity(string $location): string
58
    {
59
        if (str_contains($location, '/')) {
60
            $location = explode('/', $location)[0];
61
        }
62
        return $location;
63
    }
64
65
    /**
66
     * Translation of famous cities from CSV file : "London"->"Londres"
67
     */
68
    protected function findFrenchTranslation(string $location): string
69
    {
70
        if (!method_exists($this->pageListManager, 'findCSVline')) {
71
            return $location;
72
        }
73
        $row = $this->pageListManager->findCSVline(self::TRANSLATE_CITY_FR, $location);
0 ignored issues
show
Bug introduced by
The method findCSVline() does not exist on App\Domain\InfrastructurePorts\PageListInterface. It seems like you code against a sub-type of App\Domain\InfrastructurePorts\PageListInterface such as App\Infrastructure\FileManager. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        /** @scrutinizer ignore-call */ 
74
        $row = $this->pageListManager->findCSVline(self::TRANSLATE_CITY_FR, $location);
Loading history...
74
        if ($row !== [] && !empty($row[1]) && is_string($row[1])) {
75
            $location = $row[1];
76
        }
77
78
        return $location;
79
    }
80
}
81