Passed
Branch master (61ab03)
by Dispositif
05:16 queued 02:29
created

ExternMapper::postProcess()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 6
nc 4
nop 1
dl 0
loc 13
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Publisher;
11
12
use App\Domain\Utils\ArrayProcessTrait;
13
use Psr\Log\LoggerInterface;
14
15
/**
16
 * Generic mapper for press/revue article on web.
17
 * Using JSON-LD and meta tags to obtain {article} data.
18
 * Generic mapper for web pages URL to wiki-template references.
19
 * Converting to {article}, {lien web} or {lien brisé}
20
 * Using JSON-LD, Open Graph and Dublin Core meta extracted from HTML.
21
 * Class ExternMapper
22
 *
23
 * @package App\Domain\Publisher
24
 */
25
class ExternMapper implements MapperInterface
26
{
27
    use ArrayProcessTrait, ExternConverterTrait;
28
29
    /**
30
     * @var LoggerInterface
31
     */
32
    private $log;
33
34
    public function __construct(LoggerInterface $log)
35
    {
36
        $this->log = $log;
37
    }
38
39
    public function process($data): array
40
    {
41
        $dat = $this->processMapping($data);
42
43
        return (!empty($dat)) ? $this->postProcess($dat) : [];
44
    }
45
46
    protected function processMapping($data): array
47
    {
48
        $mapJson = [];
49
        $mapMeta = [];
50
        if (!empty($data['JSON-LD'])) {
51
            $mapJson = $this->processJsonLDMapping($data['JSON-LD']);
52
        }
53
        if (!empty($data['meta'])) {
54
            $mapMeta = (new OpenGraphMapper())->process($data['meta']);
55
        }
56
57
        // langue absente JSON-LD mais array_merge risqué (doublon)
58
        if (!empty($mapJson)) {
59
            if (!isset($mapJson['langue']) && isset($mapMeta['langue'])) {
60
                $mapJson['langue'] = $mapMeta['langue'];
61
                $mapJson['DATA-TYPE'] = 'JSON-LD+META';
62
            }
63
64
            return $mapJson;
65
        }
66
67
        return $mapMeta;
68
    }
69
70
    /**
71
     * todo move to mapper ?
72
     *
73
     * @param array $LDdata
74
     *
75
     * @return array
76
     */
77
    private function processJsonLDMapping(array $LDdata): array
78
    {
79
        if ($this->checkJSONLD($LDdata)) {
80
            return (new JsonLDMapper())->process($LDdata);
81
        }
82
        // gestion des multiples objets comme Figaro
83
        foreach ($LDdata as $dat) {
84
            if (is_array($dat) && $this->checkJSONLD($dat)) {
85
                return (new JsonLDMapper())->process($dat);
86
            }
87
        }
88
89
        return [];
90
    }
91
92
    protected function checkJSONLD(array $jsonLD): bool
93
    {
94
        return isset($jsonLD['headline']) && isset($jsonLD['@type']);
95
    }
96
97
    /**
98
     * todo Refac/move domain special mapping
99
     * todo Config parameter for post-process
100
     *
101
     * @param array $dat
102
     *
103
     * @return array
104
     */
105
    protected function postProcess(array $dat): array
106
    {
107
        $dat = $this->deleteEmptyValueArray($dat);
108
        if (isset($dat['langue']) && 'fr' === $dat['langue']) {
109
            unset($dat['langue']);
110
        }
111
112
        // Ça m'énerve ! Gallica met "vidéo" pour livre numérisé
113
        if (isset($dat['site']) && $dat['site'] === 'Gallica') {
114
            unset($dat['format']);
115
        }
116
117
        return $dat;
118
    }
119
}
120