GitterMessage   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 5
dl 0
loc 68
ccs 0
cts 32
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
A parseMentions() 0 16 3
A getUserFromMessage() 0 12 1
1
<?php
2
/**
3
 * This file is part of Platform package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Karma\System\Gitter;
11
12
use Carbon\Carbon;
13
use Karma\Platform\Io\AbstractSystem;
14
use Karma\Platform\Io\SystemInterface;
15
use Karma\Platform\Io\UserInterface;
16
use Karma\Platform\Io\AbstractMessage;
17
use Karma\Platform\Io\ChannelInterface;
18
19
/**
20
 * Class GitterMessage
21
 * @package Karma\System\Gitter
22
 */
23
class GitterMessage extends AbstractMessage
24
{
25
    /**
26
     * 5 * 60 sec = 5 min
27
     */
28
    protected const MESSAGE_EDIT_TIMEOUT = 5 * 60;
29
30
    /**
31
     * GitterMessage constructor.
32
     * @param ChannelInterface $channel
33
     * @param array $data
34
     */
35
    public function __construct(ChannelInterface $channel, array $data)
36
    {
37
        /** @var GitterSystem|AbstractSystem $system */
38
        $system   = $channel->getSystem();
39
        $mentions = $this->parseMentions($system, (array)($data['mentions'] ?? []));
40
        $user     = $this->getUserFromMessage($system, $data['fromUser']);
41
        $body     = $system->getTransformer()->parse($data['html'], $mentions);
42
43
        $system->info($data['html'] . "\n" . '   -> ' . "\n" . $body);
44
45
        parent::__construct($channel, $user, $data['id'], $body);
46
47
        $this->createdAt = Carbon::parse($data['sent']);
48
        $this->mentions  = $mentions;
49
    }
50
51
    /**
52
     * @param SystemInterface|GitterSystem $system
53
     * @param array $data
54
     * @return array
55
     */
56
    private function parseMentions(SystemInterface $system, array $data): array
57
    {
58
        $mentions = [];
59
60
        foreach ($data as $mention) {
61
            if (!isset($mention['userId'])) {
62
                continue;
63
            }
64
65
            $mentions[] = $system->getUser($mention['userId'], function () use ($system, $mention) {
66
                return new GitterUser($system, $mention);
67
            });
68
        }
69
70
        return $mentions;
71
    }
72
73
    /**
74
     * @param SystemInterface|GitterSystem $system
75
     * @param array $data
76
     * @return UserInterface
77
     */
78
    private function getUserFromMessage(SystemInterface $system, array $data): UserInterface
79
    {
80
        /** @var GitterUser $user */
81
        $user = $system->getUser($data['id'], function () use ($system, $data): UserInterface {
82
            return new GitterUser($system, $data);
83
        });
84
85
        $user->rename($data['username']);
86
        $user->setAvatar($data['avatarUrl'] ?? null);
87
88
        return $user;
89
    }
90
}
91