Test Setup Failed
Push — master ( 22888c...730ea3 )
by alexfloppy
01:21
created

ChangePasswordController   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 57
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A change() 0 18 3
A validator() 0 6 1
1
<?php
2
3
namespace App\Http\Controllers\Api\v1\Auth;
4
5
use App\Helpers\Interfaces\ResponseCodesInterface;
6
use App\Http\Controllers\Controller;
7
use App\Services\PasswordService;
8
use App\Helpers\JsonApiResponseHelper;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\Validator;
11
12
/**
13
 * Class ChangePasswordController
14
 * @package App\Http\Controllers\Api\v1\Auth
15
 */
16
class ChangePasswordController extends Controller implements ResponseCodesInterface
17
{
18
    use JsonApiResponseHelper;
19
20
    /**
21
     * @SWG\Post(path="/password/change",
22
     *   tags={"User actions"},
23
     *   summary="Change user password",
24
     *   description="Change user password",
25
     *   produces={"application/json"},
26
     *   consumes={"application/json"},
27
     *     @SWG\Parameter(
28
     *     in="body",
29
     *     name="change password",
30
     *     description="JSON Object which change user password",
31
     *     required=true,
32
     *     @SWG\Schema(
33
     *         type="object",
34
     *         @SWG\Property(property="current_password", type="string", example="87654321"),
35
     *         @SWG\Property(property="password", type="string", example="87654321"),
36
     *         @SWG\Property(property="password_confirmation", type="string", example="87654321")
37
     *     )
38
     *   ),
39
     *   @SWG\Response(response="200", description="Return message")
40
     * )
41
     */
42
43
    public function change(PasswordService $passwordService, Request $request)
44
    {
45
        $errors = $this->validator($request->all())->errors();
46
47
        if (!empty($errors->all())) {
48
            return $this->sendFailedResponse($errors->toArray(), self::HTTP_CODE_BAD_REQUEST);
49
        }
50
51
        $changed = $passwordService->changePassword($request);
52
53
        if ($changed) {
54
            return $this->sendSuccessResponse();
55
        }
56
57
        $errors = ['password' => ['Wrong credentials']];
58
59
        return $this->sendFailedResponse($errors, self::HTTP_CODE_BAD_REQUEST);
60
    }
61
62
    /**
63
     * @param array $data
64
     * @return mixed
65
     */
66
    protected function validator(array $data)
67
    {
68
        return Validator::make($data, [
69
            'password' => 'required|confirmed|min:6'
70
        ]);
71
    }
72
}
73