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
37 changes: 22 additions & 15 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,41 @@ name: build
on:
push:
branches: [master]
pull_request:
branches: [master]
jobs:
run:
runs-on: ${{ matrix.operating-system }}
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
operating-system: [ubuntu-latest]
php-versions:
["7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
name: PHP ${{ matrix.php-versions }}
php-version: ["7.1", "7.2", "7.3", "7.4", "8.0", "8.1", "8.2", "8.3", "8.4"]
name: PHP ${{ matrix.php-version }}
steps:
- uses: actions/checkout@v4
- name: Update advanced packaging tools
run: sudo apt update
- uses: actions/checkout@v5.0.1

- name: Install Ghostscript
run: sudo apt install ghostscript
- name: Install PHP
run: sudo apt-get update -qq && sudo apt-get install -y ghostscript

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
php-version: ${{ matrix.php-version }}
coverage: xdebug
tools: composer:v2

- name: Install dependencies
run: composer self-update && composer install && composer dump-autoload
- name: Copy .env.example to .env
run: composer install --no-interaction --prefer-dist

- name: Copy .env
run: cp .env.example .env

- name: Run tests
run: composer test

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
if: matrix.php-version == '8.4'
uses: codecov/codecov-action@v6
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ./coverage.xml
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ vendor/
report/
files/split/parts/
files/merge/res.pdf
files/merge/single.pdf
files/to-image/images/
composer.lock
coverage.xml
phpunit.xml
.phpunit.result.cache
.DS_Store
.env
.env
.repomix/
repomix-output.txt
.claude/
Dockerfile
docker-compose.yml
2 changes: 2 additions & 0 deletions src/Configs/Config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Configs;

use Ordinary9843\Helpers\PathHelper;
Expand Down
2 changes: 2 additions & 0 deletions src/Constants/GhostscriptConstant.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Constants;

class GhostscriptConstant
Expand Down
2 changes: 2 additions & 0 deletions src/Constants/ImageTypeConstant.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Constants;

class ImageTypeConstant
Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/BaseException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Exceptions;

use Exception;
Expand All @@ -18,7 +20,7 @@ class BaseException extends Exception
* @param array $detail
* @param BaseException $previous
*/
public function __construct(string $message = 'An unexpected error occurred.', int $code = self::CODE_DEFAULT, array $detail = [], BaseException $previous = null)
public function __construct(string $message = 'An unexpected error occurred.', int $code = self::CODE_DEFAULT, array $detail = [], ?BaseException $previous = null)
{
parent::__construct($message, $code, $previous);

Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/ConfigException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Exceptions;

class ConfigException extends BaseException
Expand All @@ -19,7 +21,7 @@ class ConfigException extends BaseException
* @param array $detail
* @param BaseException $previous
*/
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], BaseException $previous = null)
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], ?BaseException $previous = null)
{
parent::__construct($message, $code, $detail, $previous);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/HandlerException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Exceptions;

class HandlerException extends BaseException
Expand All @@ -16,7 +18,7 @@ class HandlerException extends BaseException
* @param array $detail
* @param BaseException $previous
*/
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], BaseException $previous = null)
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], ?BaseException $previous = null)
{
parent::__construct($message, $code, $detail, $previous);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/InvalidException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Exceptions;

class InvalidException extends BaseException
Expand All @@ -22,7 +24,7 @@ class InvalidException extends BaseException
* @param array $detail
* @param BaseException $previous
*/
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], BaseException $previous = null)
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], ?BaseException $previous = null)
{
parent::__construct($message, $code, $detail, $previous);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Exceptions/NotFoundException.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Exceptions;

class NotFoundException extends BaseException
Expand All @@ -19,7 +21,7 @@ class NotFoundException extends BaseException
* @param array $detail
* @param BaseException $previous
*/
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], BaseException $previous = null)
public function __construct(string $message, int $code = self::CODE_DEFAULT, array $detail = [], ?BaseException $previous = null)
{
parent::__construct($message, $code, $detail, $previous);
}
Expand Down
30 changes: 25 additions & 5 deletions src/Factories/HandlerFactory.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,46 @@
<?php

declare(strict_types=1);

namespace Ordinary9843\Factories;

use Ordinary9843\Handlers\BaseHandler;
use Ordinary9843\Handlers\ConvertHandler;
use Ordinary9843\Handlers\GetTotalPagesHandler;
use Ordinary9843\Handlers\GuessHandler;
use Ordinary9843\Handlers\MergeHandler;
use Ordinary9843\Handlers\SplitHandler;
use Ordinary9843\Handlers\ToImageHandler;
use Ordinary9843\Interfaces\HandlerInterface;
use Ordinary9843\Exceptions\NotFoundException;

