Skip to content

Commit

Permalink
Merge pull request #22503 from totten/master-pipe-nfc
Browse files Browse the repository at this point in the history
(NFC) Civi::pipe - Swap various quotes
  • Loading branch information
eileenmcnaughton authored Jan 14, 2022
2 parents 598911a + eaa0d7a commit 39d3df2
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 15 deletions.
10 changes: 5 additions & 5 deletions Civi/Pipe/BasicPipeClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function __destruct() {
*/
public function connect(string $command): array {
if ($this->process) {
throw new \RuntimeException("Client error: Already connected");
throw new \RuntimeException('Client error: Already connected');
}

$desc = [['pipe', 'r'], ['pipe', 'w'], ['pipe', 'a']];
Expand All @@ -92,7 +92,7 @@ public function connect(string $command): array {
$line = stream_get_line($this->pipes[1], $this->bufferSize, "\n");
$this->welcome = json_decode($line, TRUE);
if ($this->welcome === NULL || !isset($this->welcome['Civi::pipe'])) {
throw new \RuntimeException("Protocol error: Received malformed welcome");
throw new \RuntimeException('Protocol error: Received malformed welcome');
}
return $this->welcome['Civi::pipe'];
}
Expand All @@ -114,18 +114,18 @@ public function close(): void {
*/
public function call(string $method, array $params, $id = NULL): array {
if (!$this->process) {
throw new \RuntimeException("Client error: Connection was not been opened yet.");
throw new \RuntimeException('Client error: Connection was not been opened yet.');
}

$requestLine = json_encode(['jsonrpc' => '2.0', 'method' => $method, 'params' => $params, 'id' => $id]);
fwrite($this->pipes[0], $requestLine . "\n");
$responseLine = stream_get_line($this->pipes[1], $this->bufferSize, "\n");
$decode = json_decode($responseLine, TRUE);
if (!isset($decode['jsonrpc']) || $decode['jsonrpc'] !== '2.0') {
throw new \RuntimeException("Protocol error: Response lacks JSON-RPC header.");
throw new \RuntimeException('Protocol error: Response lacks JSON-RPC header.');
}
if (!array_key_exists('id', $decode) || $decode['id'] !== $id) {
throw new \RuntimeException("Protocol error: Received response for wrong request.");
throw new \RuntimeException('Protocol error: Received response for wrong request.');
}

if (array_key_exists('error', $decode) && !array_key_exists('result', $decode)) {
Expand Down
4 changes: 2 additions & 2 deletions Civi/Pipe/JsonRpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static function run(string $requestLine, callable $dispatcher): string {
$response = static::handleMethodCall($parsed, $dispatcher);
}
else {
// [sic] "Invalid Request" title-case is anomalous but dictated by standard.
// [sic] 'Invalid Request' title-case is anomalous but dictated by standard.
throw new \InvalidArgumentException('Invalid Request', -32600);
}

Expand All @@ -65,7 +65,7 @@ protected static function handleMethodCall($request, $dispatcher): array {
throw new \InvalidArgumentException('Parse error', -32700);
}
if (($request['jsonrpc'] ?? '') !== '2.0' || !is_string($request['method'])) {
// [sic] "Invalid Request" title-case is anomalous but dictated by standard.
// [sic] 'Invalid Request' title-case is anomalous but dictated by standard.
throw new \InvalidArgumentException('Invalid Request', -32600);
}
if (isset($request['params']) && !is_array($request['params'])) {
Expand Down
2 changes: 1 addition & 1 deletion Civi/Pipe/JsonRpcMethodException.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class JsonRpcMethodException extends \CRM_Core_Exception {
public $raw;

public function __construct(array $jsonRpcError) {
parent::__construct($jsonRpcError['error']['message'] ?? "Unknown JSON-RPC error",
parent::__construct($jsonRpcError['error']['message'] ?? 'Unknown JSON-RPC error',
$jsonRpcError['error']['code'] ?? 0,
$jsonRpcError['error']['data'] ?? []
);
Expand Down
4 changes: 2 additions & 2 deletions Civi/Pipe/LineSessionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
* $session = new class {
* use LineSessionTrait;
* protected function onRequest(string $requestLine): ?string {
* return "Thanks";
* return 'Thanks';
* }
* protected function onException(string $requestLine, \Throwable $t): ?string {
* return "Oops";
* return 'Oops';
* }
* }
* $session->setIO(STDIN, STDOUT)->run();
Expand Down
2 changes: 1 addition & 1 deletion Civi/Pipe/PipeSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected function onConnect(string $negotiationFlags): ?string {
}
}

return json_encode(["Civi::pipe" => $flags]);
return json_encode(['Civi::pipe' => $flags]);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions Civi/Pipe/PublicMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function echo(PipeSession $session, array $request) {
*/
public function login(PipeSession $session, array $request) {
if (!function_exists('authx_login')) {
throw new \CRM_Core_Exception("Cannot authenticate. Authx is not configured.");
throw new \CRM_Core_Exception('Cannot authenticate. Authx is not configured.');
}

$redact = function(?array $authx) {
Expand All @@ -116,17 +116,17 @@ public function login(PipeSession $session, array $request) {
return $redact(authx_login(['flow' => 'script', 'principal' => $principal]));
}
elseif ($principal && !$session->isTrusted()) {
throw new AuthxException("Session is not trusted.");
throw new AuthxException('Session is not trusted.');
}
elseif (isset($request['cred'])) {
$authn = new \Civi\Authx\Authenticator();
$authn->setRejectMode('exception');
if ($authn->auth(NULL, ['flow' => 'pipe', 'cred' => $request['cred']])) {
return $redact(\CRM_Core_Session::singleton()->get("authx"));
return $redact(\CRM_Core_Session::singleton()->get('authx'));
}
}

throw new AuthxException("Cannot authenticate. Must specify principal/credentials.");
throw new AuthxException('Cannot authenticate. Must specify principal/credentials.');
}

/**
Expand Down

0 comments on commit 39d3df2

Please sign in to comment.