Passed
Branch master (309757)
by Dispositif
03:20 queued 54s
created

TalkPageEditTrait::sendOuvrageErrorsOnTalkPage()   B

Complexity

Conditions 8
Paths 98

Size

Total Lines 52
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 52
rs 8.1315
c 0
b 0
f 0
cc 8
nc 98
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
11
namespace App\Application\OuvrageEdit;
12
13
use App\Infrastructure\ServiceFactory;
14
use Psr\Log\LoggerInterface;
15
use Psr\Log\NullLogger;
16
use Throwable;
17
18
trait TalkPageEditTrait
19
{
20
    /**
21
     * todo extract to class ?
22
     *
23
     * @param array                $rows Collection of citations
24
     * @param LoggerInterface|null $log
25
     *
26
     * @return bool
27
     */
28
    private function sendOuvrageErrorsOnTalkPage(array $rows, LoggerInterface $log = null): bool
29
    {
30
        if ($log === null) {
31
            $log = new NullLogger();
32
        }
33
        if (empty($rows[0]) || empty($rows[0]['page'])) {
34
            return false;
35
        }
36
        $mainTitle = $rows[0]['page'];
37
        $log->notice("** Send Error Message on talk page. Wait 3...");
38
        sleep(3);
39
40
        // format wiki message
41
        $errorList = '';
42
        foreach ($this->pageWorkStatus->errorWarning[$mainTitle] as $error) {
43
            $errorList .= sprintf("* <span style=\"background:#FCDFE8\"><nowiki>%s</nowiki></span> \n", $error);
44
        }
45
46
        $diffStr = '';
47
        try {
48
            // get last bot revision ID
49
            $main = ServiceFactory::wikiPageAction($mainTitle, true);
50
            if (getenv('BOT_NAME') === $main->getLastRevision()->getUser()) {
51
                $id = $main->getLastRevision()->getId();
52
                $diffStr = sprintf(
53
                    ' ([https://fr.wikipedia.org/w/index.php?title=%s&diff=%s diff])',
54
                    str_replace(' ', '_', $mainTitle),
55
                    $id
56
                );
57
            }
58
        } catch (Throwable $e) {
59
            unset($e);
60
        }
61
62
        $errorCategoryName = sprintf('Signalement %s', getenv('BOT_NAME'));
63
64
        $errorMessage = file_get_contents(self::ERROR_MSG_TEMPLATE);
1 ignored issue
show
Bug introduced by
The constant App\Application\OuvrageE...ait::ERROR_MSG_TEMPLATE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
65
        $errorMessage = str_replace('##CATEGORY##', $errorCategoryName, $errorMessage);
66
        $errorMessage = str_replace('##ERROR LIST##', trim($errorList), $errorMessage);
67
        $errorMessage = str_replace('##ARTICLE##', $mainTitle, $errorMessage);
68
        $errorMessage = str_replace('##DIFF##', $diffStr, $errorMessage);
69
70
        // Edit wiki talk page
71
        try {
72
            $talkPage = ServiceFactory::wikiPageAction('Discussion:'.$mainTitle);
73
            $editInfo = ServiceFactory::editInfo('🍄 Signalement erreur {ouvrage}', false, false, 5); // 💩
74
75
            return $talkPage->addToBottomOrCreatePage("\n".$errorMessage, $editInfo);
76
        } catch (Throwable $e) {
77
            $log->warning('Exception after addToBottomOrCreatePage() '.$e->getMessage());
78
79
            return false;
80
        }
81
    }
82
}
83