Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 27 additions & 11 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -28,22 +28,44 @@ jobs:
run: bin/phpcs --config-set show_warnings 0

- name: Check style on sources
run: bin/phpcs --standard=vendor/escapestudios/symfony2-coding-standard/Symfony/ src/
run: bin/phpcs --standard=src/phpcs.xml src/

- name: Check style on tests
run: bin/phpcs --standard=tests/phpcs-ruleset.xml tests/
run: bin/phpcs --standard=src/phpcs.xml tests/

phpstan:
name: Run PHPStan
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
coverage: "none"
extensions: "json"
php-version: "8.4"
tools: "composer"

- name: Install vendors
run: composer install --prefer-dist

- name: Run phpstan
run: bin/phpstan

tests:
name: Run tests
runs-on: ubuntu-latest

strategy:
matrix:
php: [ '7.2', '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]
php: [ '7.4', '8.0', '8.1', '8.2', '8.3', '8.4' ]

steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -58,9 +80,3 @@ jobs:

- name: Run tests
run: bin/phpunit

- name: Install vendors (lowest)
run: composer update --prefer-lowest

- name: Run tests
run: bin/phpunit
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
],

"require": {
"php": "^7.1 | ^8.0",
"php": "^7.4 | ^8.0",
"ext-curl": "*",
"ext-json": "*"
},

"require-dev": {
"phpunit/phpunit": "^8.0 | ^9.0 | ^10.0",
"phpunit/phpunit": "^9.0 | ^10.0",
"web-token/jwt-key-mgmt": "~2.0",
"web-token/jwt-signature": "~2.0",
"web-token/jwt-signature-algorithm-ecdsa": "~2.0",
"phpmetrics/phpmetrics": "^2.0",
"phpmetrics/phpmetrics": "~2.0",
"phpstan/phpstan": "~2.1",
"escapestudios/symfony2-coding-standard": "~3.0",
"psr/simple-cache": "~1.0"
},
Expand Down
25 changes: 25 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
parameters:
level: 8
paths:
- src

checkUninitializedProperties: true

ignoreErrors:
-
identifier: missingType.iterableValue

-
message: '/Parameter #1 \$handle of function curl_([^\s]+) expects CurlHandle, CurlHandle\|resource(\|null)? given\./'
path: src

-
message: '/Parameter #3 \$value of function curl_setopt expects (bool, int given|non-empty-string, string given)\./'

-
identifier: class.notFound
path: src/Jwt/SignatureGenerator/SignatureGeneratorFactory.php

-
identifier: class.notFound
path: src/Jwt/SignatureGenerator/SpomkyLabsJoseSignatureGenerator.php
20 changes: 9 additions & 11 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
failOnWarning="true"
failOnNotice="true"
bootstrap="./vendor/autoload.php"
displayDetailsOnAllIssues="true"
>
<testsuites>
<testsuite name="Apple ApnPush tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<source>
<include>
<directory>src</directory>
</whitelist>
</filter>
</include>
</source>
</phpunit>
32 changes: 2 additions & 30 deletions src/Certificate/Certificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,11 @@

use Apple\ApnPush\Exception\CertificateFileNotFoundException;

