Completed
Push — master ( a5b60f...e18c09 )
by Mark
03:06 queued 03:02
created

Session   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 5
Bugs 4 Features 1
Metric Value
wmc 15
eloc 49
c 5
b 4
f 1
dl 0
loc 128
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A status() 0 6 1
A clearList() 0 4 1
A add() 0 9 2
A keyList() 0 3 1
A export() 0 11 1
A newSessionKey() 0 3 1
A import() 0 12 1
A get() 0 9 2
A sessions() 0 14 3
A start() 0 7 1
A stop() 0 5 1
1
<?php
2
3
namespace Socialblue\LaravelQueryAdviser\Service;
4
5
use Illuminate\Support\Facades\Cache;
6
use Illuminate\Support\Facades\File;
7
use Illuminate\Support\Facades\Response;
8
use Socialblue\LaravelQueryAdviser\Helper\SessionFormatter;
9
use Symfony\Component\HttpFoundation\BinaryFileResponse;
10
11
class Session
12
{
13
    public static function add(string $sessionId)
14
    {
15
        $sessionIds = Cache::get(config('laravel-query-adviser.cache.session.key_list'), []);
16
        if (! is_array($sessionIds)) {
17
            $sessionIds = [$sessionId];
18
        }
19
        $sessionIds[] = $sessionId;
20
21
        Cache::put(config('laravel-query-adviser.cache.session.key_list'), $sessionIds, null);
22
    }
23
24
    public static function clearList(): array
25
    {
26
        return [
27
            'success' => Cache::forget(config('laravel-query-adviser.cache.session.key_list')),
28
        ];
29
    }
30
31
    /**
32
     * @param $sessionKey
33
     * @return BinaryFileResponse
34
     */
35
    public static function export($sessionKey)
36
    {
37
        $directory = storage_path('laravel-query-adviser/');
38
        File::makeDirectory($directory, 0777, true, true);
39
        $file = $directory . 'last-export.json';
40
        file_put_contents($file, json_encode(self::get($sessionKey), JSON_PRETTY_PRINT));
41
42
        return Response::download(
43
            $file,
44
            'query-adviser-export.json',
45
            ['Content-Type: application/json']
46
        );
47
    }
48
49
    public static function get(string $sessionId): array
50
    {
51
        $data = Cache::tags(['laravel-query-adviser-sessions'])->get($sessionId);
52
53
        if (! is_array($data)) {
54
            return [];
55
        }
56
57
        return $data;
58
    }
59
60
    /**
61
     * @param $data
62
     * @return bool[]
63
     */
64
    public static function import($data): array
65
    {
66
        $sessionKey = self::newSessionKey();
67
        self::add($sessionKey);
68
        Cache::tags(['laravel-query-adviser-sessions'])->put(
69
            $sessionKey,
70
            $data,
71
            config('laravel-query-adviser.cache.ttl')
72
        );
73
74
        return [
75
            'success' => true,
76
        ];
77
    }
78
79
    public static function status(): array
80
    {
81
        return [
82
            'active' => Cache::has(config('laravel-query-adviser.cache.session_id')),
83
            'active_session_id' => Cache::get(config('laravel-query-adviser.cache.session_id')),
84
            'has_queries' => Cache::tags(['laravel-query-adviser-sessions'])->has(Cache::get(config('laravel-query-adviser.cache.session_id'))),
85
        ];
86
    }
87
88
    /**
89
     * @return string[]
90
     */
91
    public static function start(): array
92
    {
93
        $sessionId = self::newSessionKey();
94
        self::add($sessionId);
95
        Cache::put(config('laravel-query-adviser.cache.session_id'), $sessionId, config('laravel-query-adviser.cache.session.max_time'));
96
        return [
97
            'session_id' => $sessionId,
98
        ];
99
    }
100
101
    /**
102
     * @return string[]
103
     */
104
    public static function stop(): array
105
    {
106
        Cache::forget(config('laravel-query-adviser.cache.session_id'));
107
        return [
108
            'session_id' => '',
109
        ];
110
    }
111
112
    public static function keyList(): array
113
    {
114
        return Cache::get(config('laravel-query-adviser.cache.session.key_list'), []);
115
    }
116
117
    /**
118
     * @return array[]
119
     */
120
    public static function sessions(): array
121
    {
122
        $keys = self::keyList();
123
124
        foreach ($keys as $key) {
125
            $sessionData = Cache::tags(['laravel-query-adviser-sessions'])->get($key) ?? [];
126
            $formattedData = SessionFormatter::format($sessionData, $key);
127
128
            if (! empty($formattedData)) {
129
                $dataList[] = $formattedData;
130
            }
131
        }
132
133
        return $dataList ?? [];
134
    }
135
136
    protected static function newSessionKey(): string
137
    {
138
        return uniqid('laravel-query-adviser', true);
139
    }
140
}
141