Passed
Branch master (182172)
by Matthew
04:03 queued 01:23
created

UserController::Update()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
cc 3
eloc 14
nc 4
nop 2
1
<?php
2
3
namespace PhpDraft\Controllers\Admin;
4
5
use \Silex\Application;
6
use Symfony\Component\HttpFoundation\Response;
7
use Symfony\Component\HttpFoundation\Request;
8
use PhpDraft\Domain\Models\PhpDraftResponse;
9
use PhpDraft\Domain\Entities\Draft;
10
11
class UserController
12
{
13
  public function Get(Application $app, Request $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

13
  public function Get(Application $app, /** @scrutinizer ignore-unused */ Request $request) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
14
    $response = $app['phpdraft.LoginUserService']->GetAll();
15
16
    return $app->json($response, $response->responseType());
17
  }
18
19
  public function Update(Application $app, Request $request) {
20
    $user_id = $request->get('id');
21
    $user = $app['phpdraft.LoginUserRepository']->LoadById($user_id);
22
    $user->email = $request->get('email');
23
    $user->name = $request->get('name');
24
    $user->roles = $request->get('roles');
25
26
    //Coerce down to a 1 or 0 type
27
    $enabVal = $request->get('enabled');
0 ignored issues
show
Unused Code introduced by
The assignment to $enabVal is dead and can be removed.
Loading history...
28
    $enabled = $request->get('enabled') == true;
29
    $user->enabled = $enabled ? 1 : 0;
30
31
    $validity = $app['phpdraft.LoginUserValidator']->IsAdminUserUpdateValid($user);
32
33
    if(!$validity->success) {
34
      return $app->json($validity, $validity->responseType());
35
    }
36
37
    $app['phpdraft.LoginUserRepository']->Update($user);
38
39
    $response = new PhpDraftResponse(true, array());
40
41
    return $app->json($response, $response->responseType());
42
  }
43
44
  public function Delete(Application $app, Request $request) {
45
    $user_id = $request->get('user_id');
46
    $user = $app['phpdraft.LoginUserRepository']->LoadById($user_id);
47
48
    if($app['phpdraft.LoginUserService']->CurrentUserIsAdmin($user)) {
49
      $response = new PhpDraftResponse(false, array());
50
      $response->errors[] = "Unable to delete user - user is admin.";
51
52
      return $app->json($response, $response->responseType());
53
    }
54
55
    $response = $app['phpdraft.LoginUserService']->DeleteUser($user);
56
57
    return $app->json($response, $response->responseType());
58
  }
59
}