Completed
Push — master ( ac7d60...f9cacf )
by Denis
04:30
created

Security::getUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace Lan\Ebs\Sdk;
4
5
use Exception;
6
7
/**
8
 * Класс для получения публичных ресурсов по закрытому токену
9
 *
10
 * @package Lan\Ebs\Sdk
11
 */
12
final class Security implements Common
13
{
14
    /**
15
     * Токен для тестового доступа
16
     */
17
    const TEST_TOKEN = '7c0c2193d27108a509abd8ea84a8750c82b3a520';
18
19
    /**
20
     * Домен продакшен сервера API
21
     */
22
    const PROD_API_HOST = 'https://openapi.e.lanbook.com';
23
24
    /**
25
     * Домен тестового сервера API
26
     */
27
    const TEST_API_HOST = 'http://openapi.landev.ru';
28
29
    /**
30
     * Домен дев сервера API
31
     */
32
    const DEV_API_HOST = 'http://eop.local';
33
34
    /**
35
     * Домен продакшен сервера ЭБС
36
     */
37
    const PROD_EBS_HOST = 'https://e.lanbook.com';
38
39
    /**
40
     * Домен тестового сервера ЭБС
41
     */
42
    const TEST_EBS_HOST = 'http://ebs.landev.ru';
43
44
    /**
45
     * Домен дев сервера ЭБС
46
     */
47
    const DEV_EBS_HOST = 'http://ebs.local';
48
49
    /**
50
     * Инстанс клиента API
51
     *
52
     * @var Client
53
     */
54
    private $client;
55
56
    /**
57
     * Конструктор
58
     *
59
     * @param Client $client Истанс клиента
60
     * @throws Exception
61
     */
62 1
    public function __construct(Client $client)
63
    {
64 1
        if (!$client) {
65
            throw new Exception('Client not defined');
66
        }
67
68 1
        $this->client = $client;
69 1
    }
70
71 11
    public static function getApiHost()
0 ignored issues
show
Coding Style introduced by
getApiHost uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
72
    {
73 11
        return isset($_SERVER['USER']) && $_SERVER['USER'] == 'dp' ? Security::DEV_API_HOST : Security::PROD_API_HOST;
74
    }
75
76
    public static function getEbsHost()
0 ignored issues
show
Coding Style introduced by
getEbsHost uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
77
    {
78
        return isset($_SERVER['USER']) && $_SERVER['USER'] == 'dp' ? Security::DEV_EBS_HOST : Security::PROD_EBS_HOST;
79
    }
80
81
    /**
82
     * Получение данных для запроса через API
83
     *
84
     * @param string $method Http-метод запроса
85
     * @param array $params Параметры для формирования урла
86
     *
87
     * @return array
88
     *
89
     * @throws Exception
90
     */
91 1
    public function getUrl($method, array $params = [])
92
    {
93
        switch ($method) {
94 1
            case 'getAutologinUrl':
95
                return [
96 1
                    'url' => '/1.0/security/autologinUrl',
97
                    'method' => 'GET',
98
                    'code' => 200
99
                ];
100
            default:
101
                throw new Exception('Route for ' . $method . ' not found');
102
        }
103
    }
104
105
    /**
106
     * @param $uid
107
     * @param null $fio
108
     * @param null $email
109
     * @param null $redirect
110
     * @return mixed
111
     * @throws Exception
112
     */
113 1
    public function getAutologinUrl($uid, $fio = null, $email = null, $redirect = null)
114
    {
115 1
        return $this->client->getResponse(
116 1
            $this->getUrl(__FUNCTION__),
117
            [
118 1
                'uid' => $uid,
119 1
                'time' => date('YmdHi'),
120 1
                'fio' => $fio,
121 1
                'email' => $email,
122 1
                'redirect' => $redirect
123
            ]
124
        )['data'];
125
    }
126
}