Passed
Pull Request — master (#602)
by Ross
01:39
created

ChangePasswordController::change()   B

Complexity

Conditions 9
Paths 7

Size

Total Lines 36
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 9
eloc 16
c 3
b 0
f 0
nc 7
nop 6
dl 0
loc 36
rs 8.0555
1
<?php
2
declare(strict_types=1);
3
4
namespace App\Auth\Controller;
5
6
use App\Auth\AuthService;
7
use App\Auth\Identity;
8
use App\Auth\IdentityRepository;
9
use App\Auth\Form\ChangePasswordForm;
10
use App\Service\WebControllerService;
11
use Psr\Http\Message\ResponseInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ResponseInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Psr\Http\Message\ServerRequestInterface;
0 ignored issues
show
Bug introduced by
The type Psr\Http\Message\ServerRequestInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Yiisoft\FormModel\FormHydrator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\FormModel\FormHydrator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
14
use Yiisoft\Http\Method;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Http\Method was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
15
use Yiisoft\Session\SessionInterface as Session;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Session\SessionInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use Yiisoft\Session\Flash\Flash;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Session\Flash\Flash was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Yiisoft\Translator\TranslatorInterface as Translator;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Translator\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Yiisoft\User\CurrentUser;
0 ignored issues
show
Bug introduced by
The type Yiisoft\User\CurrentUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Yiisoft\Yii\View\ViewRenderer;
0 ignored issues
show
Bug introduced by
The type Yiisoft\Yii\View\ViewRenderer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
21
final class ChangePasswordController
22
{
23
    public function __construct(
24
      private Session $session,
25
      private Flash $flash,
26
      private Translator $translator,
27
      private CurrentUser $current_user,
28
      private WebControllerService $webService, 
29
      private ViewRenderer $viewRenderer,
30
    )
31
    {
32
      $this->current_user = $current_user;
33
      $this->session = $session;      
34
      $this->flash = new Flash($session);
35
      $this->translator = $translator;
36
      $this->viewRenderer = $viewRenderer->withControllerName('changepassword');
37
    }
38
    
39
    public function change(
40
      AuthService $authService,
41
      Identity $identity,
42
      IdentityRepository $identityRepository,
43
      ServerRequestInterface $request,
44
      FormHydrator $formHydrator,
45
      ChangePasswordForm $changePasswordForm
46
    ): ResponseInterface {
47
      if ($authService->isGuest()) {
48
          return $this->redirectToMain();
49
      }  
50
      // permit an authenticated user, ie. not a guest, only and null!== current user
51
      if (!$authService->isGuest()) {
52
        if ($this->current_user->can('viewInv',[])) {
53
          // readonly the login detail on the change form
54
          $identity_id = $this->current_user->getIdentity()->getId();
55
          if (null!==$identity_id) {
56
            $identity = $identityRepository->findIdentity($identity_id);
57
            if (null!==$identity) {
58
              // Identity and User are in a HasOne relationship so no null value
59
              $login = $identity->getUser()?->getLogin();
60
              if ($request->getMethod() === Method::POST
61
                && $formHydrator->populate($changePasswordForm, $request->getParsedBody())
62
                && $changePasswordForm->change() 
63
              ) {
64
                // Identity implements CookieLoginIdentityInterface: ensure the regeneration of the cookie auth key by means of $authService->logout();
65
                // @see vendor\yiisoft\user\src\Login\Cookie\CookieLoginIdentityInterface 
66
67
                // Specific note: "Make sure to invalidate earlier issued keys when you implement force user logout,
68
                // PASSWORD CHANGE and other scenarios, that require forceful access revocation for old sessions.
69
                // The authService logout function will regenerate the auth key here => overwriting any auth key
70
                $authService->logout();
71
                $this->flash_message('success', $this->translator->translate('validator.password.change'));
72
                return $this->redirectToMain();
73
              }
74
              return $this->viewRenderer->render('change', ['formModel' => $changePasswordForm, 'login' => $login]);
75
            } // identity
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 57 is false. This is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
76
          } // identity_id 
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 55 is false. This is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
77
        } // current user
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 52 is false. This is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
78
      } // auth service 
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 51 is false. This is incompatible with the type-hinted return Psr\Http\Message\ResponseInterface. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
79
    } // reset
80
    
81
    /**
82
     * @param string $level
83
     * @param string $message
84
     * @return Flash
85
     */
86
    private function flash_message(string $level, string $message): Flash {
87
      $this->flash->add($level, $message, true);
88
      return $this->flash;
89
    }
90
    
91
    private function redirectToMain(): ResponseInterface
92
    {
93
      return $this->webService->getRedirectResponse('site/index');
94
    }
95
}
96