Removed cli-related middlewares and factories

This commit is contained in:
Alejandro Celaya
2016-07-03 09:16:56 +02:00
parent 35147fecb2
commit e09915dd21
7 changed files with 0 additions and 262 deletions

View File

@@ -1,91 +0,0 @@
<?php
namespace Acelaya\UrlShortener\Middleware\CliRoutable;
use Acelaya\UrlShortener\Exception\InvalidUrlException;
use Acelaya\UrlShortener\Service\UrlShortener;
use Acelaya\UrlShortener\Service\UrlShortenerInterface;
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Uri;
use Zend\Stratigility\MiddlewareInterface;
class GenerateShortcodeMiddleware implements MiddlewareInterface
{
/**
* @var UrlShortenerInterface
*/
private $urlShortener;
/**
* @var array
*/
private $domainConfig;
/**
* GenerateShortcodeMiddleware constructor.
*
* @param UrlShortenerInterface|UrlShortener $urlShortener
* @param array $domainConfig
*
* @Inject({UrlShortener::class, "config.url_shortener.domain"})
*/
public function __construct(UrlShortenerInterface $urlShortener, array $domainConfig)
{
$this->urlShortener = $urlShortener;
$this->domainConfig = $domainConfig;
}
/**
* Process an incoming request and/or response.
*
* Accepts a server-side request and a response instance, and does
* something with them.
*
* If the response is not complete and/or further processing would not
* interfere with the work done in the middleware, or if the middleware
* wants to delegate to another process, it can use the `$out` callable
* if present.
*
* If the middleware does not return a value, execution of the current
* request is considered complete, and the response instance provided will
* be considered the response to return.
*
* Alternately, the middleware may return a response instance.
*
* Often, middleware will `return $out();`, with the assumption that a
* later middleware will return a response.
*
* @param Request $request
* @param Response $response
* @param null|callable $out
* @return null|Response
*/
public function __invoke(Request $request, Response $response, callable $out = null)
{
$longUrl = $request->getAttribute('longUrl');
try {
if (! isset($longUrl)) {
$response->getBody()->write('A URL was not provided!' . PHP_EOL);
return;
}
$shortcode = $this->urlShortener->urlToShortCode(new Uri($longUrl));
$shortUrl = (new Uri())->withPath($shortcode)
->withScheme($this->domainConfig['schema'])
->withHost($this->domainConfig['hostname']);
$response->getBody()->write(
sprintf('Processed URL "%s".%sGenerated short URL "%s"', $longUrl, PHP_EOL, $shortUrl) . PHP_EOL
);
} catch (InvalidUrlException $e) {
$response->getBody()->write(
sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl) . PHP_EOL
);
} catch (\Exception $e) {
$response->getBody()->write($e);
} finally {
return $response;
}
}
}

View File

@@ -1,32 +0,0 @@
<?php
namespace Acelaya\UrlShortener\Middleware\Factory;
use Acelaya\UrlShortener\Middleware\CliParamsMiddleware;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class CliParamsMiddlewareFactory implements FactoryInterface
{
/**
* Create an object
*
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return object
* @throws ServiceNotFoundException if unable to resolve the service.
* @throws ServiceNotCreatedException if an exception is raised when
* creating a service.
* @throws ContainerException if any other error occurs
*/
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
return new CliParamsMiddleware(
isset($_SERVER['argv']) ? $_SERVER['argv'] : [],
php_sapi_name()
);
}
}