Passed
Push — 2.x ( 92ec38...c080f0 )
by Terry
02:07
created

RocketChat::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
/*
3
 * This file is part of the Shieldon package.
4
 *
5
 * (c) Terry L. <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
declare(strict_types=1);
12
13
namespace Shieldon\Firewall\Panel\Sandbox;
14
15
use Shieldon\Messenger as Messenger;
16
17
/**
18
 * The sandbox for RocketChat.
19
 */
20
class RocketChat
21
{
22
    /**
23
     * Invoker.
24
     *
25
     * @param array $args
26
     *
27
     * @return bool
28
     */
29
    public function __invoke(array $args): bool
30
    {
31
        return $this->sandbox($args[0], $args[1]);
32
    }
33
34
    /**
35
     * Test RocketChat.
36
     *
37
     * @param array $getParams The GET params passed from tryMessenger method.
38
     * @param array $message   The message title and body.
39
     *
40
     * @return bool
41
     */
42
    private function sandbox($getParams, $message)
43
    {
44
        $serverUrl = $getParams['serverUrl'] ?? '';
45
        $userId = $getParams['userId'] ?? '';
46
        $accessToken = $getParams['accessToken'] ?? '';
47
        $channel = $getParams['channel'] ?? '';
48
49
        if (
50
            !empty($serverUrl) &&
51
            !empty($userId) &&
52
            !empty($accessToken) &&
53
            !empty($channel)
54
        ) {
55
            $messenger = new Messenger\RocketChat($accessToken, $userId, $serverUrl, $channel);
56
            if ($messenger->send($message['body'])) {
57
                return true;
58
            }
59
        }
60
        return false;
61
    }
62
}