Completed
Pull Request — master (#447)
by Alexandru
02:13
created

ConfigAppManager::convertIntoApp()   A

Complexity

Conditions 5
Paths 9

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 32
rs 9.0968
c 0
b 0
f 0
cc 5
nc 9
nop 1
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
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
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 introduced by
The doc-type array[\BeyondCode\LaravelWebSockets\Apps\App] could not be parsed: Expected "]" at position 2, but found "\BeyondCode\LaravelWebSockets\Apps\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...
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
0 ignored issues
show
Bug introduced by
There is no parameter named $app. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
83
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
84
     */
85
    protected function convertIntoApp(?array $appAttributes): ?App
86
    {
87
        if (! $appAttributes) {
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