Passed
Push — master ( 1bcf8b...aa70d7 )
by Dispositif
05:36
created

ServiceFactory::wikiPageAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 0
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 : 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
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
    /**
99
     * todo? replace that singleton pattern ??? (multi-lang wiki?).
100
     *
101
     * @param bool|null $forceLogin
102
     *
103
     * @return MediawikiFactory
104
     * @throws UsageException
105
     */
106
    public static function wikiApi(?bool $forceLogin = false): MediawikiFactory
107
    {
108
        if (isset(self::$wikiApi) && !$forceLogin) {
109
            return self::$wikiApi;
110
        }
111
112
        self::$api = new MediawikiApi(getenv('WIKI_API_URL'));
113
        self::$api->login(
114
            new ApiUser(getenv('WIKI_API_USERNAME'), getenv('WIKI_API_PASSWORD'))
115
        );
116
117
        self::$wikiApi = new MediawikiFactory(self::$api);
118
119
        return self::$wikiApi;
120
    }
121
122
    //    /**
123
    //     * @return DbAdapter
124
    //     */
125
    //    public static function sqlConnection(): DbAdapter
126
    //    {
127
    //        if (isset(self::$dbConnection)) {
128
    //            return self::$dbConnection;
129
    //        }
130
    //        self::$dbConnection = new DbAdapter();
131
    //
132
    //        return self::$dbConnection;
133
    //    }
134
135
    /**
136
     * @param string $title
137
     * @param bool   $forceLogin
138
     *
139
     * @return WikiPageAction
140
     * @throws UsageException
141
     * @throws Exception
142
     */
143
    public static function wikiPageAction(string $title, $forceLogin = false): WikiPageAction
144
    {
145
        $wiki = self::wikiApi($forceLogin);
146
147
        return new WikiPageAction($wiki, $title);
148
    }
149
150
    public static function editInfo($summary = '', $minor = false, $bot = false, $maxLag = 5)
151
    {
152
        return new EditInfo($summary, $minor, $bot, $maxLag);
153
    }
154
155
    public static function httpClient(?array $option = null): ClientInterface
156
    {
157
        $option = $option ?? [];
158
        $defaultOption = [
159
            'timeout' => 60,
160
            'allow_redirects' => true,
161
            'headers' => ['User-Agent' => getenv('USER_AGENT')],
162
            'verify' => false, // CURLOPT_SSL_VERIFYHOST
163
            //                'proxy'           => '192.168.16.1:10',
164
        ];
165
        $option = array_merge($defaultOption, $option);
166
167
        return new Client($option);
168
    }
169
170
}
171