Completed
Push — master ( c14aad...5ba59a )
by Denis
06:56
created

Security::getUrl()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.1406

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 9
cts 12
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 14
nc 3
nop 2
crap 3.1406
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: dp
5
 * Date: 16.08.17
6
 * Time: 13:09
7
 */
8
9
namespace Lan\Ebs\Sdk;
10
11
use Exception;
12
13
/**
14
 * Класс для получения публичных ресурсов по закрытому токену
15
 *
16
 * @package Lan\Ebs\Sdk
17
 */
18
final class Security implements Common
19
{
20
    /**
21
     * Токен для тестового доступа
22
     */
23
    const TEST_TOKEN = '7c0c2193d27108a509abd8ea84a8750c82b3a520';
24
25
    /**
26
     * Домен продакшен сервера API
27
     */
28
    const PROD_API_HOST = 'https://openapi.e.lanbook.com';
29
30
    /**
31
     * Домен тестового сервера API
32
     */
33
    const TEST_API_HOST = 'http://openapi.landev.ru';
34
35
    /**
36
     * Домен дев сервера API
37
     */
38
    const DEV_API_HOST = 'http://eop.local';
39
40
    /**
41
     * Домен продакшен сервера ЭБС
42
     */
43
    const PROD_EBS_HOST = 'https://e.lanbook.com';
44
45
    /**
46
     * Домен тестового сервера ЭБС
47
     */
48
    const TEST_EBS_HOST = 'http://ebs.landev.ru';
49
50
    /**
51
     * Домен дев сервера ЭБС
52
     */
53
    const DEV_EBS_HOST = 'http://ebs.local';
54
55
    /**
56
     * Инстанс клиента API
57
     *
58
     * @var Client
59
     */
60
    private $client;
61
62
    /**
63
     * Конструктор
64
     *
65
     * @param Client $client Истанс клиента
66
     * @throws Exception
67
     */
68 2
    public function __construct(Client $client)
69
    {
70 2
        if (!$client) {
71
            throw new Exception('Client not defined');
72
        }
73
74 2
        $this->client = $client;
75 2
    }
76
77
    /**
78
     * Получение данных для запроса через API
79
     *
80
     * @param string $method Http-метод запроса
81
     * @param array $params Параметры для формирования урла
82
     *
83
     * @return array
84
     *
85
     * @throws Exception
86
     */
87 2
    public function getUrl($method, array $params = [])
88
    {
89
        switch ($method) {
90 2
            case 'getDemoUrl':
91
                return [
92 1
                    'url' => '/1.0/security/demoUrl',
93 1
                    'method' => 'GET',
94
                    'code' => 200
95 1
                ];
96 1
            case 'getAutologinUrl':
97
                return [
98 1
                    'url' => '/1.0/security/autologinUrl',
99 1
                    'method' => 'GET',
100
                    'code' => 200
101 1
                ];
102
            default:
103
                throw new Exception('Route for ' . $method . ' not found');
104
        }
105
    }
106
107
    /**
108
     * @param $type
109
     * @param $id
110
     * @return mixed
111
     * @throws Exception
112
     */
113 1
    public function getDemoUrl($type, $id)
114
    {
115 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__), ['type' => $type, 'id' => $id])['data'];
116
    }
117
118
    /**
119
     * @param $uid
120
     * @param null $fio
121
     * @param null $email
122
     * @param null $redirect
123
     * @return mixed
124
     * @throws Exception
125
     */
126 1
    public function getAutologinUrl($uid, $fio = null, $email = null, $redirect = null) {
127 1
        return $this->client->getResponse(
128 1
            $this->getUrl(__FUNCTION__),
129
            [
130 1
                'uid' => $uid,
131 1
                'time' => date('YmdHi'),
132 1
                'fio' => $fio,
133 1
                'email' => $email,
134
                'redirect' => $redirect
135 1
            ]
136 1
        )['data'];
137
    }
138
139 13
    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...
140
    {
141 13
        return isset($_SERVER['USER']) && $_SERVER['USER'] == 'dp' ? Security::DEV_API_HOST : Security::PROD_API_HOST;
142
    }
143
144
    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...
145
    {
146
        return isset($_SERVER['USER']) && $_SERVER['USER'] == 'dp' ? Security::DEV_EBS_HOST : Security::PROD_EBS_HOST;
147
    }
148
}