Request::getUser()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 14
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 26
rs 9.7998
1
<?php
2
/**
3
 * This file is part of product_management
4
 * User: Sinan TURGUT <[email protected]>
5
 * Date: 24.06.2019
6
 * php version 7.2
7
 *
8
 * @category Assessment
9
 * @package  ProductManagement
10
 * @author   Sinan TURGUT <[email protected]>
11
 * @license  See LICENSE file
12
 * @link     https://dev.sinanturgut.com.tr
13
 */
14
15
namespace App\Utility;
16
17
use GuzzleHttp\Client;
0 ignored issues
show
Bug introduced by
The type GuzzleHttp\Client 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
19
/**
20
 * Class Request
21
 * @package App\Utility
22
 */
23
class Request
24
{
25
26
    /**
27
     * @return mixed|null
28
     * @throws \GuzzleHttp\Exception\GuzzleException
29
     */
30
    public static function getUser()
31
    {
32
        $token = (new self)->getToken();
33
34
        if(!$token) {
35
            return null;
36
        }
37
38
        $client = new Client();
39
        $headers = [
40
            'Authorization' => 'Bearer ' . $token,
41
            'Accept'        => 'application/json',
42
        ];
43
        $res = $client->request('GET', 'http://user_management_nginx_1/user/checkToken',
44
            [
45
                'headers' => $headers,
46
                'exceptions' => false
47
            ]
48
        );
49
        $httpCode = $res->getStatusCode();
50
        if($httpCode!=200)
51
        {
52
            return null;
53
        }
54
55
        return json_decode($res->getBody(),1);
56
    }
57
58
    /**
59
     * Get Http Header Bearer Token
60
     *
61
     * @return string |null
62
     */
63
    private function getToken()
64
    {
65
        $result = null;
66
        if (isset($_SERVER["HTTP_AUTHORIZATION"])) {
67
            list($type, $data) = explode(" ", $_SERVER["HTTP_AUTHORIZATION"], 2);
68
            if (strcasecmp($type, "Bearer") == 0) {
69
                $result = $data;
70
            }
71
        }
72
73
        return $result;
74
    }
75
76
}