Test Failed
Push — master ( 2eb953...f148c7 )
by Dispositif
08:32
created

OpenGraphMapper::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 4
ccs 1
cts 1
cp 1
crap 3
rs 10
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © Philippe/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
11
namespace App\Domain\Publisher;
12
13
14
use Exception;
15
16
class OpenGraphMapper implements MapperInterface
17
{
18
    use ExternConverterTrait;
19
20
    /**
21
     * Tenir compte du <title>bla</title> pour générer un {lien web} ?
22
     */
23
    protected $htmlTitleAllowed = true;
24
25
    protected $titleFromHtmlState = false;
26
27
    /**
28
     * @param array|null $options
29
     */
30
    public function __construct(?array $options = [])
31 5
    {
32
        if (!empty($options['htmlTitleAllowed']) && is_bool($options['htmlTitleAllowed'])) {
33
            $this->htmlTitleAllowed = $options['htmlTitleAllowed'];
34 5
        }
35 5
    }
36 5
37 5
    /**
38 5
     * todo pretty ALLCAP UGLY TITLE
39 5
     * Mapping from Open Graph and Dublin Core meta tags
40 5
     * https://ogp.me/
41
     * https://www.dublincore.org/schemas/
42 5
     * todo extract DC ?
43 5
     *
44 5
     * @param array $meta
45
     *
46 5
     * @return array
47 5
     * @throws Exception
48 5
     */
49 5
    public function process($meta): array
50
    {
51 5
        // Mode "pas de titre html"
52
        if (!$this->htmlTitleAllowed && !empty($meta['html-title'])) {
53
            unset($meta['html-title']);
54 5
        }
55 5
56 5
        // si usage <title> HTML
57 5
        if ($this->htmlTitleAllowed
58
            && empty($meta['og:title'])
59 5
            && empty($meta['twitter:title'])
60 5
            && empty($meta['DC.title'])
61 5
            && !empty($meta['html-title'])
62 5
        ) {
63
            $this->titleFromHtmlState = true;
64
        }
65
66 5
        return [
67 5
            'DATA-TYPE' => 'Open Graph/Dublin Core',
68 5
            'DATA-ARTICLE' => $this->isAnArticle($meta['og:type'] ?? ''),
69 5
            'site' => $this->clean($meta['og:site_name'] ?? null),
70 5
            'titre' => $this->clean(
71 5
                $meta['og:title'] ?? $meta['twitter:title'] ?? $meta['DC.title'] ?? $meta['html-title'] ?? null
72 5
            ),
73 5
            'url' => $meta['og:url'] ?? $meta['URL'] ?? $meta['html-url'] ?? null,
74
            'langue' => $this->convertLangue(
75
                $meta['og:locale'] ?? $meta['DC.language'] ?? $meta['citation_language'] ?? $meta['lang'] ??
76
                $meta['language'] ??
77
                $meta['content-language'] ?? $meta['Content-Language'] ?? $meta['html-lang'] ?? null
78
            ),
79
            'consulté le' => date('d-m-Y'),
80
            'auteur' => $this->cleanAuthor(
81
                $meta['og:article:author'] ??
82
                $meta['article:author'] ?? $meta['citation_author'] ?? $meta['article:author_name'] ?? null
83
            ),
84
            'format' => $this->convertOGtype2format($meta['og:type'] ?? null),
85
            'date' => $this->convertDate(
86
                $meta['og:article:published_time'] ?? $meta['article:published_time'] ??
87
                $meta['DC.date'] ?? $meta['citation_date'] ?? $meta['citation_publication_date'] ?? null
88
            ),
89
            'accès url' => $this->convertURLaccess($meta),
90
91
            // DUBLIN CORE ONLY
92
            'périodique' => $this->clean($meta['DC.isPartOf'] ?? $meta['citation_journal_title'] ?? null),
93
            'et al.' => $this->authorsEtAl(
94
                $meta['citation_authors'] ?? $meta['DC.Contributor'] ?? null,
95
                true
96
            ),
97
            'auteur1' => $this->wikifyPressAgency(
98
                $this->cleanAuthor(
99
                    $this->authorsEtAl($meta['citation_authors'] ?? $meta['DC.Contributor'] ?? $meta['Author'] ?? null)
100
                )
101
            ),
102
            'volume' => $meta['citation_volume'] ?? null,
103
            'numéro' => $meta['citation_issue'] ?? null,
104
            'page' => $this->convertDCpage($meta),
105
            'doi' => $meta['citation_doi'] ?? $meta['DOI'] ?? null,
106
            'éditeur' => $meta['DC.publisher'] ?? $meta['dc.publisher'] ?? null, // Persée dégeulasse todo?
107
            'pmid' => $meta['citation_pmid'] ?? null,
108
            'issn' => $meta["citation_issn"] ?? null,
109
            'isbn' => $meta["citation_isbn"] ?? null,
110
            // "prism.eIssn" => "2262-7197"
111
112
            // WIKI
113
            //'note' => $this->addNote(),
114
        ];
115
    }
116
}
117