Skip to content

Commit

Permalink
Push: Make the APNS environment an enum
Browse files Browse the repository at this point in the history
  • Loading branch information
pprkut committed Jul 22, 2024
1 parent 20008a1 commit 411c308
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 60 deletions.
51 changes: 51 additions & 0 deletions ApnsPHP/Environment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/**
* APNS Environment
*
* SPDX-FileCopyrightText: Copyright 2024 Move Agency Group B.V., Zwolle, The Netherlands
* SPDX-License-Identifier: BSD-2-Clause
*/

namespace ApnsPHP;

/**
* APNS Environment.
*/
enum Environment
{
/**
* Production environment.
*/
case Production;

/**
* Production environment using alternative port.
*/
case AltProduction;

/**
* Sandbox environment.
*/
case Sandbox;

/**
* Sandbox environment using alternative port.
*/
case AltSandbox;

/**
* Get HTTP URL for a given environment
*
* @return string The URL to the matching APNS environment
*/
public function getUrl(): string
{
return match ($this) {
Environment::Production => 'https://api.push.apple.com:443',
Environment::AltProduction => 'https://api.push.apple.com:2197',
Environment::Sandbox => 'https://api.sandbox.push.apple.com:443',
Environment::AltSandbox => 'https://api.sandbox.push.apple.com:2197',
};
}
}
36 changes: 5 additions & 31 deletions ApnsPHP/Push.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,6 @@
*/
class Push
{
/**
* Production environment.
* @var int
*/
public const ENVIRONMENT_PRODUCTION = 0;

/**
* Sandbox environment.
* @var int
*/
public const ENVIRONMENT_SANDBOX = 1;

/**
* Device token length.
* @var int
Expand Down Expand Up @@ -82,9 +70,9 @@ class Push

/**
* Active environment.
* @var int
* @var Environment
*/
protected int $environment;
protected Environment $environment;

/**
* Connect timeout in seconds.
Expand Down Expand Up @@ -175,15 +163,6 @@ class Push
*/
protected int $sendRetryTimes = 3;

/**
* HTTP/2 Service URLs environments.
* @var array<int,string>
*/
protected array $HTTPServiceURLs = [
'https://api.push.apple.com:443', // Production environment
'https://api.development.push.apple.com:443' // Sandbox environment
];

/**
* Message queue.
* @var array
Expand All @@ -199,17 +178,12 @@ class Push
/**
* Constructor.
*
* @param int $environment Environment.
* @param Environment $environment Environment.
* @param string $providerCertificateFile Provider certificate file with key (Bundled PEM/P8).
* @param LoggerInterface $logger A Logger implementing PSR-3
*/
public function __construct(int $environment, string $providerCertificateFile, LoggerInterface $logger)
public function __construct(Environment $environment, string $providerCertificateFile, LoggerInterface $logger)
{
if ($environment != self::ENVIRONMENT_PRODUCTION && $environment != self::ENVIRONMENT_SANDBOX) {
throw new Exception(
"Invalid environment '{$environment}'"
);
}
$this->environment = $environment;

if (!is_readable($providerCertificateFile)) {
Expand Down Expand Up @@ -635,7 +609,7 @@ private function httpSend(Message $message, &$reply): bool
CURLOPT_POST => true,
CURLOPT_URL => sprintf(
'%s/3/device/%s',
$this->HTTPServiceURLs[$this->environment],
$this->environment->getUrl(),
$message->getRecipient()
),
CURLOPT_HTTPHEADER => $headers,
Expand Down
9 changes: 5 additions & 4 deletions ApnsPHP/Tests/PushHttpSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace ApnsPHP\Tests;

use ApnsPHP\Environment;
use ApnsPHP\Message\Priority;
use ApnsPHP\Message\PushType;

Expand Down Expand Up @@ -64,7 +65,7 @@ public function testHttpSendReturnsFalseOnCurlSessionFail(): void
return false;
});

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);
$this->set_reflection_property_value('hSocket', curl_init());

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

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);

$this->setHttpHeaders();

Expand Down Expand Up @@ -152,7 +153,7 @@ public function testHttpSendReturnsFalseOnRequestFail(): void
return 500;
});

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);

$this->setHttpHeaders();

Expand Down Expand Up @@ -195,7 +196,7 @@ public function testHttpSendReturnsTrueOnSuccess(): void
return 200;
});

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);

$this->setHttpHeaders();

Expand Down
18 changes: 3 additions & 15 deletions ApnsPHP/Tests/PushInvalidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace ApnsPHP\Tests;

use ApnsPHP\Environment;
use ApnsPHP\Push;

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

