Completed
Pull Request — master (#79)
by
unknown
02:20
created

DatabaseAppProvider::instanciate()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
cc 4
nc 5
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Apps;
4
5
use BeyondCode\LaravelWebSockets\Apps\App;
6
use BeyondCode\LaravelWebSockets\Apps\AppProvider;
7
use BeyondCode\LaravelWebSockets\Models\DatabaseApp;
8
9
class DatabaseAppProvider implements AppProvider
10
{
11
    public function all(): array
12
    {
13
        return DatabaseApp::all()->map(function (DatabaseApp $app) {
14
            return $this->instanciate($app->toArray());
15
        })->toArray();
16
    }
17
18
    public function findById($appId): ?App
19
    {
20
        return $this->instanciate(DatabaseApp::find($appId)->toArray() ?? null);
21
    }
22
23
    public function findByKey(string $appKey): ?App
24
    {
25
        return $this->instanciate(DatabaseApp::where('key', $appKey)->first()->toArray() ?? null);
26
    }
27
28
    public function findBySecret(string $appSecret): ?App
29
    {
30
        return $this->instanciate(DatabaseApp::where('secret', $appSecret)->first()->toArray() ?? null);
31
    }
32
33
    protected function instanciate(?array $appAttributes): ?App
34
    {
35
        if ( ! $appAttributes) {
36
            return null;
37
        }
38
39
        $app = new App($appAttributes['id'], $appAttributes['key'], $appAttributes['secret']);
40
41
        if (isset($appAttributes['name'])) {
42
            $app->setName($appAttributes['name']);
43
        }
44
45
        if (isset($appAttributes['host'])) {
46
            $app->setHost($appAttributes['host']);
47
        }
48
49
        $app->enableClientMessages($appAttributes['enable_client_messages'])->enableStatistics($appAttributes['enable_statistics']);
50
51
        return $app;
52
    }
53
}
54