Passed
Push — master ( 26e53d...a73e13 )
by Dispositif
09:44
created

CodexNotificationWorker::processSpecialActions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 13
rs 10
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
11
namespace App\Application;
12
13
14
use App\Infrastructure\DbAdapter;
15
use App\Infrastructure\Logger;
16
use App\Infrastructure\PageList;
17
use App\Infrastructure\ServiceFactory;
18
19
class CodexNotificationWorker extends NotificationWorker
20
{
21
    const PAGENAME_NOTIFICATION_LOG = 'Utilisateur:CodexBot/Notifications';
22
23
    /**
24
     * todo Refac that stupid idea :)
25
     * Delete article title in the log text file.
26
     *
27
     * @param $title
28
     */
29
    private function deleteEditedArticleFile(string $title): void
30
    {
31
        $text = file_get_contents(self::ARTICLE_ANALYZED_FILENAME);
32
        $newText = str_replace($title."\n", '', $text);
33
        if (!empty($text) && $text !== $newText) {
34
            @file_put_contents(self::ARTICLE_ANALYZED_FILENAME, $newText);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition for file_put_contents(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

34
            /** @scrutinizer ignore-unhandled */ @file_put_contents(self::ARTICLE_ANALYZED_FILENAME, $newText);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
35
        }
36
    }
37
38
    /**
39
     * Process external URL completion to wiki-template.
40
     *
41
     * @param string      $article
42
     * @param string|null $username
43
     */
44
    private function processExternLinks(string $article, ?string $username = '')
1 ignored issue
show
Unused Code introduced by
The parameter $username is not used and could be removed. ( Ignorable by Annotation )

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

44
    private function processExternLinks(string $article, /** @scrutinizer ignore-unused */ ?string $username = '')

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
45
    {
46
        try {
47
            $wiki = ServiceFactory::wikiApi();
48
            $logger = new Logger();
49
            //$logger->debug = true;
50
            $botConfig = new WikiBotConfig($logger);
51
            $botConfig->taskName = '🔔🌐 Amélioration de références : URL ⇒ ';
52
            new ExternRefWorker($botConfig, $wiki, new PageList([$article]));
53
54
            new GoogleBooksWorker($botConfig, $wiki, new PageList([$article]));
55
            sleep(10);
56
        } catch (\Throwable $e) {
57
            unset($e);
58
        }
59
    }
60
61
    /**
62
     * Process wikiSan for future {ouvrage} completion
63
     *
64
     * @param string $article
65
     */
66
    private function processWikiscanForOuvrage(string $article): void
67
    {
68
        try {
69
            $wiki = ServiceFactory::wikiApi();
70
            $list = new PageList([$article]);
71
            new ScanWiki2DB($wiki, new DbAdapter(), new WikiBotConfig(), $list, 15);
72
        } catch (\Throwable $e) {
73
            echo $e->getMessage();
74
        }
75
    }
76
77
    /**
78
     * @param $notif
79
     */
80
    protected function processSpecialActions($notif)
81
    {
82
        if (isset($notif['title']) && in_array($notif['title']['namespace'], ['', 'Discussion'])) {
83
            // PROCESS ARTICLES
84
            $article = $notif['title']['text'];
85
86
            // wikiScan for {ouvrage} completion
87
            $this->processWikiscanForOuvrage($article);
88
89
90
            // URL => wiki-template completion
91
            $this->deleteEditedArticleFile($article);
92
            $this->processExternLinks($article);
93
        }
94
    }
95
}
96