Skip to content

Commit

Permalink
prepare release
Browse files Browse the repository at this point in the history
  • Loading branch information
rodber committed Aug 7, 2023
1 parent 0580a27 commit ca9ef15
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
- name: Install dependencies
run: composer install --no-progress
- name: Run PHPStan
run: phpstan analyse src -l 9
run: composer phpstan
- name: Tests (PHPUnit)
run: vendor/bin/phpunit --coverage-xml=build/logs/xml-coverage --log-junit=build/logs/junit.xml
# - name: Mutation testing (Infection)
Expand Down
22 changes: 11 additions & 11 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"name": "chevere/xr",
"description": "Lightweight debug utility",
"homepage": "https://xr-debug.chevere.org",
"minimum-stability": "dev",
"keywords": [
"chevere",
"dump",
Expand All @@ -18,18 +17,16 @@
}
],
"require": {
"php": "^8.1",
"ext-curl": "*",
"ext-json": "*",
"chevere/chevere": "^3.1.x-dev",
"chevere/http": "0.2.x-dev",
"chevere/chevere": "^3.1",
"chevere/http": "^0.2",
"chevere/throwable-handler": "^0.10",
"chevere/trace": "^0.8",
"chevere/var-dump": "^0.8.x-dev",
"chevere/var-dump": "^0.8",
"phpseclib/phpseclib": "~3.0"
},
"require-dev": {
"chevere/xr-server": "^0.8.x-dev",
"phpstan/phpstan": "^1.9",
"phpunit/phpunit": "^9.5",
"symplify/easy-coding-standard": "^11.1"
Expand All @@ -49,21 +46,24 @@
"scripts": {
"all": [
"composer phpstan",
"composer test-rep",
"composer test",
"composer test-coverage",
"composer infection"
],
"infection": "infection --only-covered -j10",
"phpstan": "vendor/bin/phpstan analyze src/ --memory-limit 512M --level 9",
"test": "vendor/bin/phpunit -c phpunit.xml",
"test-rep": "vendor/bin/phpunit -c phpunit-report.xml",
"update-cs": "mkdir -p .ecs && cd .ecs && curl -O https://raw.githubusercontent.com/chevere/code-style/main/.ecs/ecs-chevere.php"
"test-coverage": "vendor/bin/phpunit -c phpunit-coverage.xml",
"cs-update": "mkdir -p .ecs && cd .ecs && curl -O https://raw.githubusercontent.com/chevere/code-style/main/.ecs/ecs-chevere.php",
"cs-fix": "vendor/bin/ecs --config='.ecs/ecs.php' check src --fix"
},
"scripts-descriptions": {
"all": "Runs all checks",
"infection": "Runs infection",
"phpstan": "Runs phpstan",
"test": "Run test suite",
"test-rep": "Run test suite (report)",
"update-cs": "Update Chevere code style definition"
"test-coverage": "Run test suite (coverage)",
"cs-update": "Update Chevere code style definition",
"cs-fix": "Update Chevere code style definition"
}
}
164 changes: 161 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,168 @@

namespace Chevere\Xr;

use Chevere\Http\Interfaces\MethodInterface;
use Chevere\Http\Methods\GetMethod;
use Chevere\Http\Methods\PostMethod;
use Chevere\Xr\Exceptions\StopException;
use Chevere\Xr\Interfaces\ClientInterface;
use Chevere\Xr\Traits\ClientTrait;
use Chevere\Xr\Interfaces\CurlInterface;
use Chevere\Xr\Interfaces\MessageInterface;
use phpseclib3\Crypt\EC\PrivateKey;
use function Chevere\Message\message;

final class Client implements ClientInterface
class Client implements ClientInterface
{
use ClientTrait;
private CurlInterface $curl;

private string $scheme = 'http';

/**
* @var array <int, mixed>
*/
private array $options = [];

public function __construct(
private string $host = 'localhost',
private int $port = 27420,
private bool $isHttps = false,

Check failure on line 40 in src/Client.php

View workflow job for this annotation

GitHub Actions / PHP 8.2 test on ubuntu-latest

Property Chevere\Xr\Client::$isHttps is never read, only written.
private ?PrivateKey $privateKey = null,
) {
$this->curl = new Curl();
if ($isHttps) {
$this->scheme = 'https';
}
}

public function withCurl(CurlInterface $curl): self
{
$new = clone $this;
$new->curl = $curl;

return $new;
}

public function curl(): CurlInterface
{
return $this->curl;
}

public function getUrl(string $endpoint): string
{
return "{$this->scheme}://{$this->host}:{$this->port}/{$endpoint}";
}

public function sendMessage(MessageInterface $message): void
{
try {
$curl = $this->getCurlHandle(
new PostMethod(),
'messages',
$message->toArray()
);
$curl->exec();
} finally {
unset($curl);
}
}

public function sendPause(MessageInterface $message): void
{
try {
$curl = $this->getCurlHandle(
new PostMethod(),
'pauses',
$message->toArray(),
);
$curl->exec();
$curlError = $curl->error();
if ($curlError === '') {
do {
sleep(1);
} while ($this->isPaused($message->id()));
}
} finally {
unset($curl);
}
}

public function isPaused(string $id): bool
{
try {
$curl = $this->getCurlHandle(
new GetMethod(),
'pauses/' . $id,
[]
);
$curlResult = $curl->exec();
if (! $curlResult || $curl->error() !== '') {
return false;
}
/** @var object $response */
$response = json_decode(strval($curlResult));
if ($response->stop ?? false) {
throw new StopException(
message('[STOP EXECUTION] triggered from %remote%')
->withTranslate('%remote%', $this->host . ':' . $this->port)
);
}

return true;
} finally {
unset($curl);
}
}

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

/**
* @codeCoverageIgnore
*/
public function exit(int $exitCode = 0): void
{
exit($exitCode);
}

/**
* @param array<string, string> $data
*/
private function getCurlHandle(MethodInterface $method, string $url, array $data): CurlInterface
{
$this->options = [
CURLINFO_HEADER_OUT => true,
CURLOPT_ENCODING => '',
CURLOPT_FAILONERROR => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => $method::name(),
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_TIMEOUT => 2,
CURLOPT_URL => $this->getUrl($url),
CURLOPT_USERAGENT => 'chevere/xr 1.0',
];
$this->handleSignature($data);
$this->curl->setOptArray($this->options);

return $this->curl;
}

/**
* @param array<string, string> $data
*/
private function handleSignature(array $data): void
{
if ($this->privateKey !== null) {
$serialize = serialize($data);
/** @var string $signature */
$signature = $this->privateKey->sign($serialize);
$signatureDisplay = base64_encode($signature);
$this->options[CURLOPT_HTTPHEADER] = [
'X-Signature: ' . $signatureDisplay,
];
}
}
}
2 changes: 1 addition & 1 deletion src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
use Ramsey\Uuid\Provider\Node\RandomNodeProvider;
use Ramsey\Uuid\Uuid;

final class Message implements MessageInterface
class Message implements MessageInterface
{
private string $body = '';

Expand Down
Loading

0 comments on commit ca9ef15

Please sign in to comment.