Passed
Branch master (fd6b1a)
by Dispositif
03:51 queued 01:13
created

SameBookValidator::hasSameISBN()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 2
nop 0
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\Transformers\Validator;
11
12
use App\Domain\Models\Wiki\OuvrageTemplate;
13
use App\Domain\Transformers\OuvrageMixTrait;
14
use App\Domain\ValidatorInterface;
15
use App\Infrastructure\IsbnFacade;
16
17
class SameBookValidator implements ValidatorInterface
18
{
19
    use OuvrageMixTrait;
20
21
    /**
22
     * @var OuvrageTemplate
23
     */
24
    protected $origin;
25
    /**
26
     * @var OuvrageTemplate
27
     */
28
    protected $book;
29
30
    public function __construct(OuvrageTemplate $origin, OuvrageTemplate $book)
31
    {
32
        $this->origin = $origin;
33
        $this->book = $book;
34
    }
35
36
    public function validate(): bool
37
    {
38
        $hasSameAuthors = (new SameAuthorValidator($this->origin, $this->book))->validate();
39
40
        if ($this->hasSameISBN() && ($this->hasSameBookTitles() || $hasSameAuthors)) {
41
            return true;
42
        }
43
        return $this->hasSameBookTitles() && $hasSameAuthors;
44
    }
45
46
    private function hasSameISBN(): bool
47
    {
48
        if (!$this->origin->hasParamValue('isbn') || !$this->book->hasParamValue('isbn')) {
49
            return false;
50
        }
51
52
        $isbn1 = IsbnFacade::isbn2ean($this->origin->getParam('isbn'));
0 ignored issues
show
Bug introduced by
It seems like $this->origin->getParam('isbn') can also be of type null; however, parameter $isbn of App\Infrastructure\IsbnFacade::isbn2ean() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

52
        $isbn1 = IsbnFacade::isbn2ean(/** @scrutinizer ignore-type */ $this->origin->getParam('isbn'));
Loading history...
53
        $isbn2 = IsbnFacade::isbn2ean($this->book->getParam('isbn'));
54
55
        return $isbn1 === $isbn2;
56
    }
57
58
    private function hasSameBookTitles(): bool
59
    {
60
        $originBigTitle = $this->charsFromBigTitle($this->origin);
61
        $bookBigTitle = $this->charsFromBigTitle($this->book);
62
63
        if ($originBigTitle === $bookBigTitle) {
64
            return true;
65
        }
66
67
        // if there is only 2 chars of difference (i.e. typo error)
68
        // strlen for resource management
69
        if (strlen($originBigTitle) < 40 && strlen($bookBigTitle) < 40
70
            && levenshtein($originBigTitle, $bookBigTitle) <= 2
71
        ) {
72
            //            $this->log('typo titre?'); // TODO Normalize:: text from external API
73
74
            return true;
75
        }
76
77
        // si l'un des ouvrages ne comporte pas le sous-titre
78
        if (
79
            $this->stripAll($this->origin->getParam('titre'))
0 ignored issues
show
Bug introduced by
It seems like $this->origin->getParam('titre') can also be of type null; however, parameter $text of App\Domain\Transformers\...okValidator::stripAll() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
            $this->stripAll(/** @scrutinizer ignore-type */ $this->origin->getParam('titre'))
Loading history...
80
            === $this->stripAll($this->book->getParam('titre'))
81
        ) {
82
            return true;
83
        }
84
85
        // sous-titre inclus dans le titre
86
        // "Loiret : un département à l'élégance naturelle" <=> "Loiret"
87
        if (
88
            $this->stripAll($this->mainBookTitle($this->origin->getParam('titre')))
0 ignored issues
show
Bug introduced by
It seems like $this->origin->getParam('titre') can also be of type null; however, parameter $str of App\Domain\Transformers\...idator::mainBookTitle() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

88
            $this->stripAll($this->mainBookTitle(/** @scrutinizer ignore-type */ $this->origin->getParam('titre')))
Loading history...
89
            === $this->stripAll($this->mainBookTitle($this->origin->getParam('titre'))
90
            )
91
        ) {
92
            return true;
93
        }
94
        // titre manquant sur wiki
95
        return empty($originBigTitle);
96
    }
97
98
    /**
99
     * Give string before ":" (or same string if no ":").
100
     */
101
    private function mainBookTitle(string $str): string
102
    {
103
        if (($pos = mb_strpos($str, ':'))) {
104
            $str = trim(mb_substr($str, 0, $pos));
105
        }
106
107
        return $str;
108
    }
109
}