Completed
Push — master ( 1392b0...257c81 )
by Denis
01:57
created

Security::getSecretKey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 3
nop 1
crap 4
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
    private $client;
16
17
    /**
18
     * Security constructor.
19
     *
20
     * @param  Client $client
21
     * @throws Exception
22
     */
23 2
    public function __construct(Client $client)
24
    {
25 2
        if (!$client) {
26
            throw new Exception('Client not defined');
27
        }
28
29 2
        $this->client = $client;
30 2
    }
31
32 1
    public function getSecretKey($date)
0 ignored issues
show
Coding Style introduced by
getSecretKey uses the super-global variable $_SESSION 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...
33
    {
34 1
        if (is_string($date) && strlen($date) >= 8) {
35 1
            $date = substr($date, 0, 8);
36
37 1
            if (!empty($_SESSION[$date])) {
38 1
                return $_SESSION[$date];
39
            }
40
        }
41
42 1
        return $_SESSION[$date] = $this->client->getResponse($this->getUrl(__FUNCTION__), ['date' => $date])['data'];
43
    }
44
45 1
    public function getUrl($method, array $params = [])
46
    {
47
        switch ($method) {
48 1
            case 'getSecretKey':
49
                return [
50 1
                    'url' => '/1.0/security/secretKey',
51
                    'method' => 'GET',
52
                    'code' => 200
53
                ];
54
            case 'getDemoUrl':
55
                return [
56
                    'url' => '/1.0/security/demoUrl',
57
                    'method' => 'GET',
58
                    'code' => 200
59
                ];
60
            case 'getAutologinUrl':
61
                return [
62
                    'url' => '/1.0/security/autologinUrl',
63
                    'method' => 'GET',
64
                    'code' => 200
65
                ];
66
            default:
67
                throw new Exception('Route for ' . $method . ' not found');
68
        }
69
    }
70
71
    public function getDemoUrl($type, $id)
72
    {
73
        return $this->client->getResponse($this->getUrl(__FUNCTION__), ['type' => $type, 'id' => $id])['data'];
74
    }
75
76
    public function getAutologinUrl($uid, $time, $fio = null, $email = null, $redirect = null) {
77
        return $this->client->getResponse(
78
            $this->getUrl(__FUNCTION__),
79
            [
80
                'uid' => $uid,
81
                'time' => $time,
82
                'fio' => $fio,
83
                'email' => $email,
84
                'redirect' => $redirect
85
            ]
86
        )['data'];
87
    }
88
}