1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Neo\EarlyAccess; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\InteractsWithTime; |
6
|
|
|
use Illuminate\Contracts\Auth\Guard as Auth; |
7
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem as Storage; |
8
|
|
|
|
9
|
|
|
class EarlyAccess |
10
|
|
|
{ |
11
|
|
|
use InteractsWithTime; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var \Illuminate\Contracts\Filesystem\Filesystem |
15
|
|
|
*/ |
16
|
|
|
private $storage; |
17
|
|
|
/** |
18
|
|
|
* @var \Illuminate\Contracts\Auth\Guard |
19
|
|
|
*/ |
20
|
|
|
private $auth; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* EarlyAccess constructor. |
24
|
|
|
* |
25
|
|
|
* @param \Illuminate\Contracts\Filesystem\Filesystem $storage |
26
|
|
|
* @param \Illuminate\Contracts\Auth\Guard $auth |
27
|
|
|
*/ |
28
|
|
|
public function __construct(Storage $storage, Auth $auth) |
29
|
|
|
{ |
30
|
|
|
$this->auth = $auth; |
31
|
|
|
|
32
|
|
|
$this->storage = $storage; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Checks if early access is enabled or not. |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
*/ |
40
|
|
|
public function isEnabled(): bool |
41
|
|
|
{ |
42
|
|
|
if ($this->auth->user()) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return (bool) $this->getBeaconDetails() ?: config('early-access.enabled'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns a list of allowed networks. |
51
|
|
|
* |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public function allowedNetworks(): array |
55
|
|
|
{ |
56
|
|
|
$data = $this->getBeaconDetails() ?? []; |
57
|
|
|
|
58
|
|
|
return array_get($data, 'allowed', []); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Adds a network to the list of allowed networks. |
63
|
|
|
* |
64
|
|
|
* @param array $networks |
65
|
|
|
*/ |
66
|
|
|
public function addAllowedNetworksToBeacon(array $networks) |
67
|
|
|
{ |
68
|
|
|
if ($data = $this->getBeaconDetails()) { |
69
|
|
|
if (! empty($networks)) { |
70
|
|
|
array_push($data['allowed'], ...$networks); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$data['allowed'] = array_unique($data['allowed']); |
74
|
|
|
|
75
|
|
|
return $this->saveBeaconFileWithData($data); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Save the beacon file. |
81
|
|
|
* |
82
|
|
|
* @param array $allowed |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function saveBeacon(array $allowed = []): bool |
86
|
|
|
{ |
87
|
|
|
if ($this->getBeaconDetails()) { |
88
|
|
|
return (bool) $this->addAllowedNetworksToBeacon($allowed); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $this->saveBeaconFileWithData([ |
92
|
|
|
'allowed' => array_unique($allowed), |
93
|
|
|
'time' => $this->currentTime(), |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Deletes the beacon file. |
99
|
|
|
* |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function deleteBeacon(): bool |
103
|
|
|
{ |
104
|
|
|
return $this->storage->delete('early-access'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the beacon file details. |
109
|
|
|
* |
110
|
|
|
* @return false|array |
111
|
|
|
*/ |
112
|
|
|
public function getBeaconDetails() |
113
|
|
|
{ |
114
|
|
|
if (! $this->storage->exists('early-access')) { |
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return json_decode($this->storage->get('early-access'), true); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Saves the beacon file. |
123
|
|
|
* |
124
|
|
|
* @param array $data |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
private function saveBeaconFileWithData(array $data): bool |
128
|
|
|
{ |
129
|
|
|
return $this->storage->put('early-access', json_encode($data, JSON_PRETTY_PRINT)); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|