Passed
Branch tests (1701da)
by Chris
02:51
created

SessionManager::deleteSession()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Slackbot001;
4
5
use Illuminate\Support\Facades\Log;
6
7
class SessionManager
8
{
9
    protected $userSession;
10
    protected $TDinstance;
11
12
    public function __invoke()
13
    {
14
        return $this->userSession;
15
    }
16
17
    public function setupSession($slackUID, $slackToken)
18
    {
19
        if (env('TD_SANDBOX') == 'TRUE') {
20
            $env = 'sandbox';
21
        } else {
22
            $env = 'prod';
23
        }
24
25
        $userSession = TDsession::where('s_user_id', $slackUID)->first();
26
27
        if ($userSession != null) {
28
            Log::info('CP_SessionManager: Found user session.');
29
            if ($userSession->td_token) {
30
                $TDinstance = new CP_TDinstance(env('TD_BEID'), env('TD_WEBSERVICESKEY'), env('TD_URLROOT'), env('TD_APPID'), $env, (string) $userSession->td_token);
31
                Log::info('CP_SessionManager: CP_TDinstance initialized with existing JWT.');
32
            } else {
33
                $TDinstance = new CP_TDinstance(env('TD_BEID'), env('TD_WEBSERVICESKEY'), env('TD_URLROOT'), env('TD_APPID'), $env);
34
                Log::info('CP_SessionManager: CP_TDinstance initialized with new JWT.');
35
            }
36
37
            Log::info('CP_SessionManager: Updating existing CP_TDsession s_token and td_token.');
38
            $userSession->s_token = $slackToken;
39
            $userSession->td_token = $TDinstance;
40
            $userSession->save();
41
            $this->userSession = $userSession;
42
            $this->TDinstance = $TDinstance;
43
        } else {
44
            Log::info('CP_SessionManager: No user session.');
45
            Log::info('CP_SessionManager: Creating new user session.');
46
            $TDinstance = new CP_TDinstance(env('TD_BEID'), env('TD_WEBSERVICESKEY'), env('TD_URLROOT'), env('TD_APPID'), $env);
47
            Log::info('CP_SessionManager: New CP_TDinstance initialized.');
48
            $userSession = new TDsession();
49
            Log::info('CP_SessionManager: New CP_TDsession initialized.');
50
            Log::info('CP_SessionManager: Updating CP_TDsession s_token and td_token.');
51
            $userSession->s_user_id = $slackUID;
52
            $userSession->s_token = $slackToken;
53
            $userSession->td_token = $TDinstance;
54
            $userSession->save();
55
            $this->userSession = $userSession;
56
            $this->TDinstance = $TDinstance;
57
        }
58
59
        return $TDinstance;
60
    }
61
62
    public function checkSession($slackUID)
63
    {
64
        Log::info('CP_SessionManager: checkSession method called.');
65
        $userSession = TDsession::where('s_user_id', $slackUID)->first();
66
        if ($userSession != null) {
67
            Log::info('CP_SessionManager: Session exists.');
68
69
            return true;
70
        } else {
71
            Log::info('CP_SessionManager: Session does not exist.');
72
73
            return false;
74
        }
75
    }
76
77
    public function deleteSession($slackUID)
78
    {
79
        Log::info('CP_SessionManager: deleteSession method called.');
80
        if ($this->checkSession($slackUID)) {
81
            $userSession = TDsession::where('s_user_id', $slackUID)->first();
82
            $sid = $userSession->id;
83
            $userSession->delete();
84
            Log::info('CP_SessionManager: Deleted session '.$sid.'.');
85
        }
86
    }
87
}
88