Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
UsersService
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 insert
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 update_user_image
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Services;
4
5use App\Models\Image;
6use App\Models\User;
7use App\Traits\Upload;
8use Illuminate\Support\Facades\Hash;
9
10class UsersService
11{
12    use Upload;
13    public function insert($request){
14        $user = User::create([
15            'username'      => $request->username,
16            'name'          => $request->name,
17            'password'      => Hash::make($request->password),
18        ]);
19
20        $user->roles()->attach([$request->role_id]);
21
22        return $user;
23    }
24
25    public function update($user, $request){
26        if($request->password == NULL){
27            $password = $user->password;
28        } else{
29            $password = Hash::make($request->password);
30        }
31
32        $user->username       = $request->username;
33        $user->name           = $request->name;
34        $user->password       = $password;
35        $user->save();
36
37        if($request->role_id){
38            $user->roles()->detach([$user->getRoleId()]);
39            $user->roles()->attach([$request->role_id]);
40        }
41    }
42
43    public function update_user_image($user,$image){
44        $path = $this->uploadImage($image, 'uploads/users', 660);
45
46        if($user->Image == null){
47            //if user don't have image 
48            Image::create([
49                'imageable_id'   => $user->id,
50                'imageable_type' => 'App\Models\User',
51                'src'            => $path,
52            ]);
53
54        } else {
55            //ig user have image
56            $oldImage = $user->Image->src;
57
58            if(file_exists(base_path('public/uploads/users/') . $oldImage))
59                unlink(base_path('public/uploads/users/') . $oldImage);
60
61            $user->Image->src = $path;
62            $user->Image->save();
63        }
64    }
65}