/**
* Base certificate
*/
class Certificate implements CertificateInterface
{
/**
* @var string
*/
private $path;

/**
* @var string
*/
private $passPhrase;
private string $path;
private string $passPhrase;

/**
* Construct
*
* @param string $path
* @param string $passPhrase
*
* @throws CertificateFileNotFoundException
*/
public function __construct(string $path, string $passPhrase)
{
if (!\file_exists($path) || !\is_file($path)) {
Expand All @@ -51,21 +33,11 @@ public function __construct(string $path, string $passPhrase)
$this->passPhrase = $passPhrase;
}

/**
* Get path
*
* @return string
*/
public function getPath(): string
{
return $this->path;
}

/**
* Get pass phrase
*
* @return string
*/
public function getPassPhrase(): string
{
return $this->passPhrase;
Expand Down
14 changes: 0 additions & 14 deletions src/Certificate/CertificateInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,8 @@

namespace Apple\ApnPush\Certificate;

/**
* All certificate should implement this interface
*/
interface CertificateInterface
{
/**
* Get path
*
* @return string
*/
public function getPath(): string;

/**
* Get pass phrase
*
* @return string
*/
public function getPassPhrase(): string;
}
65 changes: 12 additions & 53 deletions src/Certificate/ContentCertificate.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,81 +21,42 @@
*/
class ContentCertificate implements CertificateInterface
{
/**
* @var string
*/
private $content;

/**
* @var string
*/
private $passPhrase;

/**
* @var string
*/
private $tmpDir;

/**
* @var string
*/
private $certificateFilePath;

/**
* Construct
*
* @param string $content
* @param string $passPhrase
* @param string $tmpDir
*/
private string $content;
private string $passPhrase;
private string $tmpDir;
private ?string $certificateFilePath = null;

public function __construct(string $content, string $passPhrase, string $tmpDir)
{
$this->content = $content;
$this->passPhrase = $passPhrase;
$this->tmpDir = $tmpDir;
}

/**
* {@inheritdoc}
*/
public function getPath(): string
{
if ($this->certificateFilePath) {
$this->removeTemporaryFile($this->certificateFilePath);
}

$this->certificateFilePath = $this->createTemporaryFile();
file_put_contents($this->certificateFilePath, $this->content);
\file_put_contents($this->certificateFilePath, $this->content);

return $this->certificateFilePath;
}

/**
* {@inheritdoc}
*/
public function getPassPhrase(): string
{
return $this->passPhrase;
}

/**
* Implement __destruct
* Remove temporary file
*/
public function __destruct()
{
if ($this->certificateFilePath) {
$this->removeTemporaryFile($this->certificateFilePath);
}
}

/**
* Create a temporary file
*
* @return string Path to temporary file
*
* @throws \RuntimeException
*/
private function createTemporaryFile(): string
{
$tmpDir = $this->tmpDir;
Expand All @@ -106,9 +67,11 @@ private function createTemporaryFile(): string

$errorCode = $errorMessage = null;

\set_error_handler(function ($errCode, $errMessage) use (&$errorCode, &$errorMessage) {
\set_error_handler(static function (int $errCode, string $errMessage) use (&$errorCode, &$errorMessage) {
$errorCode = $errCode;
$errorMessage = $errMessage;

return true;
});

if (!\file_exists($tmpDir)) {
Expand Down Expand Up @@ -146,15 +109,11 @@ private function createTemporaryFile(): string
return $tmpFilePath;
}

/**
* Remove temporary file
*
* @param string $filePath
*/
private function removeTemporaryFile($filePath): void
private function removeTemporaryFile(string $filePath): void
{
// Set custom error handler for suppress error
\set_error_handler(function () {
\set_error_handler(static function () {
return true;
});

\unlink($filePath);
Expand Down
22 changes: 1 addition & 21 deletions src/Encoder/PayloadEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@
use Apple\ApnPush\Model\Payload;
use Apple\ApnPush\Model\Sound;

/**
* The encoder for encode notification payload to string for next send to Apple Push Notification Service
*/
class PayloadEncoder implements PayloadEncoderInterface
{
/**
* {@inheritdoc}
*/
public function encode(Payload $payload): string
{
$data = [
Expand All @@ -34,16 +28,9 @@ public function encode(Payload $payload): string

$data = \array_merge($payload->getCustomData(), $data);

return \json_encode($data);
return \json_encode($data, JSON_THROW_ON_ERROR);
}

/**
* Convert APS data to array
*
* @param Aps $aps
*
* @return array
*/
private function convertApsToArray(Aps $aps): array
{
$data = [];
Expand Down Expand Up @@ -94,13 +81,6 @@ private function convertApsToArray(Aps $aps): array
return \array_merge($aps->getCustomData(), $data);
}

/**
* Convert alert object to array
*
* @param Alert $alert
*
* @return array
*/
private function convertAlertToArray(Alert $alert): array
{
$data = [];
Expand Down
Loading