Passed
Push — master ( 54115d...67a0b3 )
by meta
02:30
created

UserFactory::convertAzureUser()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 12
nc 4
nop 1
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
1
<?php
2
3
namespace Metaclassing\EnterpriseAuth;
4
5
class UserFactory
6
{
7
    protected $config;
8
    protected static $user_callback;
9
10
    public function __construct()
11
    {
12
        $this->config = config('enterpriseauth');
13
    }
14
15
    public function convertAzureUser($azure_user)
16
    {
17
        $user_class = config('enterpriseauth.user_class');
18
        $user_map = config('enterpriseauth.user_map');
19
        $id_field = config('enterpriseauth.user_id_field');
20
21
        $new_user = new $user_class();
22
        $new_user->$id_field = $azure_user->id;
23
        //$new_user->password = bcrypt('');
24
25
        foreach ($user_map as $azure_field => $user_field) {
26
            $new_user->$user_field = $azure_user->$azure_field;
27
        }
28
29
        $callback = static::$user_callback;
30
31
        if ($callback && is_callable($callback)) {
32
            $callback($new_user);
33
        }
34
35
        $new_user->save();
36
37
        return $new_user;
38
    }
39
40
    public static function userCallback($callback)
41
    {
42
        if (! is_callable($callback)) {
43
            throw new \Exception('Must provide a callable.');
44
        }
45
46
        static::$user_callback = $callback;
47
    }
48
}
49