Completed
Push — master ( a1f3ed...74c577 )
by Kirill
02:49
created

MessageMapperTrait::fromGitterObject()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 34
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 34
rs 8.8571
cc 2
eloc 26
nc 2
nop 1
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 09.10.2015 16:59
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 Core\Mappers;
12
13
use Domains\Room;
14
use Domains\User;
15
use Domains\Message;
16
use Carbon\Carbon;
17
use Interfaces\Gitter\Support\AttributeMapper;
18
use Illuminate\Database\Eloquent\Model;
19
20
/**
21
 * Class MessageMapperTrait
22
 */
23
trait MessageMapperTrait
24
{
25
    /**
26
     * @param array $attributes
27
     * @return Message
28
     * @throws \InvalidArgumentException
29
     */
30
    public static function fromGitterObject(array $attributes)
31
    {
32
        $fields = ['gitter_id', 'text', 'html', 'edited', 'user', 'unread',
33
            'read_by', 'urls', 'mentions', 'issues', 'meta', 'created_at', 'updated_at', 'room_id'];
34
35
        $values = (new AttributeMapper($attributes))
36
            ->rename('readBy', 'read_by')
37
            ->rename('id', 'gitter_id')
38
            ->value('editedAt', function ($val) {
39
                return !!$val;
40
            }, 'edited')
41
            ->value('fromUser', function ($user) {
42
                return User::fromGitterObject($user);
43
            }, 'user')
44
            ->value('sent', function ($date) {
45
                return (new Carbon($date))->setTimezone('Europe/Moscow');
46
            }, 'created_at')
47
            ->value('editedAt', function ($date) {
48
                return (new Carbon($date))->setTimezone('Europe/Moscow');
49
            }, 'updated_at')
50
            ->value('mentions', function ($mentions) {
51
                return static::parseMentions($mentions);
52
            })
53
            ->only($fields)
54
            ->toArray();
55
56
        if (!array_key_exists('room_id', $values)) {
57
            $values['room_id'] = \App::make(Room::class)->id;
58
        }
59
60
        return static::unguarded(function () use ($values) {
61
            return new static($values);
0 ignored issues
show
Unused Code introduced by
The call to MessageMapperTrait::__construct() has too many arguments starting with $values.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
62
        });
63
    }
64
65
    /**
66
     * @param array $inputMentions
67
     * @return array
68
     */
69
    protected static function parseMentions(array $inputMentions)
70
    {
71
        $ids = [];
72
73
        $mentions = [];
74
75
        foreach ($inputMentions as $mention) {
76
            if (array_key_exists('userId', $mention)) {
77
                $user = User::where('gitter_id', $mention['userId'])->first();
78
79
                if ($user && !in_array($user->gitter_id, $ids, false)) {
80
                    $ids[] = $user->gitter_id;
81
                    $mentions[] = $user;
82
                }
83
            }
84
        }
85
86
        return $mentions;
87
    }
88
}
89