Passed
Branch dev3 (493baa)
by Dispositif
02:30
created

OuvrageEditSummaryTrait::generateFinalSummary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 17
rs 9.9
cc 1
nc 1
nop 0
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\Application\OuvrageEdit;
11
12
use DateTime;
13
use function App\Application\mb_substr;
0 ignored issues
show
introduced by
The function App\Application\mb_substr was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
15
/**
16
 * Used only by OuvrageEditWorker.
17
 */
18
trait OuvrageEditSummaryTrait
19
{
20
    /* Beware !! $importantSummary also defined in OuvrageEditWorker */
21
    public $importantSummary = [];
22
23
    /**
24
     * Generate wiki edition summary.
25
     */
26
    public function generateFinalSummary(): string
27
    {
28
        $prefix = $this->generatePrefix();
29
        $citeSummary = $this->getCiteSummary();
30
31
        $summary = sprintf(
32
            '%s [%s] %s %sx : %s',
33
            trim($prefix),
34
            str_replace('v', '', $this->pageWorkStatus->citationVersion),
35
            trim(self::TASK_NAME),
36
            $this->pageWorkStatus->nbRows,
37
            $citeSummary
38
        );
39
40
        $summary = $this->shrinkLongSummaryIfNoImportantDetailsToVerify($summary);
41
42
        return $this->couldAddLuckMessage($summary);
43
    }
44
45
    /**
46
     * Shrink long summary if no important details to verify.
47
     */
48
    protected function shrinkLongSummaryIfNoImportantDetailsToVerify(string $summary): string
49
    {
50
        if (empty($this->pageWorkStatus->importantSummary)) {
51
            $length = strlen($summary);
52
            $summary = mb_substr($summary, 0, 80);
53
            $summary .= ($length > strlen($summary)) ? '…' : '';
54
        } else {
55
            $summary .= '…'; // ?
56
        }
57
        return $summary;
58
    }
59
60
    protected function couldAddLuckMessage(string $summary): string
61
    {
62
        if (!$this->pageWorkStatus->luckyState && (new DateTime())->format('H:i') === '11:11') {
63
            $this->pageWorkStatus->luckyState = true;
64
            $summary .= self::LUCKY_MESSAGE;
65
        }
66
67
        return $summary;
68
    }
69
70
    protected function generatePrefix(): string
71
    {
72
        $prefix = ($this->pageWorkStatus->botFlag) ? 'bot ' : '';
73
        $prefix .= (empty($this->pageWorkStatus->errorWarning)) ? '' : ' ⚠️';
74
        $prefix .= (empty($this->pageWorkStatus->featured_article)) ? '' : ' ☆'; // AdQ, BA
75
76
        return $prefix;
77
    }
78
79
    /**
80
     * Generate list of details about current bot edition.
81
     */
82
    protected function getCiteSummary(): string
83
    {
84
        // basic modifs
85
        $citeSummary = implode(' ', $this->pageWorkStatus->citationSummary);
86
        // replaced by list of modifs to verify by humans
87
        if (!empty($this->pageWorkStatus->importantSummary)) {
88
            $citeSummary = implode(', ', $this->pageWorkStatus->importantSummary);
89
        }
90
        return $citeSummary;
91
    }
92
93
    /**
94
     * For substantive or ambiguous modifications done.
95
     *
96
     * @param string $tag
97
     */
98
    protected function addSummaryTag(string $tag)
99
    {
100
        if (!in_array($tag, $this->pageWorkStatus->importantSummary)) {
101
            $this->pageWorkStatus->importantSummary[] = $tag;
102
        }
103
    }
104
}
105