Verified Commit eceb384f authored by Richard Weinhold's avatar Richard Weinhold 🔨

adds support for composer/ca-bundle to FCM-Handler

parent 81969ae7
......@@ -13,10 +13,42 @@ use RuntimeException;
*/
abstract class Handler
{
/**
* @var string[]
*/
protected array $devices = [];
protected ?string $caCertPath;
protected function setCaCertPath(?string $caCertPath): void
{
if ($caCertPath !== null) {
$this->caCertPath = $caCertPath;
} elseif (class_exists('\Composer\CaBundle\CaBundle')) {
$this->caCertPath = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();
}
}
/**
* @return array|null
*/
protected function getCurlCAPathOptions(): ?array
{
if ($this->caCertPath === null) {
return null;
}
$caCertPath = realpath($this->caCertPath);
if ($caCertPath === null || !file_exists($caCertPath) || !is_readable($caCertPath)) {
throw new RuntimeException("CA not found or not readable for path: {$this->caCertPath}", 404);
}
if (is_dir($caCertPath)) {
return [CURLOPT_CAPATH => $caCertPath];
}
return [CURLOPT_CAINFO => $caCertPath];
}
public function addDevice(string $token): void
{
......
......@@ -23,7 +23,6 @@ class APNS extends Handler
private int $port;
private string $certPath;
private ?string $certPassphrase;
private ?string $caCertPath;
private int $timeout;
public function __construct(string $environment, string $appBundleID, string $certPath, ?string $certPassphrase = null, ?string $caCertPath = null, ?string $url = null, int $timeout = 10)
......@@ -54,11 +53,7 @@ class APNS extends Handler
$this->certPassphrase = $certPassphrase;
$this->timeout = $timeout;
if ($caCertPath !== null) {
$this->caCertPath = $caCertPath;
} elseif (class_exists('\Composer\CaBundle\CaBundle')) {
$this->caCertPath = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath();
}
$this->setCaCertPath($caCertPath);
}
public function addDevice(string $token): void
......@@ -128,17 +123,8 @@ class APNS extends Handler
$options[CURLOPT_KEYPASSWD] = $this->certPassphrase;
}
if ($this->caCertPath !== null) {
$caCertPath = realpath($this->caCertPath);
if ($caCertPath === null || !file_exists($caCertPath) || !is_readable($caCertPath)) {
throw new RuntimeException("[APNS] CA not found or not readable for path: {$this->caCertPath}", 404);
}
if (is_dir($caCertPath)) {
$options[CURLOPT_CAPATH] = $caCertPath;
} else {
$options[CURLOPT_CAINFO] = $caCertPath;
}
if (null !== $caCertOptions = $this->getCurlCAPathOptions()) {
$options = array_merge($options, $caCertOptions);
}
$curl = curl_init();
......
......@@ -10,8 +10,6 @@ use ricwein\PushNotification\Message;
use RuntimeException;
/**
* Class APNSBinary
* @package ricwein\PushNotification\Handler
* @deprecated
*/
class APNSBinary extends Handler
......@@ -24,7 +22,6 @@ class APNSBinary extends Handler
private string $endpoint;
private string $certPath;
private ?string $certPassphrase;
private ?string $caCertPath;
private int $timeout;
public function __construct(string $environment, string $certPath, ?string $certPassphrase = null, ?string $caCertPath = null, ?string $url = null, int $timeout = 10)
......@@ -44,7 +41,8 @@ class APNSBinary extends Handler
$this->certPath = $certPath;
$this->certPassphrase = $certPassphrase;
$this->timeout = $timeout;
$this->caCertPath = $caCertPath;
$this->setCaCertPath($caCertPath);
}
public function addDevice(string $token): void
......
......@@ -18,11 +18,13 @@ class FCM extends Handler
private string $token;
private int $timeout;
public function __construct(string $token, string $url = self::FCM_ENDPOINT, int $timeout = 30)
public function __construct(string $token, string $url = self::FCM_ENDPOINT, ?string $caCertPath = null, int $timeout = 30)
{
$this->endpoint = $url;
$this->token = $token;
$this->timeout = $timeout;
$this->setCaCertPath($caCertPath);
}
/**
......@@ -94,6 +96,10 @@ class FCM extends Handler
CURLOPT_TIMEOUT => $this->timeout,
];
if (null !== $caCertOptions = $this->getCurlCAPathOptions()) {
$options = array_merge($options, $caCertOptions);
}
$curl = curl_init();
try {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment