Completed
Push — master ( e8241e...60d0c3 )
by
unknown
04:24 queued 02:47
created

Security::getUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 5
cts 6
cp 0.8333
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2
crap 3.0416
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 2
    public function __construct(Client $client)
63
    {
64 2
        if (!$client) {
65
            throw new Exception('Client not defined');
66
        }
67
68 2
        $this->client = $client;
69 2
    }
70
71 6
    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 6
        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
     * @param $type
83
     * @param $id
84
     * @return mixed
85
     * @throws Exception
86
     */
87 1
    public function getDemoUrl($type, $id)
88
    {
89 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__), ['type' => $type, 'id' => $id])['data'];
90
    }
91
92
    /**
93
     * Получение данных для запроса через API
94
     *
95
     * @param string $method Http-метод запроса
96
     * @param array $params Параметры для формирования урла
97
     *
98
     * @return array
99
     *
100
     * @throws Exception
101
     */
102 2
    public function getUrl($method, array $params = [])
103
    {
104
        switch ($method) {
105 2
            case 'getDemoUrl':
106
                return [
107 1
                    'url' => '/1.0/security/demoUrl',
108
                    'method' => 'GET',
109
                    'code' => 200
110
                ];
111 1
            case 'getAutologinUrl':
112
                return [
113 1
                    'url' => '/1.0/security/autologinUrl',
114
                    'method' => 'GET',
115
                    'code' => 200
116
                ];
117
            default:
118
                throw new Exception('Route for ' . $method . ' not found');
119
        }
120
    }
121
122
    /**
123
     * @param $uid
124
     * @param null $fio
125
     * @param null $email
126
     * @param null $redirect
127
     * @return mixed
128
     * @throws Exception
129
     */
130 1
    public function getAutologinUrl($uid, $fio = null, $email = null, $redirect = null)
131
    {
132 1
        return $this->client->getResponse(
133 1
            $this->getUrl(__FUNCTION__),
134
            [
135 1
                'uid' => $uid,
136 1
                'time' => date('YmdHi'),
137 1
                'fio' => $fio,
138 1
                'email' => $email,
139 1
                'redirect' => $redirect
140
            ]
141
        )['data'];
142
    }
143
}