Issues (106)

src/Infrastructure/ServiceFactory.php (1 issue)

Labels
Severity
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\InfrastructurePorts\HttpClientInterface;
13
use App\Application\WikiPageAction;
14
use Exception;
15
use Mediawiki\Api\ApiUser;
16
use Mediawiki\Api\MediawikiApi;
17
use Mediawiki\Api\MediawikiFactory;
18
use Mediawiki\Api\UsageException;
19
use Mediawiki\DataModel\EditInfo;
20
use PhpAmqpLib\Channel\AMQPChannel;
21
use PhpAmqpLib\Connection\AMQPStreamConnection;
22
23
// TODO move into /Application
24
class ServiceFactory
25
{
26
    private static ?AMQPStreamConnection $AMQPConnection = null;
27
28
    private static ?MediawikiFactory $wikiApi = null;
29
30
    //    private static $dbConnection;
31
    private static ?MediawikiApi $api = null;
32
33
    private function __construct()
34
    {
35
    }
36
37
    /**
38
     * AMQP queue (actual RabbitMQ)
39
     * todo $param
40
     * todo $channel->close(); $AMQPConnection->close();.
41
     *
42
     *
43
     */
44
    public static function queueChannel(string $queueName): AMQPChannel
45
    {
46
        if (!isset(self::$AMQPConnection)) {
47
            self::$AMQPConnection = new AMQPStreamConnection(
48
                getenv('AMQP_HOST'),
49
                getenv('AMQP_PORT'),
50
                getenv('AMQP_USER'),
51
                getenv('AMQP_PASSWORD'),
52
                getenv('AMQP_VHOST')
53
            );
54
        }
55
56
        $channel = self::$AMQPConnection->channel();
0 ignored issues
show
The method channel() does not exist on null. ( Ignorable by Annotation )

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

56
        /** @scrutinizer ignore-call */ 
57
        $channel = self::$AMQPConnection->channel();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
58
        $channel->queue_declare(
59
            $queueName,
60
            false,
61
            true, // won't be lost if MQ server restarts
62
            false,
63
            false
64
        );
65
66
        return $channel;
67
    }
68
69
    // --Commented out by Inspection START (21/04/2020 02:45):
70
    //    /**
71
    //     * @throws Exception
72
    //     */
73
    //    public static function closeAMQPconnection()
74
    //    {
75
    //        if (isset(self::$AMQPConnection)) {
76
    //            self::$AMQPConnection->close();
77
    //            self::$AMQPConnection = null;
78
    //        }
79
    //    }
80
    // --Commented out by Inspection STOP (21/04/2020 02:45)
81
    /**
82
     * @throws UsageException
83
     */
84
    public static function getMediawikiApi(?bool $forceLogin = false): MediawikiApi
85
    {
86
        if (isset(self::$api) && $forceLogin !== true) {
87
            return self::$api;
88
        }
89
        self::$api = new MediawikiApi(getenv('WIKI_API_URL'));
90
        self::$api->login(
91
            new ApiUser(getenv('WIKI_API_USERNAME'), getenv('WIKI_API_PASSWORD'))
92
        );
93
94
        return self::$api;
95
    }
96
97
    /**
98
     * todo rename getMediawikiFactory
99
     * todo? replace that singleton pattern ??? (multi-lang wiki?).
100
     *
101
     *
102
     * @throws UsageException
103
     */
104
    public static function getMediawikiFactory(?bool $forceLogin = false): MediawikiFactory
105
    {
106
        if (isset(self::$wikiApi) && !$forceLogin) {
107
            return self::$wikiApi;
108
        }
109
110
        $api = self::getMediawikiApi($forceLogin);
111
112
        self::$wikiApi = new MediawikiFactory($api);
113
114
        return self::$wikiApi;
115
    }
116
117
    /**
118
     * @param bool $forceLogin
119
     *
120
     * @throws UsageException
121
     * @throws Exception
122
     */
123
    public static function wikiPageAction(string $title, $forceLogin = false): WikiPageAction
124
    {
125
        $wiki = self::getMediawikiFactory($forceLogin);
126
127
        return new WikiPageAction($wiki, $title);
128
    }
129
130
    public static function editInfo($summary = '', $minor = false, $bot = false, $maxLag = 5)
131
    {
132
        return new EditInfo($summary, $minor, $bot, $maxLag);
133
    }
134
135
    public static function getHttpClient(bool $torEnabled = false): HttpClientInterface
136
    {
137
        return HttpClientFactory::create($torEnabled);
138
    }
139
}
140