Completed
Pull Request — master (#90)
by
unknown
02:51 queued 54s
created

AppProvider::findByKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Database;
4
5
use BeyondCode\LaravelWebSockets\Apps\App;
6
use BeyondCode\LaravelWebSockets\Apps\AppProvider as IAppProvider;
7
use BeyondCode\LaravelWebSockets\Database\Models\App as DatabaseApp;
8
9
class AppProvider implements IAppProvider
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'])
50
            ->enableStatistics($appAttributes['enable_statistics']);
51
52
        return $app;
53
    }
54
}
55