Completed
Push — master ( 772f65...48ab5c )
by Kirill
02:32
created

GitterMessage::parseMentions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 2
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->parseMessage($data['html'], $mentions);
42
43
        echo str_repeat('-', 50) . "\n";
44
        echo $data['html'] . "\n";
45
        echo '   -> ' . "\n";
46
        echo $body . "\n";
47
48
        parent::__construct($channel, $user, $data['id'], $body);
49
50
        $this->createdAt = Carbon::parse($data['sent']);
51
        $this->mentions  = $mentions;
52
    }
53
54
    /**
55
     * @param SystemInterface|GitterSystem $system
56
     * @param array $data
57
     * @return array
58
     */
59
    private function parseMentions(SystemInterface $system, array $data): array
60
    {
61
        $mentions = [];
62
63
        foreach ($data as $mention) {
64
            if (!isset($mention['userId'])) {
65
                continue;
66
            }
67
68
            $mentions[] = $system->getUser($mention['userId'], function () use ($system, $mention) {
69
                return new GitterUser($system, $mention);
70
            });
71
        }
72
73
        return $mentions;
74
    }
75
76
    /**
77
     * @param SystemInterface|GitterSystem $system
78
     * @param array $data
79
     * @return UserInterface
80
     */
81
    private function getUserFromMessage(SystemInterface $system, array $data): UserInterface
82
    {
83
        /** @var GitterUser $user */
84
        $user = $system->getUser($data['id'], function () use ($system, $data): UserInterface {
85
            return new GitterUser($system, $data);
86
        });
87
88
        $user->rename($data['username']);
89
        $user->setAvatar($data['avatarUrl'] ?? null);
90
91
        return $user;
92
    }
93
}
94