ConfigAppProvider   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A all() 0 8 1
A findById() 0 8 1
A findByKey() 0 8 1
A findBySecret() 0 8 1
A instanciate() 0 31 5
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Apps;
4
5
use Illuminate\Support\Collection;
6
7
class ConfigAppProvider implements AppProvider
8
{
9
    /** @var Collection */
10
    protected $apps;
11
12
    public function __construct()
13
    {
14
        $this->apps = collect(config('websockets.apps'));
15
    }
16
17
    /**  @return array[\BeyondCode\LaravelWebSockets\AppProviders\App] */
0 ignored issues
show
Documentation introduced by freek
The doc-type array[\BeyondCode\Larave...ckets\AppProviders\App] could not be parsed: Expected "]" at position 2, but found "\BeyondCode\LaravelWebSockets\AppProviders\App". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
18
    public function all(): array
19
    {
20
        return $this->apps
21
            ->map(function (array $appAttributes) {
22
                return $this->instanciate($appAttributes);
23
            })
24
            ->toArray();
25
    }
26
27
    public function findById($appId): ?App
28
    {
29
        $appAttributes = $this
30
            ->apps
31
            ->firstWhere('id', $appId);
32
33
        return $this->instanciate($appAttributes);
34
    }
35
36
    public function findByKey(string $appKey): ?App
37
    {
38
        $appAttributes = $this
39
            ->apps
40
            ->firstWhere('key', $appKey);
41
42
        return $this->instanciate($appAttributes);
43
    }
44
45
    public function findBySecret(string $appSecret): ?App
46
    {
47
        $appAttributes = $this
48
            ->apps
49
            ->firstWhere('secret', $appSecret);
50
51
        return $this->instanciate($appAttributes);
52
    }
53
54
    protected function instanciate(?array $appAttributes): ?App
55
    {
56
        if (! $appAttributes) {
57
            return null;
58
        }
59
60
        $app = new App(
61
            $appAttributes['id'],
62
            $appAttributes['key'],
63
            $appAttributes['secret']
64
        );
65
66
        if (isset($appAttributes['name'])) {
67
            $app->setName($appAttributes['name']);
68
        }
69
70
        if (isset($appAttributes['host'])) {
71
            $app->setHost($appAttributes['host']);
72
        }
73
74
        if (isset($appAttributes['path'])) {
75
            $app->setPath($appAttributes['path']);
76
        }
77
78
        $app
79
            ->enableClientMessages($appAttributes['enable_client_messages'])
80
            ->enableStatistics($appAttributes['enable_statistics'])
81
            ->setCapacity($appAttributes['capacity'] ?? null);
82
83
        return $app;
84
    }
85
}
86