Skip to content

Commit 411c308

Browse files
committed
Push: Make the APNS environment an enum
1 parent 20008a1 commit 411c308

11 files changed

+76
-60
lines changed

ApnsPHP/Environment.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* APNS Environment
5+
*
6+
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands
7+
* SPDX-License-Identifier: BSD-2-Clause
8+
*/
9+
10+
namespace ApnsPHP;
11+
12+
/**
13+
* APNS Environment.
14+
*/
15+
enum Environment
16+
{
17+
/**
18+
* Production environment.
19+
*/
20+
case Production;
21+
22+
/**
23+
* Production environment using alternative port.
24+
*/
25+
case AltProduction;
26+
27+
/**
28+
* Sandbox environment.
29+
*/
30+
case Sandbox;
31+
32+
/**
33+
* Sandbox environment using alternative port.
34+
*/
35+
case AltSandbox;
36+
37+
/**
38+
* Get HTTP URL for a given environment
39+
*
40+
* @return string The URL to the matching APNS environment
41+
*/
42+
public function getUrl(): string
43+
{
44+
return match ($this) {
45+
Environment::Production => 'https://api.push.apple.com:443',
46+
Environment::AltProduction => 'https://api.push.apple.com:2197',
47+
Environment::Sandbox => 'https://api.sandbox.push.apple.com:443',
48+
Environment::AltSandbox => 'https://api.sandbox.push.apple.com:2197',
49+
};
50+
}
51+
}

ApnsPHP/Push.php

Lines changed: 5 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,6 @@
2626
*/
2727
class Push
2828
{
29-
/**
30-
* Production environment.
31-
* @var int
32-
*/
33-
public const ENVIRONMENT_PRODUCTION = 0;
34-
35-
/**
36-
* Sandbox environment.
37-
* @var int
38-
*/
39-
public const ENVIRONMENT_SANDBOX = 1;
40-
4129
/**
4230
* Device token length.
4331
* @var int
@@ -82,9 +70,9 @@ class Push
8270

8371
/**
8472
* Active environment.
85-
* @var int
73+
* @var Environment
8674
*/
87-
protected int $environment;
75+
protected Environment $environment;
8876

8977
/**
9078
* Connect timeout in seconds.
@@ -175,15 +163,6 @@ class Push
175163
*/
176164
protected int $sendRetryTimes = 3;
177165

178-
/**
179-
* HTTP/2 Service URLs environments.
180-
* @var array<int,string>
181-
*/
182-
protected array $HTTPServiceURLs = [
183-
'https://api.push.apple.com:443', // Production environment
184-
'https://api.development.push.apple.com:443' // Sandbox environment
185-
];
186-
187166
/**
188167
* Message queue.
189168
* @var array
@@ -199,17 +178,12 @@ class Push
199178
/**
200179
* Constructor.
201180
*
202-
* @param int $environment Environment.
181+
* @param Environment $environment Environment.
203182
* @param string $providerCertificateFile Provider certificate file with key (Bundled PEM/P8).
204183
* @param LoggerInterface $logger A Logger implementing PSR-3
205184
*/
206-
public function __construct(int $environment, string $providerCertificateFile, LoggerInterface $logger)
185+
public function __construct(Environment $environment, string $providerCertificateFile, LoggerInterface $logger)
207186
{
208-
if ($environment != self::ENVIRONMENT_PRODUCTION && $environment != self::ENVIRONMENT_SANDBOX) {
209-
throw new Exception(
210-
"Invalid environment '{$environment}'"
211-
);
212-
}
213187
$this->environment = $environment;
214188

215189
if (!is_readable($providerCertificateFile)) {
@@ -635,7 +609,7 @@ private function httpSend(Message $message, &$reply): bool
635609
CURLOPT_POST => true,
636610
CURLOPT_URL => sprintf(
637611
'%s/3/device/%s',
638-
$this->HTTPServiceURLs[$this->environment],
612+
$this->environment->getUrl(),
639613
$message->getRecipient()
640614
),
641615
CURLOPT_HTTPHEADER => $headers,

ApnsPHP/Tests/PushHttpSendTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace ApnsPHP\Tests;
1212

13+
use ApnsPHP\Environment;
1314
use ApnsPHP\Message\Priority;
1415
use ApnsPHP\Message\PushType;
1516

@@ -64,7 +65,7 @@ public function testHttpSendReturnsFalseOnCurlSessionFail(): void
6465
return false;
6566
});
6667

67-
$this->set_reflection_property_value('environment', 1);
68+
$this->set_reflection_property_value('environment', Environment::Sandbox);
6869
$this->set_reflection_property_value('hSocket', curl_init());
6970

7071
$this->setHttpHeaders();
@@ -110,7 +111,7 @@ public function testHttpSendReturnsFalseOnCurlOptsCannotBeSet(): void
110111
return 'OpenSSL SSL_read: error:14094415:SSL routines:ssl3_read_bytes:sslv3 alert certificate expired';
111112
});
112113

