refactor: added AbstractProxy base class for shortener proxies

This commit is contained in:
Karthik Kasturi 2025-08-15 23:28:44 +01:00
parent 714e455479
commit 4a39a2ad0f
12 changed files with 221 additions and 216 deletions

View file

@ -149,10 +149,10 @@ class Controller
$this->_jsonld($this->_request->getParam('jsonld'));
return;
case 'yourlsproxy':
$this->_yourlsproxy($this->_request->getParam('link'));
$this->_shortenerproxy($this->_request->getParam('link'), YourlsProxy::class);
break;
case 'shlinkproxy':
$this->_shlinkproxy($this->_request->getParam('link'));
$this->_shortenerproxy($this->_request->getParam('link'), ShlinkProxy::class);
break;
}
@ -454,12 +454,12 @@ class Controller
$page->assign('NAME', $this->_conf->getKey('name'));
if ($this->_request->getOperation() === 'yourlsproxy') {
$page->assign('SHORTURL', $this->_status);
$page->draw('yourlsproxy');
$page->draw('shortenerproxy');
return;
}
if ($this->_request->getOperation() === 'shlinkproxy') {
$page->assign('SHORTURL', $this->_status);
$page->draw('shlinkproxy');
$page->draw('shortenerproxy');
return;
}
$page->assign('BASEPATH', I18n::_($this->_conf->getKey('basepath')));
@ -536,34 +536,23 @@ class Controller
}
/**
* proxies link to YOURLS, updates status or error with response
* Proxies a link using the specified proxy class, and updates the status or error with the response.
*
* @access private
* @param string $link
* @param string $link The link to be proxied.
* @param string $proxyClass The fully qualified class name of the proxy to use.
*/
private function _yourlsproxy($link)
private function _shortenerproxy($link, $proxyClass)
{
$yourls = new YourlsProxy($this->_conf, $link);
if ($yourls->isError()) {
$this->_error = $yourls->getError();
} else {
$this->_status = $yourls->getUrl();
if (!is_subclass_of($proxyClass, AbstractProxy::class)) {
$this->_error = 'Invalid proxy class.';
return;
}
}
/**
* proxies link to SHLINK, updates status or error with response
*
* @access private
* @param string $link
*/
private function _shlinkproxy($link)
{
$shlink = new ShlinkProxy($this->_conf, $link);
if ($shlink->isError()) {
$this->_error = $shlink->getError();
$proxy = new $proxyClass($this->_conf, $link);
if ($proxy->isError()) {
$this->_error = $proxy->getError();
} else {
$this->_status = $shlink->getUrl();
$this->_status = $proxy->getUrl();
}
}