yiisoft /
yii-demo
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | declare(strict_types=1); |
||||||
| 4 | |||||||
| 5 | namespace App\Controller; |
||||||
| 6 | |||||||
| 7 | use App\User\User; |
||||||
| 8 | use Psr\Http\Message\RequestInterface; |
||||||
| 9 | use Psr\Http\Message\ResponseFactoryInterface; |
||||||
| 10 | use Psr\Http\Message\ResponseInterface; |
||||||
| 11 | use Psr\Log\LoggerInterface; |
||||||
| 12 | use Yiisoft\Auth\IdentityRepositoryInterface; |
||||||
| 13 | use Yiisoft\Http\Method; |
||||||
| 14 | use Yiisoft\Router\UrlGeneratorInterface; |
||||||
| 15 | use Yiisoft\Yii\Cycle\Data\Writer\EntityWriter; |
||||||
| 16 | use Yiisoft\Yii\View\ViewRenderer; |
||||||
| 17 | |||||||
| 18 | final class SignupController |
||||||
| 19 | { |
||||||
| 20 | private ViewRenderer $viewRenderer; |
||||||
| 21 | |||||||
| 22 | public function __construct(ViewRenderer $viewRenderer) |
||||||
| 23 | { |
||||||
| 24 | $this->viewRenderer = $viewRenderer->withControllerName('signup'); |
||||||
| 25 | } |
||||||
| 26 | |||||||
| 27 | public function signup( |
||||||
| 28 | RequestInterface $request, |
||||||
| 29 | IdentityRepositoryInterface $identityRepository, |
||||||
| 30 | EntityWriter $entityWriter, |
||||||
| 31 | UrlGeneratorInterface $urlGenerator, |
||||||
| 32 | LoggerInterface $logger, |
||||||
| 33 | ResponseFactoryInterface $responseFactory |
||||||
| 34 | ): ResponseInterface { |
||||||
| 35 | $body = $request->getParsedBody(); |
||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||
| 36 | $error = null; |
||||||
| 37 | |||||||
| 38 | if ($request->getMethod() === Method::POST) { |
||||||
| 39 | try { |
||||||
| 40 | foreach (['login', 'password'] as $name) { |
||||||
| 41 | if (empty($body[$name])) { |
||||||
| 42 | throw new \InvalidArgumentException(ucfirst($name) . ' is required.'); |
||||||
| 43 | } |
||||||
| 44 | } |
||||||
| 45 | |||||||
| 46 | /** @var \App\User\User $identity */ |
||||||
| 47 | $identity = $identityRepository->findByLogin($body['login']); |
||||||
|
0 ignored issues
–
show
The method
findByLogin() does not exist on Yiisoft\Auth\IdentityRepositoryInterface. It seems like you code against a sub-type of Yiisoft\Auth\IdentityRepositoryInterface such as App\User\UserRepository.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 48 | if ($identity !== null) { |
||||||
| 49 | throw new \InvalidArgumentException('Unable to register user with such username.'); |
||||||
| 50 | } |
||||||
| 51 | |||||||
| 52 | $user = new User($body['login'], $body['password']); |
||||||
| 53 | $entityWriter->write([$user]); |
||||||
| 54 | |||||||
| 55 | return $responseFactory |
||||||
| 56 | ->createResponse(302) |
||||||
| 57 | ->withHeader( |
||||||
| 58 | 'Location', |
||||||
| 59 | $urlGenerator->generate('site/index') |
||||||
| 60 | ); |
||||||
| 61 | } catch (\Throwable $e) { |
||||||
| 62 | $logger->error($e); |
||||||
| 63 | $error = $e->getMessage(); |
||||||
| 64 | } |
||||||
| 65 | } |
||||||
| 66 | |||||||
| 67 | return $this->viewRenderer->render( |
||||||
| 68 | 'signup', |
||||||
| 69 | [ |
||||||
| 70 | 'body' => $body, |
||||||
| 71 | 'error' => $error, |
||||||
| 72 | ] |
||||||
| 73 | ); |
||||||
| 74 | } |
||||||
| 75 | } |
||||||
| 76 |