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

UserMapper::fromSlackObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 15
c 1
b 0
f 1
nc 2
nop 1
dl 21
loc 21
rs 9.3142
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\Slack;
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
0 ignored issues
show
Bug introduced by
There is no parameter named $attributes. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
24
     * @return User
25
     * @throws InvalidArgumentException
26
     */
27
    public static function fromSlackObject(\Slack\User $user)
28
    {
29
        $values = (new AttributeMapper((array) $user->data))
30
            ->rename('id', 'gitter_id')
31
            ->rename('name', 'login')
32
            ->rename('real_name', 'name')
33
            ->value('profile', function($profile) {
34
                return $profile['image_48'];
35
            }, 'avatar')
36
            ->only(['gitter_id', 'login', 'name', 'avatar'])
37
            ->toArray();
38
39
        $user = User::where('gitter_id', $values['gitter_id'])->first();
40
        if (!$user) {
41
            $user = User::unguarded(function () use ($values) {
42
                return User::create($values);
43
            });
44
        }
45
46
        return $user;
47
    }
48
}
49