Completed
Push — master ( db0246...91f80b )
by Denis
02:07
created

Security::getApiHost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 2
eloc 2
nc 2
nop 0
crap 6
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
final class Security implements Common
14
{
15
    const TEST_TOKEN = '7c0c2193d27108a509abd8ea84a8750c82b3a520';
16
17
    const PROD_API_HOST = 'https://openapi.e.lanbook.com';
18
    const TEST_API_HOST = 'http://openapi.landev.ru';
19
    const DEV_API_HOST = 'http://eop.local';
20
21
    const PROD_EBS_HOST = 'https://e.lanbook.com';
22
    const TEST_EBS_HOST = 'http://ebs.landev.ru';
23
    const DEV_EBS_HOST = 'http://ebs.local';
24
25
    private $client;
26
27
    /**
28
     * Security constructor.
29
     *
30
     * @param  Client $client
31
     * @throws Exception
32
     */
33 2
    public function __construct(Client $client)
34
    {
35 2
        if (!$client) {
36
            throw new Exception('Client not defined');
37
        }
38
39 2
        $this->client = $client;
40 2
    }
41
42 2
    public function getUrl($method, array $params = [])
43
    {
44
        switch ($method) {
45 2
            case 'getSecretKey':
46
                return [
47
                    'url' => '/1.0/security/secretKey',
48
                    'method' => 'GET',
49
                    'code' => 200
50
                ];
51 2
            case 'getDemoUrl':
52
                return [
53 1
                    'url' => '/1.0/security/demoUrl',
54
                    'method' => 'GET',
55
                    'code' => 200
56
                ];
57 1
            case 'getAutologinUrl':
58
                return [
59 1
                    'url' => '/1.0/security/autologinUrl',
60
                    'method' => 'GET',
61
                    'code' => 200
62
                ];
63
            default:
64
                throw new Exception('Route for ' . $method . ' not found');
65
        }
66
    }
67
68 1
    public function getDemoUrl($type, $id)
69
    {
70 1
        return $this->client->getResponse($this->getUrl(__FUNCTION__), ['type' => $type, 'id' => $id])['data'];
71
    }
72
73 1
    public function getAutologinUrl($uid, $fio = null, $email = null, $redirect = null) {
74 1
        return $this->client->getResponse(
75 1
            $this->getUrl(__FUNCTION__),
76
            [
77 1
                'uid' => $uid,
78 1
                'time' => date('YmdHi'),
79 1
                'fio' => $fio,
80 1
                'email' => $email,
81 1
                'redirect' => $redirect
82
            ]
83
        )['data'];
84
    }
85
86
    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...
87
    {
88
        return $_SERVER['USER'] == 'dp' ? Security::DEV_API_HOST : Security::TEST_API_HOST;
89
    }
90
91
    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...
92
    {
93
        return $_SERVER['USER'] == 'dp' ? Security::DEV_EBS_HOST : Security::TEST_EBS_HOST;
94
    }
95
}