Skip to content
This repository has been archived by the owner on Apr 14, 2024. It is now read-only.

Commit

Permalink
Merge pull request #22 from rodnaph/remove-safe
Browse files Browse the repository at this point in the history
Remove Safe
  • Loading branch information
gulien authored Dec 30, 2020
2 parents 120fc00 + 01dbffe commit 97cb313
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 27 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: "Continuous Integration"

on:
pull_request:
pull_request_target:
push:
branches:
- master

jobs:
ci:
name: "Run Build"
runs-on: ubuntu-latest

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

- name: "Run Lint"
run: make lint

- name: "Run Tests"
run: make test
10 changes: 0 additions & 10 deletions .travis.yml

This file was deleted.

4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
"php-http/httplug": ">=1.0",
"php-http/discovery": "^1.0",
"guzzlehttp/psr7": "^1.4.2",
"php-http/message": "^1.0",
"thecodingmachine/safe": "^1.0"
"php-http/message": "^1.0"
},
"require-dev" : {
"phpunit/phpunit": "^7",
"squizlabs/php_codesniffer": "^3.2",
"phpstan/phpstan": "^0.12.7",
"thecodingmachine/phpstan-safe-rule": "^1.0",
"thecodingmachine/phpstan-strict-rules": "^0.12.0",
"php-http/mock-client": "^1.0",
"php-http/guzzle6-adapter": "^1.1",
Expand Down
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
includes:
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
- vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon
- vendor/thecodingmachine/phpstan-strict-rules/phpstan-strict-rules.neon
20 changes: 14 additions & 6 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@
use Http\Discovery\MessageFactoryDiscovery;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Safe\Exceptions\FilesystemException;
use function Safe\fclose;
use function Safe\fopen;
use function Safe\fwrite;
use function fclose;
use function fopen;
use function fwrite;

final class Client
{
Expand Down Expand Up @@ -57,8 +56,17 @@ public function store(GotenbergRequestInterface $request, string $destination):
$response = $this->handleResponse($this->client->sendRequest($this->makeMultipartFormDataRequest($request)));
$fileStream = $response->getBody();
$fp = fopen($destination, 'w');
fwrite($fp, $fileStream->getContents());
fclose($fp);
if ($fp === false) {
throw FilesystemException::createFromPhpError();
}

if (fwrite($fp, $fileStream->getContents()) === false) {
throw FilesystemException::createFromPhpError();
}

if (fclose($fp) === false) {
throw FilesystemException::createFromPhpError();
}
}

private function makeMultipartFormDataRequest(GotenbergRequestInterface $request): RequestInterface
Expand Down
13 changes: 9 additions & 4 deletions src/DocumentFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@

use GuzzleHttp\Psr7\LazyOpenStream;
use Psr\Http\Message\StreamInterface;
use Safe\Exceptions\FilesystemException;
use function fopen;
use function fwrite;
use function GuzzleHttp\Psr7\stream_for;
use function Safe\fopen;
use function Safe\fwrite;

final class DocumentFactory
{
Expand All @@ -29,7 +28,13 @@ public static function makeFromStream(string $fileName, StreamInterface $fileStr
public static function makeFromString(string $fileName, string $string): Document
{
$fileStream = fopen('php://memory', 'rb+');
fwrite($fileStream, $string);
if ($fileStream === false) {
throw FilesystemException::createFromPhpError();
}

if (fwrite($fileStream, $string) === false) {
throw FilesystemException::createFromPhpError();
}

return new Document($fileName, stream_for($fileStream));
}
Expand Down
23 changes: 23 additions & 0 deletions src/FilesystemException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace TheCodingMachine\Gotenberg;

use ErrorException;
use RuntimeException;
use function error_get_last;

class FilesystemException extends ErrorException
{
public static function createFromPhpError(): self
{
$error = error_get_last();

if ($error === null) {
throw new RuntimeException('No error information available');
}

return new self($error['message'] ?? 'An error occured', 0, $error['type'] ?? 1);
}
}
1 change: 0 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use HTTP\Client\Exception;
use PHPUnit\Framework\TestCase;
use Safe\Exceptions\FilesystemException;

final class ClientTest extends TestCase
{
Expand Down
1 change: 0 additions & 1 deletion tests/DocumentFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use GuzzleHttp\Psr7\LazyOpenStream;
use PHPUnit\Framework\TestCase;
use Safe\Exceptions\FilesystemException;

final class DocumentFactoryTest extends TestCase
{
Expand Down
39 changes: 39 additions & 0 deletions tests/FilesystemExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace TheCodingMachine\Gotenberg;

use ErrorException;
use PHPUnit\Framework\TestCase;
use RuntimeException;
use function error_clear_last;
use function fclose;
use function fopen;

class FilesystemExceptionTest extends TestCase
{
public function testExceptionCanBeCreated(): void
{
$fp = fopen('php://memory', 'r');
fclose($fp);
@fclose($fp);

$exception = FilesystemException::createFromPhpError();

$this->assertInstanceOf(ErrorException::class, $exception);
$this->assertSame('fclose(): supplied resource is not a valid stream resource', $exception->getMessage());
$this->assertSame(0, $exception->getCode());
$this->assertSame(2, $exception->getSeverity());
}

public function testExceptionThrownWhenNoError(): void
{
error_clear_last();

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('No error information available');

FilesystemException::createFromPhpError();
}
}

0 comments on commit 97cb313

Please sign in to comment.