Passed
Push — master ( 27d925...adfd7e )
by Dispositif
02:31
created

OuvrageEditSummaryTrait   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 34
c 0
b 0
f 0
dl 0
loc 84
rs 10
wmc 15

6 Methods

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