/**
* Test that trying to instantiate the Push class with an invalid environment throws an exception.
*
* @covers \ApnsPHP\Push::__construct
*/
public function testConstructWithInvalidEnvironment(): void
{
$this->expectException('ApnsPHP\Push\Exception');
$this->expectExceptionMessage("Invalid environment '3'");

$this->class = new Push(3, 'server_certificates_bundle_sandbox.pem', $this->logger);
}

/**
* Test that trying to instantiate the Push class with an non-existing provider certificate file
* throws an exception.
Expand All @@ -50,7 +38,7 @@ public function testConstructWithNonExistingProviderCertificateFile(): void
$this->expectException('ApnsPHP\Push\Exception');
$this->expectExceptionMessage("Unable to read certificate file 'server_certificates_bundle_invalid.pem'");

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

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

$this->class = new Push(0, 'server_certificates_bundle_unreadable.pem', $this->logger);
$this->class = new Push(Environment::Production, 'server_certificates_bundle_unreadable.pem', $this->logger);
}
}
9 changes: 5 additions & 4 deletions ApnsPHP/Tests/PushSendTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace ApnsPHP\Tests;

use ApnsPHP\Environment;
use stdClass;

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

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);
$this->set_reflection_property_value('hSocket', curl_init());
$this->set_reflection_property_value('messageQueue', $message);
$this->set_reflection_property_value('logger', $this->logger);
Expand Down Expand Up @@ -138,7 +139,7 @@ public function testSendFailsWithRetrying(): void

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

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);
$this->set_reflection_property_value('hSocket', curl_init());
$this->set_reflection_property_value('messageQueue', $message);
$this->set_reflection_property_value('logger', $this->logger);
Expand Down Expand Up @@ -213,7 +214,7 @@ public function testSendRemovesWhenNoError(): void

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

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);
$this->set_reflection_property_value('hSocket', curl_init());
$this->set_reflection_property_value('messageQueue', $message);
$this->set_reflection_property_value('logger', $this->logger);
Expand Down Expand Up @@ -269,7 +270,7 @@ public function testSendSuccessfullySends(): void

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

$this->set_reflection_property_value('environment', 1);
$this->set_reflection_property_value('environment', Environment::Sandbox);
$this->set_reflection_property_value('hSocket', curl_init());
$this->set_reflection_property_value('messageQueue', $message);
$this->set_reflection_property_value('logger', $this->logger);
Expand Down
3 changes: 2 additions & 1 deletion ApnsPHP/Tests/PushTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace ApnsPHP\Tests;

use ApnsPHP\Environment;
use ApnsPHP\Message;
use ApnsPHP\Push;
use Lunr\Halo\LunrBaseTest;
Expand Down Expand Up @@ -49,7 +50,7 @@ public function setUp(): void
->getMock();

$this->class = new Push(
Push::ENVIRONMENT_SANDBOX,
Environment::Sandbox,
'server_certificates_bundle_sandbox.pem',
$this->logger,
);
Expand Down
2 changes: 1 addition & 1 deletion sample_push.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function log($level, $message, array $context = []): void

// Instantiate a new ApnsPHP_Push object
$push = new \ApnsPHP\Push(
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
\ApnsPHP\Environment::Sandbox,
'server_certificates_bundle_sandbox.pem',
new SampleLogger(),
);
Expand Down
2 changes: 1 addition & 1 deletion sample_push_custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function log($level, $message, array $context = []): void

// Instantiate a new ApnsPHP_Push object
$push = new \ApnsPHP\Push(
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
\ApnsPHP\Environment::Sandbox,
'server_certificates_bundle_sandbox.pem',
new SampleLogger(),
);
Expand Down
2 changes: 1 addition & 1 deletion sample_push_http.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function log($level, $message, array $context = []): void

// Instantiate a new ApnsPHP_Push object
$push = new \ApnsPHP\Push(
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
\ApnsPHP\Environment::Sandbox,
'UniversalPushNotificationClientSSLCertificate.p8',
new SampleLogger(),
);
Expand Down
2 changes: 1 addition & 1 deletion sample_push_many.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function log($level, $message, array $context = []): void

// Instantiate a new ApnsPHP_Push object
$push = new \ApnsPHP\Push(
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
\ApnsPHP\Environment::Sandbox,
'server_certificates_bundle_sandbox.pem',
new SampleLogger(),
);
Expand Down
2 changes: 1 addition & 1 deletion sample_push_safari.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function log($level, $message, array $context = []): void

// Instantiate a new ApnsPHP_Push object
$push = new \ApnsPHP\Push(
\ApnsPHP\Push::ENVIRONMENT_SANDBOX,
\ApnsPHP\Environment::Sandbox,
'server_certificates_bundle_sandbox.pem',
new SampleLogger(),
);
Expand Down

0 comments on commit 411c308

Please sign in to comment.