Completed
Push — master ( 5c2719...0841d5 )
by
unknown
03:14
created

ProxyApiLoginAction::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 48
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 48
rs 9.125
c 0
b 0
f 0
cc 3
eloc 27
nc 3
nop 1
1
<?php
2
3
namespace App\Containers\Authentication\Actions;
4
5
use Apiato\Core\Foundation\Facades\Apiato;
6
use App\Containers\Authentication\Data\Transporters\ProxyApiLoginTransporter;
7
use App\Ship\Parents\Actions\Action;
8
9
/**
10
 * Class ProxyApiLoginAction.
11
 */
12
class ProxyApiLoginAction extends Action
13
{
14
15
    /**
16
     * @param \App\Containers\Authentication\Data\Transporters\ProxyApiLoginTransporter $data
17
     *
18
     * @return array
19
     */
20
    public function run(ProxyApiLoginTransporter $data): array
21
    {
22
        $requestData = [
23
            'grant_type'    => $data->grant_type ?? 'password',
24
            'client_id'     => $data->client_id,
25
            'client_secret' => $data->client_password,
26
            // 'username'      => $data->email,
0 ignored issues
show
Unused Code Comprehensibility introduced by Johannes Schobel
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
            'password'      => $data->password,
28
            'scope'         => $data->scope ?? '',
29
        ];
30
31
        $prefix = config('authentication-container.login.prefix', '');
32
        $allowedLoginFields = config('authentication-container.login.allowed_login_attributes', ['email' => []]);
33
        $fields = array_keys($allowedLoginFields);
34
35
        $loginUsername = null;
36
        $loginAttribute = null;
37
38
        foreach ($fields as $field)
39
        {
40
            $fieldname = $prefix . $field;
41
            $loginUsername = $data->getInputByKey($fieldname);
42
            $loginAttribute = $field;
43
44
            if ($loginUsername !== null) {
45
                break;
46
            }
47
        }
48
49
        $requestData = array_merge($requestData,
50
            [
51
                'username' => $loginUsername,
52
            ]
53
        );
54
55
        $responseContent = Apiato::call('[email protected]', [$requestData]);
56
57
        // check if user email is confirmed only if that feature is enabled.
58
        Apiato::call('[email protected]', [],
59
            [['loginWithCredentials' => [$requestData['username'], $requestData['password'], $loginAttribute]]]);
60
61
        $refreshCookie = Apiato::call('[email protected]', [$responseContent['refresh_token']]);
62
63
        return [
64
            'response_content' => $responseContent,
65
            'refresh_cookie'   => $refreshCookie,
66
        ];
67
    }
68
}
69