class HandlerFactory
{
private const HANDLER_MAP = [
'base' => BaseHandler::class,
'convert' => ConvertHandler::class,
'guess' => GuessHandler::class,
'getTotalPages' => GetTotalPagesHandler::class,
'merge' => MergeHandler::class,
'split' => SplitHandler::class,
'toImage' => ToImageHandler::class,
];

/**
* @param string $type
*
*
* @return HandlerInterface
*
*
* @throws NotFoundException
*/
public function create(string $type): HandlerInterface
{
$class = 'Ordinary9843\\Handlers\\' . ucfirst($type) . 'Handler';
if (!class_exists($class)) {
throw new NotFoundException('Class "' . $class . '" does not exist.', NotFoundException::CODE_CLASS);
if (!isset(self::HANDLER_MAP[$type])) {
throw new NotFoundException('Handler "' . $type . '" does not exist.', NotFoundException::CODE_CLASS);
}

$class = self::HANDLER_MAP[$type];

return new $class();
}
}
125 changes: 75 additions & 50 deletions src/Ghostscript.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
<?php

declare(strict_types=1);

namespace Ordinary9843;

use Ordinary9843\Configs\Config;
use Ordinary9843\Factories\HandlerFactory;
use Ordinary9843\Exceptions\InvalidException;
use Ordinary9843\Interfaces\HandlerInterface;
use Ordinary9843\Constants\ImageTypeConstant;

/**
* @method string convert(string $file, float $version)
* @method float guess(string $file)
* @method string merge(string $path, string $filename, array $files, bool $isAutoConvert = true)
* @method array split(string $file, string $path)
* @method array toImage(string $file, string $path, string $type = 'jpeg')
* @method int getTotalPages(string $file)
* @method void clearTmpFiles(bool $isForceClear = false, int $days = 7)
* @method void setBinPath(string $binPath)
* @method string getBinPath()
* @method void setTmpPath(string $tmpPath)
* @method string getTmpPath()
* @method void setOptions(array $options)
* @method array getOptions()
*/
class Ghostscript
{
/** @var HandlerInterface[] */
Expand All @@ -44,53 +32,90 @@ public function __construct(string $binPath = '', string $tmpPath = '')
Config::initialize($this->arguments);
}

public function convert(string $file, float $version): string
{
return $this->createHandler('convert')->execute($file, $version);
}

public function guess(string $file): float
{
return $this->createHandler('guess')->execute($file);
}

public function merge(string $path, string $filename, array $files, bool $isAutoConvert = true): string
{
return $this->createHandler('merge')->execute($path, $filename, $files, $isAutoConvert);
}

public function split(string $file, string $path): array
{
return $this->createHandler('split')->execute($file, $path);
}

public function toImage(string $file, string $path, string $type = ImageTypeConstant::JPEG): array
{
return $this->createHandler('toImage')->execute($file, $path, $type);
}

public function getTotalPages(string $file): int
{
return $this->createHandler('getTotalPages')->execute($file);
}

public function clearTmpFiles(bool $isForceClear = false, int $days = 7): void
{
$this->createBaseHandler()->clearTmpFiles($isForceClear, $days);
}

public function setBinPath(string $binPath): void
{
$this->createBaseHandler()->setBinPath($binPath);
}

public function getBinPath(): string
{
return $this->createBaseHandler()->getBinPath();
}

public function setTmpPath(string $tmpPath): void
{
$this->createBaseHandler()->setTmpPath($tmpPath);
}

public function getTmpPath(): string
{
return $this->createBaseHandler()->getTmpPath();
}

public function setOptions(array $options): void
{
$this->createBaseHandler()->setOptions($options);
}

public function getOptions(): array
{
return $this->createBaseHandler()->getOptions();
}

/**
* @param string $name
* @param array $arguments
*
*
* @return mixed
*
*
* @throws InvalidException
*/
public function __call(string $name, array $arguments)
{
switch ($name) {
case 'convert':
case 'guess':
case 'merge':
case 'split':
case 'toImage':
case 'getTotalPages':
$handler = $this->createHandler($name);

return $handler->execute(...$arguments);
case 'getBinPath':
case 'getTmpPath':
case 'getOptions':
case 'clearTmpFiles':
$handler = $this->createBaseHandler();

return $handler->{$name}();
case 'setBinPath':
case 'setTmpPath':
$handler = $this->createBaseHandler();

return $handler->{$name}(current($arguments));
case 'setOptions':
$handler = $this->createBaseHandler();

return $handler->{$name}(...$arguments);
default:
throw new InvalidException('Invalid method: "' . $name . '".', InvalidException::CODE_METHOD, [
'name' => $name,
'arguments' => $arguments
]);
}
throw new InvalidException('Invalid method: "' . $name . '".', InvalidException::CODE_METHOD, [
'name' => $name,
'arguments' => $arguments
]);
}

/**
* @param string $name
*
*
* @return HandlerInterface
*/
private function createHandler(string $name): HandlerInterface
Expand Down
Loading