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

UserMapperTrait   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
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 29
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 UserMapperTrait
20
 */
21
trait UserMapperTrait
22
{
23
    /**
24
     * @TODO Add User::class memory pool (with GC probably)
25
     *
26
     * @param array $attributes
27
     * @return User
28
     * @throws InvalidArgumentException
29
     */
30
    public static function fromGitterObject(array $attributes)
31
    {
32
        $values = (new AttributeMapper($attributes))
33
            ->rename('id', 'gitter_id')
34
            ->rename('username', 'login')
35
            ->rename('displayName', 'name')
36
            ->rename('avatarUrlMedium', 'avatar')
37
            ->only(['gitter_id', 'login', 'name', 'avatar', 'url'])
38
            ->toArray();
39
40
        $user = static::where('gitter_id', $values['gitter_id'])->first();
41
        if (!$user) {
42
            $user = static::unguarded(function () use ($values) {
43
                return static::create($values);
44
            });
45
        }
46
47
        return $user;
48
    }
49
}
50