UserRepository::fillData()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 5
nc 8
nop 1
1
<?php namespace Usman\Guardian\Repositories;
2
3
use Hash;
4
use Usman\Guardian\Repositories\Interfaces\UserRepositoryInterface;
5
6
class UserRepository extends BaseRepository implements UserRepositoryInterface {
7
       
8
    /**
9
     * Searches the database for a username with a specific role
10
     * 
11
     * @param  string $username username to search
12
     * @param  string $role     role for the user to search
13
     * @param  int $perPage  records per page
14
     * @return Illuminate\Pagination\paginator the paginator instance.
15
     */
16
    public function searchUserByRole($username, $role, $perPage = self::PAGE)
17
    {
18
        
19
        $query = $this->model->query();
20
        
21
        if( ! empty($username))
22
        {
23
            $query->where('username','LIKE','%'.$username.'%');
24
        }
25
26
        if( ! empty($role))
27
        {
28
            $query->whereHas('roles',function($query) use ($role)
29
            {
30
                $query->where('role_name','=',$role);
31
            });
32
        }
33
34
        return $query->with('roles')->paginate($perPage);
35
    }
36
37
    /**
38
     * fills the user model data
39
     * 
40
     * @param  array  $data
41
     * @return void       
42
     */
43
    protected function fillData(array $data)
44
    {
45
        $this->model->username = (!empty($data['username'])) ? $data['username'] : $this->model->username;
46
        $this->model->password = (!empty($data['password'])) ? Hash::make($data['password']) : $this->model->password;
47
        $this->model->email    = (!empty($data['email'])) ? $data['email'] : $this->model->email;
48
        $this->model->active   = $data['active'];
49
    }
50
}