Test Failed
Push — master ( 02204c...466d37 )
by Maximo
03:15
created

AuthenticationMiddleware::call()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 1
nop 1
dl 0
loc 28
rs 9.7666
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style introduced by
End of line character is invalid; expected "\n" but found "\r\n"
Loading history...
2
3
declare(strict_types=1);
4
5
namespace Gewaer\Middleware;
6
7
use Phalcon\Mvc\Micro;
8
use Phalcon\Mvc\Micro\MiddlewareInterface;
9
use Baka\Auth\Models\Sessions;
10
use Gewaer\Models\Users;
11
use Phalcon\Http\Request;
12
use Gewaer\Exception\UnauthorizedHttpException;
13
14
/**
15
 * Class AuthenticationMiddleware
16
 *
17
 * @package Niden\Middleware
18
 */
19
class AuthenticationMiddleware implements MiddlewareInterface
20
{
21
    /**
22
     * Call me
23
     *
24
     * @param Micro $api
25
     * @todo need to check section for auth here
26
     * @return bool
27
     */
28
    public function call(Micro $api)
29
    {
30
        $auth = $api->getService('auth');
31
        $config = $api->getService('config');
32
33
        // to get the payload
34
        $data = $auth->data();
35
36
        $api->getDI()->setShared(
37
            'userData',
38
            function () use ($config, $data) {
0 ignored issues
show
Unused Code introduced by
The import $config is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
39
                $session = new Sessions();
40
                $request = new Request();
41
42
                if (!empty($data) && !empty($data['sessionId'])) {
43
                    //user
44
                    if (!$user = Users::getByEmail($data['email'])) {
45
                        throw new UnauthorizedHttpException('User not found');
46
                    }
47
48
                    return $session->check($user, $data['sessionId'], $request->getClientAddress(), 1);
0 ignored issues
show
Bug introduced by
It seems like $request->getClientAddress() can also be of type boolean; however, parameter $userIp of Baka\Auth\Models\Sessions::check() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
                    return $session->check($user, $data['sessionId'], /** @scrutinizer ignore-type */ $request->getClientAddress(), 1);
Loading history...
49
                } else {
50
                    throw new UnauthorizedHttpException('User not found');
51
                }
52
            }
53
        );
54
55
        return true;
56
    }
57
}
58