Passed
Push — main ( db60ec...ce87af )
by Nobufumi
02:23
created

AccountController::handleRenderEditForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 3
dl 0
loc 10
rs 10
1
<?php
2
3
namespace Jidaikobo\Kontiki\Controllers;
4
5
use Psr\Http\Message\ResponseInterface as Response;
6
use Psr\Http\Message\ServerRequestInterface as Request;
7
use Slim\App;
8
use Slim\Views\PhpRenderer;
9
10
use Jidaikobo\Kontiki\Core\Auth;
11
use Jidaikobo\Kontiki\Managers\CsrfManager;
12
use Jidaikobo\Kontiki\Managers\FlashManager;
13
use Jidaikobo\Kontiki\Models\AccountModel;
14
use Jidaikobo\Kontiki\Services\FormService;
15
use Jidaikobo\Kontiki\Services\RoutesService;
16
17
class AccountController extends BaseController
18
{
19
    protected string $adminDirName = 'account';
20
    protected string $label = 'account';
21
    private Auth $auth;
22
    private FormService $formService;
23
    private AccountModel $model;
24
25
    use Traits\CreateEditTrait;
26
27
    public function __construct(
28
        CsrfManager $csrfManager,
29
        FlashManager $flashManager,
30
        PhpRenderer $view,
31
        RoutesService $routesService,
32
        Auth $auth,
33
        FormService $formService,
34
        AccountModel $model
35
    ) {
36
        parent::__construct(
37
            $csrfManager,
38
            $flashManager,
39
            $view,
40
            $routesService
41
        );
42
        $this->auth = $auth;
43
        $this->formService = $formService;
44
        $this->formService->setModel($model);
45
        $this->model = $model;
46
    }
47
48
    public static function registerRoutes(App $app, string $basePath = ''): void
49
    {
50
        $app->get('/account/settings', AccountController::class . ':handleRenderEditForm');
51
        $app->post("/account/edit/{id}", AccountController::class . ':handleEdit');
52
53
        // redirect
54
        $app->get('/account/index', AccountController::class . ':accoutEditRedirect');
55
        $app->get("/account/edit/{id}", AccountController::class . ':accoutEditRedirect');
56
    }
57
58
    public function accoutEditRedirect(
59
        Request $request,
60
        Response $response,
61
        array $args
0 ignored issues
show
Unused Code introduced by
The parameter $args 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

61
        /** @scrutinizer ignore-unused */ array $args

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...
62
    ): Response {
63
        return $this->redirectResponse(
64
            $request,
65
            $response,
66
            "/account/settings"
67
        );
68
    }
69
70
    public function handleRenderEditForm(
71
        Request $request,
72
        Response $response,
73
        array $args
74
    ): Response {
75
        $args['id'] = $this->auth->getCurrentUser()['id'] ?? 0;
76
        if ($args['id'] == 0) {
77
            die();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Bug Best Practice introduced by
In this branch, the function will implicitly return null which 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
        }
79
        return $this->renderEditForm($request, $response, $args);
80
    }
81
82
    public function handleEdit(
83
        Request $request,
84
        Response $response,
85
        array $args
86
    ): Response {
87
        $id = $args['id'];
88
        return $this->handleSave($request, $response, 'edit', $id);
89
    }
90
}
91