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

ChangePasswordController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 26
c 4
b 0
f 0
dl 0
loc 72
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 1
B change() 0 36 7
A redirectToMain() 0 3 1
A flash_message() 0 3 1
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 $currentUser,
28
      private WebControllerService $webService, 
29
      private ViewRenderer $viewRenderer,
30
    )
31
    {
32
      $this->currentUser = $currentUser;
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
     
51
      $identity_id = $this->currentUser->getIdentity()->getId();
52
      if (null!==$identity_id) {
53
        $identity = $identityRepository->findIdentity($identity_id);
54
        if (null!==$identity) {
55
          // Identity and User are in a HasOne relationship so no null value
56
          $login = $identity->getUser()?->getLogin();
57
          if ($request->getMethod() === Method::POST
58
            && $formHydrator->populate($changePasswordForm, $request->getParsedBody())
59
            && $changePasswordForm->change() 
60
          ) {
61
            // Identity implements CookieLoginIdentityInterface: ensure the regeneration of the cookie auth key by means of $authService->logout();
62
            // @see vendor\yiisoft\user\src\Login\Cookie\CookieLoginIdentityInterface 
63
            // Specific note: "Make sure to invalidate earlier issued keys when you implement force user logout,
64
            // PASSWORD CHANGE and other scenarios, that require forceful access revocation for old sessions.
65
            // The authService logout function will regenerate the auth key here => overwriting any auth key
66
            $authService->logout();
67
            $this->flash_message('success', $this->translator->translate('validator.password.change'));
68
            return $this->redirectToMain();
69
          }
70
          return $this->viewRenderer->render('change', 
71
                  [
72
                      'formModel' => $changePasswordForm, 
73
                      'login' => $login,
74
                      'canChangePasswordForAnyUser' => $this->currentUser->can('changePasswordForAnyUser')    
75
                  ]);
76
        } // identity
0 ignored issues
show
Bug Best Practice introduced by
The function implicitly returns null when the if condition on line 54 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
      } // identity_id 
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
    } // reset
79
    
80
    /**
81
     * @param string $level
82
     * @param string $message
83
     * @return Flash
84
     */
85
    private function flash_message(string $level, string $message): Flash {
86
      $this->flash->add($level, $message, true);
87
      return $this->flash;
88
    }
89
    
90
    private function redirectToMain(): ResponseInterface
91
    {
92
      return $this->webService->getRedirectResponse('site/index');
93
    }
94
}
95