Completed
Pull Request — master (#90)
by
unknown
01:40
created

AppProvider   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 46
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 6 1
A findById() 0 4 1
A findByKey() 0 4 1
A findBySecret() 0 4 1
A instantiate() 0 21 4
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->instantiate($app->toArray());
15
        })->toArray();
16
    }
17
18
    public function findById($appId): ?App
19
    {
20
        return $this->instantiate(DatabaseApp::find($appId)->toArray() ?? null);
21
    }
22
23
    public function findByKey(string $appKey): ?App
24
    {
25
        return $this->instantiate(DatabaseApp::where('key', $appKey)->first()->toArray() ?? null);
26
    }
27
28
    public function findBySecret(string $appSecret): ?App
29
    {
30
        return $this->instantiate(DatabaseApp::where('secret', $appSecret)->first()->toArray() ?? null);
31
    }
32
33
    protected function instantiate(?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