Completed
Pull Request — master (#447)
by Marcel
03:06 queued 01:18
created

ConfigAppManager   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 117
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 instantiate() 0 32 5
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Apps;
4
5
class ConfigAppManager implements AppManager
6
{
7
    /**
8
     * The list of apps.
9
     *
10
     * @var \Illuminate\Support\Collection
11
     */
12
    protected $apps;
13
14
    /**
15
     * Initialize the class.
16
     *
17
     * @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...
18
     */
19
    public function __construct()
20
    {
21
        $this->apps = collect(config('websockets.apps'));
22
    }
23
24
    /**
25
     * Get all apps.
26
     *
27
     * @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...
28
     */
29
    public function all(): array
30
    {
31
        return $this->apps
32
            ->map(function (array $appAttributes) {
33
                return $this->instantiate($appAttributes);
34
            })
35
            ->toArray();
36
    }
37
38
    /**
39
     * Get app by id.
40
     *
41
     * @param  mixed  $appId
42
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
43
     */
44
    public function findById($appId): ?App
45
    {
46
        $appAttributes = $this
47
            ->apps
48
            ->firstWhere('id', $appId);
49
50
        return $this->instantiate($appAttributes);
51
    }
52
53
    /**
54
     * Get app by app key.
55
     *
56
     * @param  mixed  $appKey
57
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
58
     */
59
    public function findByKey($appKey): ?App
60
    {
61
        $appAttributes = $this
62
            ->apps
63
            ->firstWhere('key', $appKey);
64
65
        return $this->instantiate($appAttributes);
66
    }
67
68
    /**
69
     * Get app by secret.
70
     *
71
     * @param  mixed  $appSecret
72
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
73
     */
74
    public function findBySecret($appSecret): ?App
75
    {
76
        $appAttributes = $this
77
            ->apps
78
            ->firstWhere('secret', $appSecret);
79
80
        return $this->instantiate($appAttributes);
81
    }
82
83
    /**
84
     * Map the app into an App instance.
85
     *
86
     * @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...
87
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
88
     */
89
    protected function instantiate(?array $appAttributes): ?App
90
    {
91
        if (! $appAttributes) {
92
            return null;
93
        }
94
95
        $app = new App(
96
            $appAttributes['id'],
97
            $appAttributes['key'],
98
            $appAttributes['secret']
99
        );
100
101
        if (isset($appAttributes['name'])) {
102
            $app->setName($appAttributes['name']);
103
        }
104
105
        if (isset($appAttributes['host'])) {
106
            $app->setHost($appAttributes['host']);
107
        }
108
109
        if (isset($appAttributes['path'])) {
110
            $app->setPath($appAttributes['path']);
111
        }
112
113
        $app
114
            ->enableClientMessages($appAttributes['enable_client_messages'])
115
            ->enableStatistics($appAttributes['enable_statistics'])
116
            ->setCapacity($appAttributes['capacity'] ?? null)
117
            ->setAllowedOrigins($appAttributes['allowed_origins'] ?? []);
118
119
        return $app;
120
    }
121
}
122