Completed
Push — master ( 60508b...b5ebd1 )
by
unknown
02:58
created

IzbergConnector::setForce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/**
4
 * @author Paquet Judicaël <[email protected]>
5
 */
6
7
namespace judicaelpaquet\IzbergBundle\Service;
8
9
use Guzzle\Http\Client;
10
use Guzzle\Http\Message\Response;
11
12
/**
13
 * Class IzbergConnector
14
 * @package judicaelpaquet\IzbergBundle\Service
15
 */
16
class IzbergConnector
17
{
18
    /**
19
     * @var string
20
     */
21
    private $izbergUrl;
22
    /**
23
     * @var Client
24
     */
25
    private $httpClient;
26
    /**
27
     * @var \Predis\Client
28
     */
29
    private $redis;
30
    /**
31
     * @var boolean
32
     */
33
    private $force = false;
34
35
    /**
36
     * IzbergConnector constructor.
37
     * @param string $izbergUrl
38
     * @param Client $httpClient
39
     * @param \Predis\Client $redis
40
     */
41
    public function __construct(string $izbergUrl, Client $httpClient, \Predis\Client $redis)
42
    {
43
        $this->izbergUrl = $izbergUrl;
44
        $this->httpClient = $httpClient;
45
        $this->redis = $redis;
46
    }
47
48
    /**
49
     * @param string $email
50
     * @param string $firstName
51
     * @param string $lastName
52
     * @param string $secretKey
53
     */
54
    public function connect(string $email = '', string $firstName = '', string $lastName = '', string $secretKey = '')
55
    {
56
        // If the izberg_sso key already exists in redis, we just have to exit.
57
58
        if ($this->getAuthentication() && $this->force == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
59
            return;
60
        }
61
62
        // Otherwise, we have to use izberg url to generate a new authentication.
63
        if ($this->getAuthentication() && !$this->force) {
64
            return;
65
        }
66
67
        // Otherwise, we have to izberg url to generate a new authentication.
68
        $request = $this->httpClient->get(
69
            $this->generateUrl($email, $firstName, $lastName, $secretKey)
70
        );
71
72
        if ($response = $request->send()) {
73
            $this->setAuthentication($response);
74
        }
75
    }
76
77
    /**
78
     * @param string $email
79
     * @param string $firstName
80
     * @param string $lastName
81
     * @param string $secretKey
82
     */
83
    public function forceAuthAndConnect(
84
        string $email = '',
85
        string $firstName = '',
86
        string $lastName = '',
87
        string $secretKey = ''
88
    ) {
89
        $this->setForce(true);
90
        $this->connect($email, $firstName, $lastName, $secretKey);
91
    }
92
93
    /**
94
     * @param string $email
95
     * @param string $firstName
96
     * @param string $lastName
97
     * @param string $secretKey
98
     * @return string
99
     */
100
    public function generateUrl(
101
        string $email = '',
102
        string $firstName = '',
103
        string $lastName = '',
104
        string $secretKey = ''
105
    ):string
106
    {
107
        $timestamp = time();
108
        $toCompose = [$email, $firstName, $lastName, $timestamp];
109
        $message_auth = hash_hmac('sha1', implode(";", $toCompose), $secretKey);
110
111
        $url = $this->izbergUrl;
112
        $url .= 'first_name=' . urlencode($firstName) . '&last_name=' . urlencode($lastName) . '&message_auth=' . $message_auth . '&email=' . urlencode($email);
113
        $url .= '&timestamp=' . $timestamp . '&is_staff=true';
114
        return $url;
115
    }
116
117
    /**
118
     * <<<<<<< HEAD
119
     * @param bool $force
120
     */
121
    public function setForce(bool $force)
122
    {
123
        $this->force = $force;
124
    }
125
126
    /**
127
     * @return bool
128
     */
129
    public function getForce():bool
130
    {
131
        return $this->force;
132
    }
133
134
    /**
135
     * @param Response $response
136
     */
137
    public function setAuthentication(Response $response)
138
    {
139
        $this->redis->set('izberg_sso', json_encode(
140
                json_decode($response->getBody(), true)
141
            )
142
        );
143
    }
144
145
    /**
146
     * @return array
147
     */
148
    public function getAuthentication()
149
    {
150
        return json_decode($this->redis->get('izberg_sso'), true);
151
    }
152
}
153
154