From 0323e0d17d30ed9bd9507ef156eaf21970b28afb Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sun, 11 Aug 2019 10:22:19 +0200 Subject: [PATCH] Simplified IpAddressMiddlewareFactory and decoupled from Core module --- module/Common/src/Image/ImageBuilder.php | 1 + .../Common/src/Image/ImageBuilderFactory.php | 1 + .../src/Image/ImageBuilderInterface.php | 1 + module/Common/src/Image/ImageFactory.php | 1 + module/Common/src/Logger/LoggerFactory.php | 21 +++-------------- .../Middleware/IpAddressMiddlewareFactory.php | 23 +++++-------------- .../IpAddressMiddlewareFactoryTest.php | 5 ++-- module/Core/src/Model/Visitor.php | 5 ++-- 8 files changed, 17 insertions(+), 41 deletions(-) diff --git a/module/Common/src/Image/ImageBuilder.php b/module/Common/src/Image/ImageBuilder.php index 2dc3b942..06940327 100644 --- a/module/Common/src/Image/ImageBuilder.php +++ b/module/Common/src/Image/ImageBuilder.php @@ -6,6 +6,7 @@ namespace Shlinkio\Shlink\Common\Image; use mikehaertl\wkhtmlto\Image; use Zend\ServiceManager\AbstractPluginManager; +/** @deprecated */ class ImageBuilder extends AbstractPluginManager implements ImageBuilderInterface { protected $instanceOf = Image::class; diff --git a/module/Common/src/Image/ImageBuilderFactory.php b/module/Common/src/Image/ImageBuilderFactory.php index a330666f..93c369bb 100644 --- a/module/Common/src/Image/ImageBuilderFactory.php +++ b/module/Common/src/Image/ImageBuilderFactory.php @@ -10,6 +10,7 @@ use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\Factory\FactoryInterface; +/** @deprecated */ class ImageBuilderFactory implements FactoryInterface { /** diff --git a/module/Common/src/Image/ImageBuilderInterface.php b/module/Common/src/Image/ImageBuilderInterface.php index ff4baeeb..c2d3c820 100644 --- a/module/Common/src/Image/ImageBuilderInterface.php +++ b/module/Common/src/Image/ImageBuilderInterface.php @@ -5,6 +5,7 @@ namespace Shlinkio\Shlink\Common\Image; use Zend\ServiceManager\ServiceLocatorInterface; +/** @deprecated */ interface ImageBuilderInterface extends ServiceLocatorInterface { } diff --git a/module/Common/src/Image/ImageFactory.php b/module/Common/src/Image/ImageFactory.php index 1305d16d..b02cde04 100644 --- a/module/Common/src/Image/ImageFactory.php +++ b/module/Common/src/Image/ImageFactory.php @@ -10,6 +10,7 @@ use Zend\ServiceManager\Exception\ServiceNotCreatedException; use Zend\ServiceManager\Exception\ServiceNotFoundException; use Zend\ServiceManager\Factory\FactoryInterface; +/** @deprecated */ class ImageFactory implements FactoryInterface { /** diff --git a/module/Common/src/Logger/LoggerFactory.php b/module/Common/src/Logger/LoggerFactory.php index 0671bbcc..7e896108 100644 --- a/module/Common/src/Logger/LoggerFactory.php +++ b/module/Common/src/Logger/LoggerFactory.php @@ -5,29 +5,14 @@ namespace Shlinkio\Shlink\Common\Logger; use Cascade\Cascade; use Interop\Container\ContainerInterface; -use Interop\Container\Exception\ContainerException; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; -use Zend\ServiceManager\Factory\FactoryInterface; +use Monolog\Logger; use function count; use function explode; -class LoggerFactory implements FactoryInterface +class LoggerFactory { - /** - * 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) + public function __invoke(ContainerInterface $container, string $requestedName, ?array $options = null): Logger { $config = $container->has('config') ? $container->get('config') : []; Cascade::fileConfig($config['logger'] ?? ['loggers' => []]); diff --git a/module/Common/src/Middleware/IpAddressMiddlewareFactory.php b/module/Common/src/Middleware/IpAddressMiddlewareFactory.php index 67063c28..73d643b4 100644 --- a/module/Common/src/Middleware/IpAddressMiddlewareFactory.php +++ b/module/Common/src/Middleware/IpAddressMiddlewareFactory.php @@ -3,28 +3,17 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Common\Middleware; -use Interop\Container\ContainerInterface; +use Psr\Container\ContainerInterface; use RKA\Middleware\IpAddress; -use Shlinkio\Shlink\Core\Model\Visitor; -use Zend\ServiceManager\Exception\ServiceNotCreatedException; -use Zend\ServiceManager\Exception\ServiceNotFoundException; -use Zend\ServiceManager\Factory\FactoryInterface; -class IpAddressMiddlewareFactory implements FactoryInterface +class IpAddressMiddlewareFactory { - /** - * Create an object - * - * @param ContainerInterface $container - * @param string $requestedName - * @param null|array $options - * @throws ServiceNotFoundException if unable to resolve the service. - * @throws ServiceNotCreatedException if an exception is raised when creating a service. - */ - public function __invoke(ContainerInterface $container, $requestedName, ?array $options = null): IpAddress + public const REQUEST_ATTR = 'remote_address'; + + public function __invoke(ContainerInterface $container): IpAddress { $config = $container->get('config'); $headersToInspect = $config['ip_address_resolution']['headers_to_inspect'] ?? []; - return new IpAddress(true, [], Visitor::REMOTE_ADDRESS_ATTR, $headersToInspect); + return new IpAddress(true, [], self::REQUEST_ATTR, $headersToInspect); } } diff --git a/module/Common/test/Middleware/IpAddressMiddlewareFactoryTest.php b/module/Common/test/Middleware/IpAddressMiddlewareFactoryTest.php index 4a3361bd..fdf81a35 100644 --- a/module/Common/test/Middleware/IpAddressMiddlewareFactoryTest.php +++ b/module/Common/test/Middleware/IpAddressMiddlewareFactoryTest.php @@ -6,7 +6,6 @@ namespace ShlinkioTest\Shlink\Common\Middleware; use PHPUnit\Framework\TestCase; use ReflectionObject; use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory; -use Shlinkio\Shlink\Core\Model\Visitor; use Zend\ServiceManager\ServiceManager; class IpAddressMiddlewareFactoryTest extends TestCase @@ -26,7 +25,7 @@ class IpAddressMiddlewareFactoryTest extends TestCase { $instance = ($this->factory)(new ServiceManager(['services' => [ 'config' => $config, - ]]), ''); + ]])); $ref = new ReflectionObject($instance); $checkProxyHeaders = $ref->getProperty('checkProxyHeaders'); @@ -40,7 +39,7 @@ class IpAddressMiddlewareFactoryTest extends TestCase $this->assertTrue($checkProxyHeaders->getValue($instance)); $this->assertEquals([], $trustedProxies->getValue($instance)); - $this->assertEquals(Visitor::REMOTE_ADDRESS_ATTR, $attributeName->getValue($instance)); + $this->assertEquals(IpAddressMiddlewareFactory::REQUEST_ATTR, $attributeName->getValue($instance)); $this->assertEquals($expectedHeadersToInspect, $headersToInspect->getValue($instance)); } diff --git a/module/Core/src/Model/Visitor.php b/module/Core/src/Model/Visitor.php index 8a8107cf..1a4506b7 100644 --- a/module/Core/src/Model/Visitor.php +++ b/module/Core/src/Model/Visitor.php @@ -4,11 +4,10 @@ declare(strict_types=1); namespace Shlinkio\Shlink\Core\Model; use Psr\Http\Message\ServerRequestInterface; +use Shlinkio\Shlink\Common\Middleware\IpAddressMiddlewareFactory; final class Visitor { - public const REMOTE_ADDRESS_ATTR = 'remote_address'; - /** @var string */ private $userAgent; /** @var string */ @@ -28,7 +27,7 @@ final class Visitor return new self( $request->getHeaderLine('User-Agent'), $request->getHeaderLine('Referer'), - $request->getAttribute(self::REMOTE_ADDRESS_ATTR) + $request->getAttribute(IpAddressMiddlewareFactory::REQUEST_ATTR) ); }