Issues (192)

src/Apps/ConfigAppManager.php (2 issues)

1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Apps;
4
5
use BeyondCode\LaravelWebSockets\Contracts\AppManager;
6
7
class ConfigAppManager implements AppManager
8
{
9
    /**
10
     * The list of apps.
11
     *
12
     * @var \Illuminate\Support\Collection
13
     */
14
    protected $apps;
15
16
    /**
17
     * Initialize the class.
18
     *
19
     * @return void
20
     */
21
    public function __construct()
22
    {
23
        $this->apps = collect(config('websockets.apps'));
24
    }
25
26
    /**
27
     * Get all apps.
28
     *
29
     * @return array[\BeyondCode\LaravelWebSockets\Apps\App]
0 ignored issues
show
Documentation Bug introduced by
The doc comment array[\BeyondCode\LaravelWebSockets\Apps\App] at position 1 could not be parsed: Expected ']' at position 1, but found '['.
Loading history...
30
     */
31
    public function all(): array
32
    {
33
        return $this->apps
34
            ->map(function (array $appAttributes) {
35
                return $this->convertIntoApp($appAttributes);
36
            })
37
            ->toArray();
38
    }
39
40
    /**
41
     * Get app by id.
42
     *
43
     * @param  string|int  $appId
44
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
45
     */
46
    public function findById($appId): ?App
47
    {
48
        return $this->convertIntoApp(
49
            $this->apps->firstWhere('id', $appId)
50
        );
51
    }
52
53
    /**
54
     * Get app by app key.
55
     *
56
     * @param  string  $appKey
57
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
58
     */
59
    public function findByKey($appKey): ?App
60
    {
61
        return $this->convertIntoApp(
62
            $this->apps->firstWhere('key', $appKey)
63
        );
64
    }
65
66
    /**
67
     * Get app by secret.
68
     *
69
     * @param  string  $appSecret
70
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
71
     */
72
    public function findBySecret($appSecret): ?App
73
    {
74
        return $this->convertIntoApp(
75
            $this->apps->firstWhere('secret', $appSecret)
76
        );
77
    }
78
79
    /**
80
     * Map the app into an App instance.
81
     *
82
     * @param  array|null  $app
83
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
84
     */
85
    protected function convertIntoApp(?array $appAttributes): ?App
86
    {
87
        if (! $appAttributes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $appAttributes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
88
            return null;
89
        }
90
91
        $app = new App(
92
            $appAttributes['id'],
93
            $appAttributes['key'],
94
            $appAttributes['secret']
95
        );
96
97
        if (isset($appAttributes['name'])) {
98
            $app->setName($appAttributes['name']);
99
        }
100
101
        if (isset($appAttributes['host'])) {
102
            $app->setHost($appAttributes['host']);
103
        }
104
105
        if (isset($appAttributes['path'])) {
106
            $app->setPath($appAttributes['path']);
107
        }
108
109
        $app
110
            ->enableClientMessages($appAttributes['enable_client_messages'])
111
            ->enableStatistics($appAttributes['enable_statistics'])
112
            ->setCapacity($appAttributes['capacity'] ?? null)
113
            ->setAllowedOrigins($appAttributes['allowed_origins'] ?? []);
114
115
        return $app;
116
    }
117
}
118