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: 38 additions & 0 deletions .github/workflows/lint-php-cs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Lint php-cs

on: pull_request

permissions:
contents: read

concurrency:
group: lint-php-cs-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

jobs:
lint:
runs-on: ubuntu-latest

name: php-cs

steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

- name: Set up php8.1
uses: shivammathur/setup-php@0f7f1d08e3e32076e51cae65eb0b0c871405b16e # v2.34.1
with:
php-version: 8.1
extensions: json, openssl
coverage: none
ini-file: development
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: composer i

- name: Lint
run: composer run cs:check || ( echo 'Please run `composer run cs:fix` to format your code' && exit 1 )
39 changes: 39 additions & 0 deletions .github/workflows/psalm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Static analysis

on: pull_request

concurrency:
group: psalm-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

permissions:
contents: read

jobs:
static-analysis:
runs-on: ubuntu-latest

name: static-psalm-analysis
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
with:
persist-credentials: false

- name: Set up php8.1
uses: shivammathur/setup-php@ccf2c627fe61b1b4d924adfcbd19d661a18133a0 # v2.35.2
with:
php-version: 8.1
extensions: json, openssl
coverage: none
ini-file: development
# Temporary workaround for missing pcntl_* in PHP 8.3
ini-values: disable_functions=
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: composer i

- name: Run coding standards check
run: composer run psalm -- --output-format=github
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.idea
.vscode
.phpunit.result.cache
.php-cs-fixer.cache
vendor
/vendor-bin/**/vendor/
17 changes: 17 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

require_once './vendor-bin/coding-standard/vendor/autoload.php';

use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;

