16
16
class Subscription implements SubscriptionInterface
17
17
{
18
18
/**
19
- * @param string|null $contentEncoding (Optional) Must be "aesgcm"
19
+ * @param string $contentEncoding (Optional) defaults to "aesgcm"
20
20
* @throws \ErrorException
21
21
*/
22
22
public function __construct (
23
- private string $ endpoint ,
24
- private ? string $ publicKey = null ,
25
- private ? string $ authToken = null ,
26
- private ? string $ contentEncoding = null
23
+ private readonly string $ endpoint ,
24
+ private readonly string $ publicKey ,
25
+ private readonly string $ authToken ,
26
+ private readonly string $ contentEncoding = " aesgcm " ,
27
27
) {
28
- if ( $ publicKey || $ authToken || $ contentEncoding ) {
29
- $ supportedContentEncodings = [ ' aesgcm ' , ' aes128gcm ' ];
30
- if ( $ contentEncoding && ! in_array ( $ contentEncoding, $ supportedContentEncodings , true )) {
31
- throw new \ ErrorException ( ' This content encoding ( ' . $ contentEncoding . ' ) is not supported. ' );
32
- }
33
- $ this -> contentEncoding = $ contentEncoding ?: " aesgcm " ;
28
+ $ supportedContentEncodings = [ ' aesgcm ' , ' aes128gcm ' ];
29
+ if ( $ contentEncoding && ! in_array ( $ contentEncoding , $ supportedContentEncodings, true )) {
30
+ throw new \ ErrorException ( ' This content encoding ( ' . $ contentEncoding. ' ) is not supported. ' );
31
+ }
32
+ if ( empty ( $ publicKey ) || empty ( $ authToken ) || empty ( $ contentEncoding )) {
33
+ throw new \ ValueError ( ' Missing values. ' ) ;
34
34
}
35
35
}
36
36
@@ -42,55 +42,41 @@ public static function create(array $associativeArray): self
42
42
{
43
43
if (array_key_exists ('keys ' , $ associativeArray ) && is_array ($ associativeArray ['keys ' ])) {
44
44
return new self (
45
- $ associativeArray ['endpoint ' ],
46
- $ associativeArray ['keys ' ]['p256dh ' ] ?? null ,
47
- $ associativeArray ['keys ' ]['auth ' ] ?? null ,
45
+ $ associativeArray ['endpoint ' ] ?? "" ,
46
+ $ associativeArray ['keys ' ]['p256dh ' ] ?? "" ,
47
+ $ associativeArray ['keys ' ]['auth ' ] ?? "" ,
48
48
$ associativeArray ['contentEncoding ' ] ?? "aesgcm "
49
49
);
50
50
}
51
51
52
52
if (array_key_exists ('publicKey ' , $ associativeArray ) || array_key_exists ('authToken ' , $ associativeArray ) || array_key_exists ('contentEncoding ' , $ associativeArray )) {
53
53
return new self (
54
- $ associativeArray ['endpoint ' ],
55
- $ associativeArray ['publicKey ' ] ?? null ,
56
- $ associativeArray ['authToken ' ] ?? null ,
54
+ $ associativeArray ['endpoint ' ] ?? "" ,
55
+ $ associativeArray ['publicKey ' ] ?? "" ,
56
+ $ associativeArray ['authToken ' ] ?? "" ,
57
57
$ associativeArray ['contentEncoding ' ] ?? "aesgcm "
58
58
);
59
59
}
60
60
61
- return new self (
62
- $ associativeArray ['endpoint ' ]
63
- );
61
+ throw new \ValueError ('Missing values. ' );
64
62
}
65
63
66
- /**
67
- * {@inheritDoc}
68
- */
69
64
public function getEndpoint (): string
70
65
{
71
66
return $ this ->endpoint ;
72
67
}
73
68
74
- /**
75
- * {@inheritDoc}
76
- */
77
- public function getPublicKey (): ?string
69
+ public function getPublicKey (): string
78
70
{
79
71
return $ this ->publicKey ;
80
72
}
81
73
82
- /**
83
- * {@inheritDoc}
84
- */
85
- public function getAuthToken (): ?string
74
+ public function getAuthToken (): string
86
75
{
87
76
return $ this ->authToken ;
88
77
}
89
78
90
- /**
91
- * {@inheritDoc}
92
- */
93
- public function getContentEncoding (): ?string
79
+ public function getContentEncoding (): string
94
80
{
95
81
return $ this ->contentEncoding ;
96
82
}
0 commit comments