UserMapper::fromGitterObject()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 19
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 13
c 1
b 0
f 0
nc 2
nop 1
dl 19
loc 19
rs 9.4285
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 Interfaces\Gitter;
13
14
use Domains\User;
15
use Interfaces\Gitter\Support\AttributeMapper;
16
17
/**
18
 * Class UserMapper
19
 */
20 View Code Duplication
class UserMapper
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    /**
23
     * @param array|\StdClass $attributes
24
     * @return User
25
     * @throws InvalidArgumentException
26
     */
27
    public static function fromGitterObject($attributes)
28
    {
29
        $values = (new AttributeMapper((array) $attributes))
30
            ->rename('id', 'gitter_id')
31
            ->rename('username', 'login')
32
            ->rename('displayName', 'name')
33
            ->rename('avatarUrlMedium', 'avatar')
34
            ->only(['gitter_id', 'login', 'name', 'avatar', 'url'])
35
            ->toArray();
36
37
        $user = User::where('gitter_id', $values['gitter_id'])->first();
38
        if (!$user) {
39
            $user = User::unguarded(function () use ($values) {
40
                return User::create($values);
41
            });
42
        }
43
44
        return $user;
45
    }
46
}
47