$config = new PhpCsFixer\Config();
$config
->setParallelConfig(ParallelConfigFactory::detect())
->getFinder()
->ignoreVCSIgnored(true)
->notPath('vendor')
->notPath('vendor-bin')
->in(__DIR__);
return $config;
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,19 @@
}
},
"scripts": {
"cs:check": "php-cs-fixer fix --dry-run --diff",
"cs:fix": "php-cs-fixer fix",
"test:unit": "vendor/bin/phpunit --no-coverage --colors=always --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations",
"test:coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit",
"psalm": "psalm --no-cache --threads=$(nproc)",
"psalm:update-baseline": "psalm --threads=$(nproc) --update-baseline --set-baseline=tests/psalm-baseline.xml",
"post-install-cmd": [
"@composer bin all install --ansi",
"composer dump-autoload"
],
"post-update-cmd": [
"composer dump-autoload"
],
"test:unit": "vendor/bin/phpunit --no-coverage --colors=always --fail-on-warning --fail-on-risky --display-deprecations --display-phpunit-deprecations",
"test:coverage": "XDEBUG_MODE=coverage vendor/bin/phpunit"
]
},
"extra": {
"bamarni-bin": {
Expand Down
29 changes: 29 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<psalm
errorBaseline="tests/psalm-baseline.xml"
errorLevel="8"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
phpVersion="8.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor-bin/psalm/vendor/vimeo/psalm/config.xsd"
>
<projectFiles>
<directory name="src" />
<ignoreFiles>
<directory name="vendor" />
</ignoreFiles>
</projectFiles>
<issueHandlers>
<LessSpecificReturnStatement errorLevel="error"/>
<LessSpecificReturnType errorLevel="error"/>
<LessSpecificImplementedReturnType errorLevel="error"/>
<MoreSpecificReturnType errorLevel="error"/>
<UnusedVariable errorLevel="error"/>
<UnusedMethod errorLevel="error"/>
<UnusedClass errorLevel="error"/>
<RedundantCondition errorLevel="error"/>
</issueHandlers>
</psalm>
20 changes: 12 additions & 8 deletions src/JSignFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,43 @@
*/
class JSignFileService
{

public static function instance()
public static function instance(): self
{
return new self();
}

public function contentFile($path, $isInBase64 = false)
public function contentFile(string $path, bool $isInBase64 = false): string
{
$content = file_get_contents($path);
if ($content === false) {
return '';
}
return $isInBase64 ? base64_encode($content) : $content;
}

public function storeFile($path, $name, $content)
public function storeFile(string $path, string $name, string $content): string
{
$filename = $path . $name;
file_put_contents($filename, $content);
return $filename;
}

public function deleteFile(string $path)
public function deleteFile(string $path): void
{
if (is_file($path))
if (is_file($path)) {
unlink($path);
}
}

public function deleteTempFiles(string $pathTemp, string $name)
public function deleteTempFiles(string $pathTemp, string $name): void
{
$pathPfxFile = "$pathTemp$name.pfx";
$pathPdfFile = "$pathTemp$name.pdf";
$pathPdfSignedFile = "{$pathTemp}{$name}_signed.pdf";
$tempFiles = [$pathPfxFile, $pathPdfFile, $pathPdfSignedFile];
foreach ($tempFiles as $path)
foreach ($tempFiles as $path) {
$this->deleteFile($path);
}
}

}
19 changes: 13 additions & 6 deletions src/JSignPDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jeidison\JSignPDF;

use Exception;
use Jeidison\JSignPDF\Sign\JSignParam;
use Jeidison\JSignPDF\Sign\JSignService;

Expand All @@ -10,31 +11,37 @@
*/
class JSignPDF
{
private $service;
private $param;
private JSignService $service;
private ?JSignParam $param = null;

public function __construct(?JSignParam $param = null)
{
$this->service = new JSignService();
$this->param = $param;
}

public static function instance(?JSignParam $param = null)
public static function instance(?JSignParam $param = null): self
{
return new self($param);
}

public function sign()
public function sign(): string
{
if (!$this->param instanceof JSignParam) {
throw new Exception('Invalid JSignParam instance');
}
return $this->service->sign($this->param);
}

public function getVersion()
public function getVersion(): string
{
if (!$this->param instanceof JSignParam) {
throw new Exception('Invalid JSignParam instance');
}
return $this->service->getVersion($this->param);
}

public function setParam(JSignParam $param)
public function setParam(JSignParam $param): void
{
$this->param = $param;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Runtime/JSignPdfRuntimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function getPath(JSignParam $params): string

if ($downloadUrl && $jsignPdfPath) {
$baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath);
if (!is_string($baseDir)) {
throw new InvalidArgumentException('Invalid JsignParamPath');
}
if (!is_dir($baseDir)) {
$ok = mkdir($baseDir, 0755, true);
if ($ok === false) {
Expand Down Expand Up @@ -53,7 +56,9 @@ private function downloadAndExtract(JSignParam $params): void
$url = $params->getJSignPdfDownloadUrl();

$baseDir = preg_replace('/\/JSignPdf.jar$/', '', $jsignPdfPath);

if (!is_string($baseDir)) {
throw new InvalidArgumentException('Invalid JsignParamPath');
}
if (!is_dir($baseDir)) {
$ok = mkdir($baseDir, 0755, true);
if (!$ok) {
Expand Down Expand Up @@ -89,6 +94,9 @@ private function chunkDownload(string $url, string $destination): void

if ($fp) {
$ch = curl_init($url);
if ($ch === false) {
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Expand Down
21 changes: 19 additions & 2 deletions src/Runtime/JavaRuntimeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public function getPath(JSignParam $params): string

if ($downloadUrl && $javaPath) {
$baseDir = preg_replace('/\/bin\/java$/', '', $javaPath);
if (!is_string($baseDir)) {
throw new InvalidArgumentException('Invalid JsignParamPath');
}
if (!is_dir($baseDir)) {
$ok = mkdir($baseDir, 0755, true);
if ($ok === false) {
Expand All @@ -52,13 +55,19 @@ private function validateVersion(JSignParam $params): bool
{
$javaPath = $params->getJavaPath();
$baseDir = preg_replace('/\/bin\/java$/', '', $javaPath);
if (!is_string($baseDir)) {
throw new InvalidArgumentException('Invalid JsignParamPath');
}
$lastVersion = $baseDir . '/.java_version_' . basename($params->getJavaDownloadUrl());
return file_exists($lastVersion);
}

private function downloadAndExtract(string $url, string $baseDir): void
{
$baseDir = preg_replace('/\/bin\/java$/', '', $baseDir);
if (!is_string($baseDir)) {
throw new InvalidArgumentException('Invalid JsignParamPath');
}

if (!is_dir($baseDir)) {
$ok = mkdir($baseDir, 0755, true);
Expand All @@ -76,7 +85,7 @@ private function downloadAndExtract(string $url, string $baseDir): void
throw new InvalidArgumentException('The file ' . $baseDir . '/java.tar.gz cannot be extracted');
}
$rootDirInsideTar = $this->findRootDir($tar, $baseDir . '/java.tar.gz');
if (!$rootDirInsideTar) {
if (empty($rootDirInsideTar)) {
throw new InvalidArgumentException('Invalid tar content.');
}
$tar->extractTo(directory: $baseDir, overwrite: true);
Expand All @@ -91,9 +100,13 @@ private function downloadAndExtract(string $url, string $baseDir): void
chmod($baseDir . '/bin/java', 0700);
}

private function findRootDir(PharData $phar, $rootDir) {
private function findRootDir(PharData $phar, string $rootDir): string
{
$files = new \RecursiveIteratorIterator($phar, \RecursiveIteratorIterator::CHILD_FIRST);
$rootDir = realpath($rootDir);
if (!is_string($rootDir) || empty($rootDir)) {
throw new InvalidArgumentException('Invalid tar content.');
}

foreach ($files as $file) {
$pathName = $file->getPathname();
Expand All @@ -104,6 +117,7 @@ private function findRootDir(PharData $phar, $rootDir) {
return trim($parts[0], '/');
}
}
return '';
}

private function chunkDownload(string $url, string $destination): void
Expand All @@ -112,6 +126,9 @@ private function chunkDownload(string $url, string $destination): void

if ($fp) {
$ch = curl_init($url);
if ($ch === false) {
throw new InvalidArgumentException('Failure to download file using the url ' . $url);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
Expand Down
Loading