mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
refactor response model to multi-protocol connection interface
This commit is contained in:
parent
3316a149a6
commit
f0024a0855
8 changed files with 652 additions and 350 deletions
144
src/Model/Connection/Gemini.php
Normal file
144
src/Model/Connection/Gemini.php
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yggverse\Yoda\Model\Connection;
|
||||
|
||||
use \Yggverse\Gemini\Client\Request;
|
||||
use \Yggverse\Gemini\Client\Response;
|
||||
use \Yggverse\Net\Address;
|
||||
|
||||
use \Yggverse\Yoda\Model\Connection;
|
||||
|
||||
class Gemini
|
||||
{
|
||||
private Connection $_connection;
|
||||
|
||||
public function __construct(
|
||||
Connection $connection
|
||||
) {
|
||||
$this->_connection = $connection;
|
||||
}
|
||||
|
||||
public function sync(
|
||||
Address $address,
|
||||
int $timeout = 5
|
||||
): void
|
||||
{
|
||||
$request = new Request(
|
||||
$address->get()
|
||||
);
|
||||
|
||||
$response = new Response(
|
||||
$request->getResponse(
|
||||
$timeout
|
||||
)
|
||||
);
|
||||
|
||||
// Route status code
|
||||
// https://geminiprotocol.net/docs/protocol-specification.gmi#status-codes
|
||||
switch ($response->getCode())
|
||||
{
|
||||
case 10: // response expected
|
||||
case 11: // sensitive input
|
||||
|
||||
$this->_connection->setMime(
|
||||
$this->_connection::MIME_TEXT_GEMINI
|
||||
);
|
||||
|
||||
$this->_connection->setRequest(
|
||||
[
|
||||
'placeholder' => $response->getMeta(),
|
||||
'visible' => 11 !== $response->getCode()
|
||||
]
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case 20: // ok
|
||||
|
||||
$this->_connection->setData(
|
||||
$response->getBody()
|
||||
);
|
||||
|
||||
switch (true)
|
||||
{
|
||||
case str_contains(
|
||||
$response->getMeta(),
|
||||
self::MIME_TEXT_GEMINI
|
||||
):
|
||||
|
||||
$this->_connection->setMime(
|
||||
$this->_connection::MIME_TEXT_GEMINI
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
case str_contains(
|
||||
$response->getMeta(),
|
||||
self::MIME_TEXT_PLAIN
|
||||
):
|
||||
|
||||
$this->_connection->setMime(
|
||||
$this->_connection::MIME_TEXT_PLAIN
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
throw new \Exception(
|
||||
sprintf(
|
||||
_('MIME type not implemented for %s'),
|
||||
$response->getMeta()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case 31: // redirect
|
||||
// show link, no follow
|
||||
|
||||
$this->_connection->setTitle(
|
||||
_('Redirect!')
|
||||
);
|
||||
|
||||
$this->_connection->setData(
|
||||
sprintf(
|
||||
'=> %s',
|
||||
$response->getMeta()
|
||||
)
|
||||
);
|
||||
|
||||
$this->_connection->setMime(
|
||||
$this->_connection::MIME_TEXT_GEMINI
|
||||
);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
$this->_connection->setTitle(
|
||||
_('Oops!')
|
||||
);
|
||||
|
||||
$this->_connection->setData(
|
||||
sprintf(
|
||||
'Could not open request (code: %d)',
|
||||
intval(
|
||||
$response->getCode()
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
$this->_connection->setMime(
|
||||
$this->_connection::MIME_TEXT_GEMINI
|
||||
);
|
||||
}
|
||||
|
||||
$this->_connection->setCompleted(
|
||||
true
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue