Completed
Push — master ( 74c577...c1ce98 )
by Kirill
06:10
created

UserMapper   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 2
c 1
b 0
f 0
lcom 0
cbo 1
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A fromGitterObject() 0 19 2
1
<?php
2
/**
3
 * This file is part of GitterBot package.
4
 *
5
 * @author Serafim <[email protected]>
6
 * @date 09.10.2015 16:56
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Core\Mappers;
13
14
use Domains\User;
15
use InvalidArgumentException;
16
use Interfaces\Gitter\Support\AttributeMapper;
17
18
/**
19
 * Class UserMapper
20
 */
21
class UserMapper
22
{
23
    /**
24
     * @param array|\StdClass $attributes
25
     * @return User
26
     * @throws InvalidArgumentException
27
     */
28
    public static function fromGitterObject($attributes)
29
    {
30
        $values = (new AttributeMapper((array)$attributes))
31
            ->rename('id', 'gitter_id')
32
            ->rename('username', 'login')
33
            ->rename('displayName', 'name')
34
            ->rename('avatarUrlMedium', 'avatar')
35
            ->only(['gitter_id', 'login', 'name', 'avatar', 'url'])
36
            ->toArray();
37
38
        $user = User::where('gitter_id', $values['gitter_id'])->first();
39
        if (!$user) {
40
            $user = User::unguarded(function () use ($values) {
41
                return User::create($values);
42
            });
43
        }
44
45
        return $user;
46
    }
47
}
48