Passed
Push — dev ( 043eb4...bf3609 )
by Dispositif
06:23
created

TemplateConverter   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
dl 0
loc 109
rs 10
c 1
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convertDateForArticle() 0 18 2
A ArticleFromOuvrage() 0 21 4
A convertDataOuvrage2Article() 0 19 4
A hasNeededParams() 0 12 3
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © 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
11
namespace App\Domain\Models\Wiki;
12
13
use Exception;
14
15
/**
16
 * Convert wiki-template in another wiki-template.
17
 * Class TemplateConverter
18
 *
19
 * @package App\Domain\Models\Wiki
20
 */
21
class TemplateConverter
22
{
23
    const PARAM_OUVRAGE_TO_ARTICLE
24
        = [
25
            'tome' => 'volume',
26
            'numéro chapitre' => 'numéro',
27
            'pages totales' => 'pages',
28
        ];
29
30
    /**
31
     * Conversion {ouvrage} en {article}.
32
     * todo Move factory ?
33
     *
34
     * @param OuvrageTemplate $ouvrage
35
     *
36
     * @return ArticleTemplate|null
37
     * @throws Exception
38
     */
39
    public static function ArticleFromOuvrage(OuvrageTemplate $ouvrage): ?ArticleTemplate
40
    {
41
        $article = \App\Domain\WikiTemplateFactory::create('article');
42
        try {
43
            $data = self::convertDataOuvrage2Article($ouvrage->toArray());
44
45
            $article->hydrate($data);
46
47
            $articleInfos = array_merge(['ConvertFromOuvrage' => 1], $ouvrage->getInfos());
48
            $article->setInfos($articleInfos);
3 ignored issues
show
Bug introduced by
The method setInfos() does not exist on App\Domain\Models\Wiki\LienBriseTemplate. ( Ignorable by Annotation )

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

48
            $article->/** @scrutinizer ignore-call */ 
49
                      setInfos($articleInfos);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setInfos() does not exist on App\Domain\Models\Wiki\GoogleLivresTemplate. ( Ignorable by Annotation )

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

48
            $article->/** @scrutinizer ignore-call */ 
49
                      setInfos($articleInfos);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method setInfos() does not exist on App\Domain\Models\Wiki\LienWebTemplate. ( Ignorable by Annotation )

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

48
            $article->/** @scrutinizer ignore-call */ 
49
                      setInfos($articleInfos);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
49
        } catch (\Throwable $e) {
50
            return null;
51
        }
52
53
        if ($article instanceof ArticleTemplate
54
            && self::hasNeededParams($article)
55
        ) {
56
            return $article;
57
        }
58
59
        return null;
60
    }
61
62
    /**
63
     * TODO : refac/move in AbstractTemplate
64
     *
65
     * @param ArticleTemplate $article
66
     *
67
     * @return bool
68
     * @throws Exception
69
     */
70
    public static function hasNeededParams(ArticleTemplate $article): bool
71
    {
72
        $needed = ['titre', 'périodique', 'date'];
73
        foreach ($needed as $need) {
74
            if (empty($article->getParam($need))) {
75
                echo "Article : paramètre obligatoire '$need' manquant";
76
77
                return false;
78
            }
79
        }
80
81
        return true;
82
    }
83
84
    /**
85
     * Convert param names between wiki-templates.
86
     *
87
     * @param array $ouvrageData
88
     *
89
     * @return array
90
     */
91
    private static function convertDataOuvrage2Article(array $ouvrageData): array
92
    {
93
        $data = [];
94
        foreach ($ouvrageData as $param => $value) {
95
            // Conversion du nom de paramètre
96
            // + esquive des doublons qui supprimeraient une value
97
            if (key_exists($param, self::PARAM_OUVRAGE_TO_ARTICLE)
98
                && !key_exists(self::PARAM_OUVRAGE_TO_ARTICLE[$param], $ouvrageData)
99
            ) {
100
                $data[self::PARAM_OUVRAGE_TO_ARTICLE[$param]] = $value;
101
                continue;
102
            }
103
            // Sinon : on conserve param/value, même si param invalide dans Article
104
            $data[$param] = $value;
105
        }
106
107
        $data = self::convertDateForArticle($data);
108
109
        return $data;
110
    }
111
112
    public static function convertDateForArticle(array $data): array
113
    {
114
        // generate 'date'
115
        if (empty($data['date'])) {
116
            $data['date'] = trim(
117
                sprintf(
118
                    '%s %s %s',
119
                    $data['jour'] ?? '',
120
                    $data['mois'] ?? '',
121
                    $data['année'] ?? ''
122
                )
123
            );
124
            unset($data['jour']);
125
            unset($data['mois']);
126
            unset($data['année']);
127
        }
128
129
        return $data;
130
    }
131
}
132