1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BeyondCode\LaravelWebSockets\Apps; |
4
|
|
|
|
5
|
|
|
class ConfigAppManager implements AppManager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* The list of apps. |
9
|
|
|
* |
10
|
|
|
* @var \Illuminate\Support\Collection |
11
|
|
|
*/ |
12
|
|
|
protected $apps; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Initialize the class. |
16
|
|
|
* |
17
|
|
|
* @return void |
|
|
|
|
18
|
|
|
*/ |
19
|
|
|
public function __construct() |
20
|
|
|
{ |
21
|
|
|
$this->apps = collect(config('websockets.apps')); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get all apps. |
26
|
|
|
* |
27
|
|
|
* @return array[\BeyondCode\LaravelWebSockets\Apps\App] |
|
|
|
|
28
|
|
|
*/ |
29
|
|
|
public function all(): array |
30
|
|
|
{ |
31
|
|
|
return $this->apps |
32
|
|
|
->map(function (array $appAttributes) { |
33
|
|
|
return $this->instantiate($appAttributes); |
34
|
|
|
}) |
35
|
|
|
->toArray(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get app by id. |
40
|
|
|
* |
41
|
|
|
* @param mixed $appId |
42
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null |
43
|
|
|
*/ |
44
|
|
|
public function findById($appId): ?App |
45
|
|
|
{ |
46
|
|
|
$appAttributes = $this |
47
|
|
|
->apps |
48
|
|
|
->firstWhere('id', $appId); |
49
|
|
|
|
50
|
|
|
return $this->instantiate($appAttributes); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get app by app key. |
55
|
|
|
* |
56
|
|
|
* @param mixed $appKey |
57
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null |
58
|
|
|
*/ |
59
|
|
|
public function findByKey($appKey): ?App |
60
|
|
|
{ |
61
|
|
|
$appAttributes = $this |
62
|
|
|
->apps |
63
|
|
|
->firstWhere('key', $appKey); |
64
|
|
|
|
65
|
|
|
return $this->instantiate($appAttributes); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get app by secret. |
70
|
|
|
* |
71
|
|
|
* @param mixed $appSecret |
72
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null |
73
|
|
|
*/ |
74
|
|
|
public function findBySecret($appSecret): ?App |
75
|
|
|
{ |
76
|
|
|
$appAttributes = $this |
77
|
|
|
->apps |
78
|
|
|
->firstWhere('secret', $appSecret); |
79
|
|
|
|
80
|
|
|
return $this->instantiate($appAttributes); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Map the app into an App instance. |
85
|
|
|
* |
86
|
|
|
* @param array|null $app |
|
|
|
|
87
|
|
|
* @return \BeyondCode\LaravelWebSockets\Apps\App|null |
88
|
|
|
*/ |
89
|
|
|
protected function instantiate(?array $appAttributes): ?App |
90
|
|
|
{ |
91
|
|
|
if (! $appAttributes) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$app = new App( |
96
|
|
|
$appAttributes['id'], |
97
|
|
|
$appAttributes['key'], |
98
|
|
|
$appAttributes['secret'] |
99
|
|
|
); |
100
|
|
|
|
101
|
|
|
if (isset($appAttributes['name'])) { |
102
|
|
|
$app->setName($appAttributes['name']); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if (isset($appAttributes['host'])) { |
106
|
|
|
$app->setHost($appAttributes['host']); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (isset($appAttributes['path'])) { |
110
|
|
|
$app->setPath($appAttributes['path']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$app |
114
|
|
|
->enableClientMessages($appAttributes['enable_client_messages']) |
115
|
|
|
->enableStatistics($appAttributes['enable_statistics']) |
116
|
|
|
->setCapacity($appAttributes['capacity'] ?? null) |
117
|
|
|
->setAllowedOrigins($appAttributes['allowed_origins'] ?? []); |
118
|
|
|
|
119
|
|
|
return $app; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.