113-
$this->set_reflection_property_value('environment', 1);
114+
$this->set_reflection_property_value('environment', Environment::Sandbox);
114115

115116
$this->setHttpHeaders();
116117

@@ -152,7 +153,7 @@ public function testHttpSendReturnsFalseOnRequestFail(): void
152153
return 500;
153154
});
154155

155-
$this->set_reflection_property_value('environment', 1);
156+
$this->set_reflection_property_value('environment', Environment::Sandbox);
156157

157158
$this->setHttpHeaders();
158159

@@ -195,7 +196,7 @@ public function testHttpSendReturnsTrueOnSuccess(): void
195196
return 200;
196197
});
197198

198-
$this->set_reflection_property_value('environment', 1);
199+
$this->set_reflection_property_value('environment', Environment::Sandbox);
199200

200201
$this->setHttpHeaders();
201202

ApnsPHP/Tests/PushInvalidTest.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace ApnsPHP\Tests;
1111

12+
use ApnsPHP\Environment;
1213
use ApnsPHP\Push;
1314

1415
/**
@@ -26,19 +27,6 @@ public function setUp(): void
2627
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
2728
}
2829

29-
/**
30-
* Test that trying to instantiate the Push class with an invalid environment throws an exception.
31-
*
32-
* @covers \ApnsPHP\Push::__construct
33-
*/
34-
public function testConstructWithInvalidEnvironment(): void
35-
{
36-
$this->expectException('ApnsPHP\Push\Exception');
37-
$this->expectExceptionMessage("Invalid environment '3'");
38-
39-
$this->class = new Push(3, 'server_certificates_bundle_sandbox.pem', $this->logger);
40-
}
41-
4230
/**
4331
* Test that trying to instantiate the Push class with an non-existing provider certificate file
4432
* throws an exception.
@@ -50,7 +38,7 @@ public function testConstructWithNonExistingProviderCertificateFile(): void
5038
$this->expectException('ApnsPHP\Push\Exception');
5139
$this->expectExceptionMessage("Unable to read certificate file 'server_certificates_bundle_invalid.pem'");
5240

53-
$this->class = new Push(0, 'server_certificates_bundle_invalid.pem', $this->logger);
41+
$this->class = new Push(Environment::Production, 'server_certificates_bundle_invalid.pem', $this->logger);
5442
}
5543

5644
/**
@@ -63,6 +51,6 @@ public function testConstructWithUnreadableProviderCertificateFile(): void
6351
$this->expectException('ApnsPHP\Push\Exception');
6452
$this->expectExceptionMessage("Unable to read certificate file 'server_certificates_bundle_unreadable.pem'");
6553

66-
$this->class = new Push(0, 'server_certificates_bundle_unreadable.pem', $this->logger);
54+
$this->class = new Push(Environment::Production, 'server_certificates_bundle_unreadable.pem', $this->logger);
6755
}
6856
}

ApnsPHP/Tests/PushSendTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace ApnsPHP\Tests;
1212

13+
use ApnsPHP\Environment;
1314
use stdClass;
1415

1516
/**
@@ -82,7 +83,7 @@ public function testSendFailsWithoutRetrying(): void
8283
];
8384
$message = [ 1 => [ 'MESSAGE' => $this->message, 'ERRORS' => [] ] ];
8485

85-
$this->set_reflection_property_value('environment', 1);
86+
$this->set_reflection_property_value('environment', Environment::Sandbox);
8687
$this->set_reflection_property_value('hSocket', curl_init());
8788
$this->set_reflection_property_value('messageQueue', $message);
8889
$this->set_reflection_property_value('logger', $this->logger);
@@ -138,7 +139,7 @@ public function testSendFailsWithRetrying(): void
138139

139140
$message = [ 1 => [ 'MESSAGE' => $this->message, 'ERRORS' => [] ] ];
140141

141-
$this->set_reflection_property_value('environment', 1);
142+
$this->set_reflection_property_value('environment', Environment::Sandbox);
142143
$this->set_reflection_property_value('hSocket', curl_init());
143144
$this->set_reflection_property_value('messageQueue', $message);
144145
$this->set_reflection_property_value('logger', $this->logger);
@@ -213,7 +214,7 @@ public function testSendRemovesWhenNoError(): void
213214

214215
$message = [ 1 => [ 'MESSAGE' => $this->message, 'ERRORS' => [] ] ];
215216

216-
$this->set_reflection_property_value('environment', 1);
217+
$this->set_reflection_property_value('environment', Environment::Sandbox);
217218
$this->set_reflection_property_value('hSocket', curl_init());
218219
$this->set_reflection_property_value('messageQueue', $message);
219220
$this->set_reflection_property_value('logger', $this->logger);
@@ -269,7 +270,7 @@ public function testSendSuccessfullySends(): void
269270

270271
$message = [ 1 => [ 'MESSAGE' => $this->message, 'ERRORS' => [] ] ];
271272

272-
$this->set_reflection_property_value('environment', 1);
273+
$this->set_reflection_property_value('environment', Environment::Sandbox);
273274
$this->set_reflection_property_value('hSocket', curl_init());
274275
$this->set_reflection_property_value('messageQueue', $message);
275276
$this->set_reflection_property_value('logger', $this->logger);

ApnsPHP/Tests/PushTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace ApnsPHP\Tests;
1212

13+
use ApnsPHP\Environment;
1314
use ApnsPHP\Message;
1415
use ApnsPHP\Push;
1516
use Lunr\Halo\LunrBaseTest;
@@ -49,7 +50,7 @@ public function setUp(): void
4950
->getMock();
5051

5152
$this->class = new Push(
52-
Push::ENVIRONMENT_SANDBOX,
53+
Environment::Sandbox,
5354
'server_certificates_bundle_sandbox.pem',
5455
$this->logger,
5556
);

sample_push.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function log($level, $message, array $context = []): void
3030

3131
// Instantiate a new ApnsPHP_Push object
3232
$push = new \ApnsPHP\Push(
33-
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
33+
\ApnsPHP\Environment::Sandbox,
3434
'server_certificates_bundle_sandbox.pem',
3535
new SampleLogger(),
3636
);

sample_push_custom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function log($level, $message, array $context = []): void
3030

3131
// Instantiate a new ApnsPHP_Push object
3232
$push = new \ApnsPHP\Push(
33-
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
33+
\ApnsPHP\Environment::Sandbox,
3434
'server_certificates_bundle_sandbox.pem',
3535
new SampleLogger(),
3636
);

sample_push_http.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function log($level, $message, array $context = []): void
3030

3131
// Instantiate a new ApnsPHP_Push object
3232
$push = new \ApnsPHP\Push(
33-
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
33+
\ApnsPHP\Environment::Sandbox,
3434
'UniversalPushNotificationClientSSLCertificate.p8',
3535
new SampleLogger(),
3636
);

sample_push_many.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function log($level, $message, array $context = []): void
3333

3434
// Instantiate a new ApnsPHP_Push object
3535
$push = new \ApnsPHP\Push(
36-
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
36+
\ApnsPHP\Environment::Sandbox,
3737
'server_certificates_bundle_sandbox.pem',
3838
new SampleLogger(),
3939
);

0 commit comments

Comments
 (0)