Completed
Push — master ( d56647...5f1d83 )
by Kirill
11s
created

MessageMapper::parseMentions()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
rs 8.8571
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author butschster <[email protected]>
6
 * @date 20.207.2016 17:08
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Interfaces\Gitter;
12
13
14
use Carbon\Carbon;
15
use Domains\User;
16
use Illuminate\Contracts\Support\Arrayable;
17
use Domains\Room\RoomInterface;
18
use Interfaces\Gitter\Support\AttributeMapper;
19
20
class MessageMapper implements Arrayable
21
{
22
    /**
23
     * @var array
24
     */
25
    protected $attributes;
26
27
    /**
28
     * MessageMapper constructor.
29
     *
30
     * @param RoomInterface $room
31
     * @param array         $attributes
32
     */
33
    public function __construct(RoomInterface $room, array $attributes)
34
    {
35
        $fields = ['gitter_id', 'text', 'html', 'edited', 'user', 'unread',
36
            'read_by', 'urls', 'mentions', 'issues', 'meta', 'created_at', 'updated_at', 'room_id'];
37
38
        $this->attributes = (new AttributeMapper($attributes))
39
            ->rename('readBy', 'read_by')
40
            ->rename('id', 'gitter_id')
41
            ->value('editedAt', function ($val) {
42
                return !!$val;
43
            }, 'edited')
44
            ->value('fromUser', function ($user) {
45
                return UserMapper::fromGitterObject($user);
46
            }, 'user')
47
            ->value('sent', function ($date) {
48
                return (new Carbon($date))->setTimezone('Europe/Moscow');
49
            }, 'created_at')
50
            ->value('editedAt', function ($date) {
51
                return (new Carbon($date))->setTimezone('Europe/Moscow');
52
            }, 'updated_at')
53
            ->value('mentions', function ($mentions) {
54
                return $this->parseMentions($mentions);
55
            })
56
            ->only($fields)
57
            ->toArray();
58
59
        if (!array_key_exists('room_id', $this->attributes)) {
60
            $this->attributes['room_id'] = $room->id();
61
        }
62
    }
63
64
    /**
65
     * @param array $inputMentions
66
     * @return array
67
     */
68
    protected function parseMentions(array $inputMentions)
69
    {
70
        $ids = [];
71
72
        $mentions = [];
73
74
        foreach ($inputMentions as $mention) {
75
            if (array_key_exists('userId', $mention)) {
76
                $user = User::where('gitter_id', $mention['userId'])->first();
77
78
                if ($user && !in_array($user->gitter_id, $ids, false)) {
79
                    $ids[] = $user->gitter_id;
80
                    $mentions[] = $user;
81
                }
82
            }
83
        }
84
85
        return $mentions;
86
    }
87
88
    /**
89
     * Get the instance as an array.
90
     *
91
     * @return array
92
     */
93
    public function toArray()
94
    {
95
        return $this->attributes;
96
    }
97
}