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

App::setAllowedOrigins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace BeyondCode\LaravelWebSockets\Apps;
4
5
use BeyondCode\LaravelWebSockets\Exceptions\InvalidApp;
6
7
class App
8
{
9
    /** @var int */
10
    public $id;
11
12
    /** @var string */
13
    public $key;
14
15
    /** @var string */
16
    public $secret;
17
18
    /** @var string|null */
19
    public $name;
20
21
    /** @var string|null */
22
    public $host;
23
24
    /** @var string|null */
25
    public $path;
26
27
    /** @var int|null */
28
    public $capacity = null;
29
30
    /** @var bool */
31
    public $clientMessagesEnabled = false;
32
33
    /** @var bool */
34
    public $statisticsEnabled = true;
35
36
    /** @var array */
37
    public $allowedOrigins = [];
38
39
    /**
40
     * Find the app by id.
41
     *
42
     * @param  mixed  $appId
43
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
44
     */
45
    public static function findById($appId)
46
    {
47
        return app(AppManager::class)->findById($appId);
48
    }
49
50
    /**
51
     * Find the app by app key.
52
     *
53
     * @param  mixed  $appId
0 ignored issues
show
Bug introduced by
There is no parameter named $appId. 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...
54
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
55
     */
56
    public static function findByKey($appKey): ?self
57
    {
58
        return app(AppManager::class)->findByKey($appKey);
59
    }
60
61
    /**
62
     * Find the app by app secret.
63
     *
64
     * @param  mixed  $appId
0 ignored issues
show
Bug introduced by
There is no parameter named $appId. 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...
65
     * @return \BeyondCode\LaravelWebSockets\Apps\App|null
66
     */
67
    public static function findBySecret($appSecret): ?self
68
    {
69
        return app(AppManager::class)->findBySecret($appSecret);
70
    }
71
72
    /**
73
     * Initialize the Web Socket app instance.
74
     *
75
     * @param  mixed  $appId
76
     * @param  mixed  $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. 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...
77
     * @param  mixed  $secret
0 ignored issues
show
Documentation introduced by
There is no parameter named $secret. Did you maybe mean $appSecret?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

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

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

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

Loading history...
78
     * @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...
79
     * @throws \BeyondCode\LaravelWebSockets\Exceptions\InvalidApp
80
     */
81
    public function __construct($appId, $appKey, $appSecret)
82
    {
83
        if ($appKey === '') {
84
            throw InvalidApp::valueIsRequired('appKey', $appId);
85
        }
86
87
        if ($appSecret === '') {
88
            throw InvalidApp::valueIsRequired('appSecret', $appId);
89
        }
90
91
        $this->id = $appId;
92
        $this->key = $appKey;
93
        $this->secret = $appSecret;
94
    }
95
96
    /**
97
     * Set the name of the app.
98
     *
99
     * @param  string  $appName
100
     * @return $this
101
     */
102
    public function setName(string $appName)
103
    {
104
        $this->name = $appName;
105
106
        return $this;
107
    }
108
109
    /**
110
     * Set the app host.
111
     *
112
     * @param  string  $host
113
     * @return $this
114
     */
115
    public function setHost(string $host)
116
    {
117
        $this->host = $host;
118
119
        return $this;
120
    }
121
122
    /**
123
     * Set path for the app.
124
     *
125
     * @param  string  $path
126
     * @return $this
127
     */
128
    public function setPath(string $path)
129
    {
130
        $this->path = $path;
131
132
        return $this;
133
    }
134
135
    /**
136
     * Enable client messages.
137
     *
138
     * @param  bool  $enabled
139
     * @return $this
140
     */
141
    public function enableClientMessages(bool $enabled = true)
142
    {
143
        $this->clientMessagesEnabled = $enabled;
144
145
        return $this;
146
    }
147
148
    /**
149
     * Set the maximum capacity for the app.
150
     *
151
     * @param  int|null  $capacity
152
     * @return $this
153
     */
154
    public function setCapacity(?int $capacity)
155
    {
156
        $this->capacity = $capacity;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Enable statistics for the app.
163
     *
164
     * @param  bool  $enabled
165
     * @return $this
166
     */
167
    public function enableStatistics(bool $enabled = true)
168
    {
169
        $this->statisticsEnabled = $enabled;
170
171
        return $this;
172
    }
173
174
    /**
175
     * Add whitelisted origins.
176
     *
177
     * @param  array  $allowedOrigins
178
     * @return $this
179
     */
180
    public function setAllowedOrigins(array $allowedOrigins)
181
    {
182
        $this->allowedOrigins = $allowedOrigins;
183
184
        return $this;
185
    }
186
}
187