Completed
Push — master ( 53691c...15e6b6 )
by Marcel
01:44
created

src/Apps/ConfigAppProvider.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
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
        $app
75
            ->enableClientMessages($appAttributes['enable_client_messages'])
76
            ->enableStatistics($appAttributes['enable_statistics']);
77
78
        return $app;
79
    }
80
}
81