Passed
Push — master ( 6b8380...9538c8 )
by Dispositif
02:38
created

ServiceFactory::editInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 4
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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\Infrastructure;
11
12
use App\Application\WikiPageAction;
13
use Exception;
14
use GuzzleHttp\Client;
15
use GuzzleHttp\ClientInterface;
16
use Mediawiki\Api\ApiUser;
17
use Mediawiki\Api\MediawikiApi;
18
use Mediawiki\Api\MediawikiFactory;
19
use Mediawiki\Api\UsageException;
20
use Mediawiki\DataModel\EditInfo;
21
use PhpAmqpLib\Channel\AMQPChannel;
22
use PhpAmqpLib\Connection\AMQPStreamConnection;
23
24
/**
25
 * Class ServiceFactory.
26
 */
27
class ServiceFactory
28
{
29
    /**
30
     * @var AMQPStreamConnection
31
     */
32
    private static $AMQPConnection;
33
34
    /**
35
     * @var MediawikiFactory
36
     */
37
    private static $wikiApi;
38
39
    //    private static $dbConnection;
40
41
    /**
42
     * @var MediawikiApi
43
     */
44
    private static $api;
45
46
    private function __construct()
47
    {
48
    }
49
50
    /**
51
     * AMQP queue (actual RabbitMQ)
52
     * todo $param
53
     * todo $channel->close(); $AMQPConnection->close();.
54
     *
55
     * @param string $queueName
56
     *
57
     * @return AMQPChannel
58
     */
59
    public static function queueChannel(string $queueName): AMQPChannel
60
    {
61
        if (!isset(self::$AMQPConnection)) {
62
            self::$AMQPConnection = new AMQPStreamConnection(
63
                getenv('AMQP_HOST'),
64
                getenv('AMQP_PORT'),
65
                getenv('AMQP_USER'),
66
                getenv('AMQP_PASSWORD'),
67
                getenv('AMQP_VHOST')
68
            );
69
        }
70
71
        $channel = self::$AMQPConnection->channel();
72
73
        $channel->queue_declare(
74
            $queueName,
75
            false,
76
            true, // won't be lost if MQ server restarts
77
            false,
78
            false
79
        );
80
81
        return $channel;
82
    }
83
84
    // --Commented out by Inspection START (21/04/2020 02:45):
85
    //    /**
86
    //     * @throws Exception
87
    //     */
88
    //    public static function closeAMQPconnection()
89
    //    {
90
    //        if (isset(self::$AMQPConnection)) {
91
    //            self::$AMQPConnection->close();
92
    //            self::$AMQPConnection = null;
93
    //        }
94
    //    }
95
    // --Commented out by Inspection STOP (21/04/2020 02:45)
96
97
    /**
98
     * @param bool|null $forceLogin
99
     *
100
     * @return MediawikiApi
101
     * @throws UsageException
102
     */
103
    public static function getMediawikiApi(?bool $forceLogin = false): MediawikiApi
104
    {
105
        if (isset(self::$api) && $forceLogin !== true) {
106
            return self::$api;
107
        }
108
        self::$api = new MediawikiApi(getenv('WIKI_API_URL'));
109
        self::$api->login(
110
            new ApiUser(getenv('WIKI_API_USERNAME'), getenv('WIKI_API_PASSWORD'))
111
        );
112
113
        return self::$api;
114
    }
115
116
    /**
117
     * todo rename getMediawikiFactory
118
     * todo? replace that singleton pattern ??? (multi-lang wiki?).
119
     *
120
     * @param bool|null $forceLogin
121
     *
122
     * @return MediawikiFactory
123
     * @throws UsageException
124
     */
125
    public static function getMediawikiFactory(?bool $forceLogin = false): MediawikiFactory
126
    {
127
        if (isset(self::$wikiApi) && !$forceLogin) {
128
            return self::$wikiApi;
129
        }
130
131
        $api = self::getMediawikiApi($forceLogin);
132
133
        self::$wikiApi = new MediawikiFactory($api);
134
135
        return self::$wikiApi;
136
    }
137
138
    /**
139
     * @param string $title
140
     * @param bool   $forceLogin
141
     *
142
     * @return WikiPageAction
143
     * @throws UsageException
144
     * @throws Exception
145
     */
146
    public static function wikiPageAction(string $title, $forceLogin = false): WikiPageAction
147
    {
148
        $wiki = self::getMediawikiFactory($forceLogin);
149
150
        return new WikiPageAction($wiki, $title);
151
    }
152
153
    public static function editInfo($summary = '', $minor = false, $bot = false, $maxLag = 5)
154
    {
155
        return new EditInfo($summary, $minor, $bot, $maxLag);
156
    }
157
158
    public static function httpClient(?array $option = null): ClientInterface
159
    {
160
        $option = $option ?? [];
161
        $defaultOption = [
162
            'timeout' => 60,
163
            'allow_redirects' => true,
164
            'headers' => ['User-Agent' => getenv('USER_AGENT')],
165
            'verify' => false, // CURLOPT_SSL_VERIFYHOST
166
            //                'proxy'           => '192.168.16.1:10',
167
        ];
168
        $option = array_merge($defaultOption, $option);
169
170
        return new Client($option);
171
    }
172
173
}
174