Passed
Branch master (f55b85)
by Dispositif
03:58 queued 01:26
created

PublisherLogicTrait   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 38
c 1
b 0
f 0
dl 0
loc 81
rs 10
wmc 17

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isScientificDomain() 0 6 2
C replaceSitenameByConfig() 0 34 13
A importConfigAndData() 0 22 2
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\ExternLink;
11
12
use App\Domain\Models\Wiki\ArticleTemplate;
13
use App\Domain\Models\Wiki\LienWebTemplate;
14
use App\Domain\Models\Wiki\OuvrageTemplate;
15
use App\Domain\Utils\WikiTextUtil;
16
use Symfony\Component\Yaml\Yaml;
17
18
trait PublisherLogicTrait
19
{
20
    /**
21
     * todo extract Infra getcont form file + inject
22
     */
23
    protected function importConfigAndData(): void
24
    {
25
        // todo REFAC DataObject[]
26
        $this->config = Yaml::parseFile(self::CONFIG_PRESSE);
1 ignored issue
show
Bug introduced by
The constant App\Domain\ExternLink\Pu...gicTrait::CONFIG_PRESSE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug Best Practice introduced by
The property config does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27
        $skipFromFile = file(
28
            self::SKIP_DOMAIN_FILENAME,
1 ignored issue
show
Bug introduced by
The constant App\Domain\ExternLink\Pu...t::SKIP_DOMAIN_FILENAME was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
29
            FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
30
        );
31
        $this->skip_domain = $skipFromFile ?: [];
1 ignored issue
show
Bug Best Practice introduced by
The property skip_domain does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
32
33
        $this->publisherData['newspaper'] = json_decode(file_get_contents(self::CONFIG_NEWSPAPER_JSON), true, 512, JSON_THROW_ON_ERROR);
2 ignored issues
show
Bug Best Practice introduced by
The property publisherData does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
Bug introduced by
The constant App\Domain\ExternLink\Pu...::CONFIG_NEWSPAPER_JSON was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
34
        $this->publisherData['scientific domain'] = json_decode(
35
            file_get_contents(self::CONFIG_SCIENTIFIC_JSON),
1 ignored issue
show
Bug introduced by
The constant App\Domain\ExternLink\Pu...:CONFIG_SCIENTIFIC_JSON was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
            true,
37
            512,
38
            JSON_THROW_ON_ERROR
39
        );
40
        $this->publisherData['scientific wiki'] = json_decode(
41
            file_get_contents(self::CONFIG_SCIENTIFIC_WIKI_JSON),
1 ignored issue
show
Bug introduced by
The constant App\Domain\ExternLink\Pu...IG_SCIENTIFIC_WIKI_JSON was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42
            true,
43
            512,
44
            JSON_THROW_ON_ERROR
45
        );
46
    }
47
48
    /**
49
     * TODO rename/move Postprocess data site/périodique
50
     * Logique : remplacement titre périodique ou nom du site
51
     *
52
     * @param array $mapData
53
     * @param       $template
54
     *
55
     * @return array
56
     */
57
    protected function replaceSitenameByConfig(array $mapData, $template): array
58
    {
59
        // from wikidata URL of newspapers
60
        if (!empty($this->publisherData['newspaper'][$this->registrableDomain])) {
61
            $frwiki = $this->publisherData['newspaper'][$this->registrableDomain]['frwiki'];
62
            $label = $this->publisherData['newspaper'][$this->registrableDomain]['fr'];
63
            if (isset($mapData['site']) || $template instanceof LienWebTemplate) {
64
                $mapData['site'] = WikiTextUtil::wikilink($label, $frwiki);
65
            }
66
            if (isset($mapData['périodique']) || $template instanceof ArticleTemplate) {
67
                $mapData['périodique'] = WikiTextUtil::wikilink($label, $frwiki);
68
            }
69
        }
70
71
        // from wikidata of scientific journals
72
        if (isset($mapData['périodique']) && isset($this->publisherData['scientific wiki'][$mapData['périodique']])) {
73
            $mapData['périodique'] = WikiTextUtil::wikilink(
74
                $mapData['périodique'],
75
                $this->publisherData['scientific wiki'][$mapData['périodique']]
76
            );
77
        }
78
79
        // from YAML config
80
        if (!empty($this->config[$this->registrableDomain]['site']) && $template instanceof LienWebTemplate) {
81
            $mapData['site'] = $this->config[$this->registrableDomain]['site'];
82
        }
83
        if (!empty($this->config[$this->registrableDomain]['périodique'])
84
            && (!empty($mapData['périodique'])
85
                || $template instanceof OuvrageTemplate)
86
        ) {
87
            $mapData['périodique'] = $this->config[$this->registrableDomain]['périodique'];
88
        }
89
90
        return $mapData;
91
    }
92
93
    protected function isScientificDomain(): bool
94
    {
95
        if (isset($this->publisherData['scientific domain'][$this->registrableDomain])) {
96
            return true;
97
        }
98
        return strpos('.revues.org', $this->registrableDomain) > 0;
99
    }
100
}