mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-20 01:09:56 +03:00
Merge branch 'feature/1.7.0' of github.com:acelaya/shlink into feature/1.7.0
This commit is contained in:
commit
6f9b727673
20 changed files with 63 additions and 31 deletions
1
.gitattributes
vendored
1
.gitattributes
vendored
|
@ -22,3 +22,4 @@ indocker export-ignore
|
||||||
phpcs.xml export-ignore
|
phpcs.xml export-ignore
|
||||||
phpunit.xml.dist export-ignore
|
phpunit.xml.dist export-ignore
|
||||||
phpunit-func.xml export-ignore
|
phpunit-func.xml export-ignore
|
||||||
|
phpstan.neon
|
||||||
|
|
|
@ -21,7 +21,7 @@ before_script:
|
||||||
script:
|
script:
|
||||||
- mkdir build
|
- mkdir build
|
||||||
- composer check
|
- composer check
|
||||||
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then ~/.composer/vendor/bin/phpstan analyse module/*/src/ --level=5 -c phpstan.neon; fi
|
- if [[ $TRAVIS_PHP_VERSION = 7.1 ]] || [[ $TRAVIS_PHP_VERSION = 7.2 ]]; then ~/.composer/vendor/bin/phpstan analyse module/*/src/ --level=6 -c phpstan.neon; fi
|
||||||
|
|
||||||
after_script:
|
after_script:
|
||||||
- vendor/bin/phpcov merge build --clover build/clover.xml
|
- vendor/bin/phpcov merge build --clover build/clover.xml
|
||||||
|
|
|
@ -3,6 +3,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Common\Exception;
|
namespace Shlinkio\Shlink\Common\Exception;
|
||||||
|
|
||||||
interface ExceptionInterface
|
interface ExceptionInterface extends \Throwable
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,9 +3,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\Exception;
|
namespace Shlinkio\Shlink\Core\Exception;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Common\Exception\ExceptionInterface;
|
class EntityDoesNotExistException extends RuntimeException
|
||||||
|
|
||||||
class EntityDoesNotExistException extends \RuntimeException implements ExceptionInterface
|
|
||||||
{
|
{
|
||||||
public static function createFromEntityAndConditions($entityName, array $conditions)
|
public static function createFromEntityAndConditions($entityName, array $conditions)
|
||||||
{
|
{
|
||||||
|
|
8
module/Core/src/Exception/ExceptionInterface.php
Normal file
8
module/Core/src/Exception/ExceptionInterface.php
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Exception;
|
||||||
|
|
||||||
|
interface ExceptionInterface extends \Throwable
|
||||||
|
{
|
||||||
|
}
|
8
module/Core/src/Exception/InvalidArgumentException.php
Normal file
8
module/Core/src/Exception/InvalidArgumentException.php
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Exception;
|
||||||
|
|
||||||
|
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
|
||||||
|
{
|
||||||
|
}
|
|
@ -3,8 +3,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\Exception;
|
namespace Shlinkio\Shlink\Core\Exception;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
|
||||||
|
|
||||||
class InvalidShortCodeException extends RuntimeException
|
class InvalidShortCodeException extends RuntimeException
|
||||||
{
|
{
|
||||||
public static function fromCharset($shortCode, $charSet, \Exception $previous = null)
|
public static function fromCharset($shortCode, $charSet, \Exception $previous = null)
|
||||||
|
|
|
@ -3,8 +3,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\Exception;
|
namespace Shlinkio\Shlink\Core\Exception;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
|
||||||
|
|
||||||
class InvalidUrlException extends RuntimeException
|
class InvalidUrlException extends RuntimeException
|
||||||
{
|
{
|
||||||
public static function fromUrl($url, \Throwable $previous = null)
|
public static function fromUrl($url, \Throwable $previous = null)
|
||||||
|
|
|
@ -3,8 +3,6 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Core\Exception;
|
namespace Shlinkio\Shlink\Core\Exception;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
|
||||||
|
|
||||||
class NonUniqueSlugException extends InvalidArgumentException
|
class NonUniqueSlugException extends InvalidArgumentException
|
||||||
{
|
{
|
||||||
public static function fromSlug(string $slug): self
|
public static function fromSlug(string $slug): self
|
||||||
|
|
8
module/Core/src/Exception/RuntimeException.php
Normal file
8
module/Core/src/Exception/RuntimeException.php
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Core\Exception;
|
||||||
|
|
||||||
|
class RuntimeException extends \RuntimeException implements ExceptionInterface
|
||||||
|
{
|
||||||
|
}
|
|
@ -11,12 +11,12 @@ use Doctrine\ORM\ORMException;
|
||||||
use GuzzleHttp\ClientInterface;
|
use GuzzleHttp\ClientInterface;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Psr\Http\Message\UriInterface;
|
use Psr\Http\Message\UriInterface;
|
||||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
||||||
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
use Shlinkio\Shlink\Core\Repository\ShortUrlRepository;
|
||||||
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
use Shlinkio\Shlink\Core\Util\TagManagerTrait;
|
||||||
|
|
||||||
|
|
|
@ -4,11 +4,11 @@ declare(strict_types=1);
|
||||||
namespace Shlinkio\Shlink\Core\Service;
|
namespace Shlinkio\Shlink\Core\Service;
|
||||||
|
|
||||||
use Psr\Http\Message\UriInterface;
|
use Psr\Http\Message\UriInterface;
|
||||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
|
||||||
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
use Shlinkio\Shlink\Core\Exception\EntityDoesNotExistException;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
use Shlinkio\Shlink\Core\Exception\InvalidUrlException;
|
||||||
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
use Shlinkio\Shlink\Core\Exception\NonUniqueSlugException;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\RuntimeException;
|
||||||
|
|
||||||
interface UrlShortenerInterface
|
interface UrlShortenerInterface
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,10 +5,10 @@ namespace Shlinkio\Shlink\Core\Service;
|
||||||
|
|
||||||
use Doctrine\ORM;
|
use Doctrine\ORM;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
|
||||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||||
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
||||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
|
||||||
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
use Shlinkio\Shlink\Core\Repository\VisitRepository;
|
||||||
|
|
||||||
class VisitsTracker implements VisitsTrackerInterface
|
class VisitsTracker implements VisitsTrackerInterface
|
||||||
|
|
|
@ -4,9 +4,9 @@ declare(strict_types=1);
|
||||||
namespace Shlinkio\Shlink\Core\Service;
|
namespace Shlinkio\Shlink\Core\Service;
|
||||||
|
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
|
|
||||||
use Shlinkio\Shlink\Common\Util\DateRange;
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
||||||
use Shlinkio\Shlink\Core\Entity\Visit;
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
||||||
|
use Shlinkio\Shlink\Core\Exception\InvalidArgumentException;
|
||||||
|
|
||||||
interface VisitsTrackerInterface
|
interface VisitsTrackerInterface
|
||||||
{
|
{
|
||||||
|
|
|
@ -99,7 +99,7 @@ class UrlShortenerTest extends TestCase
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @expectedException \Shlinkio\Shlink\Common\Exception\RuntimeException
|
* @expectedException \Shlinkio\Shlink\Core\Exception\RuntimeException
|
||||||
*/
|
*/
|
||||||
public function exceptionIsThrownWhenOrmThrowsException()
|
public function exceptionIsThrownWhenOrmThrowsException()
|
||||||
{
|
{
|
||||||
|
|
|
@ -3,9 +3,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest\Exception;
|
namespace Shlinkio\Shlink\Rest\Exception;
|
||||||
|
|
||||||
use Shlinkio\Shlink\Common\Exception\ExceptionInterface;
|
class AuthenticationException extends RuntimeException
|
||||||
|
|
||||||
class AuthenticationException extends \RuntimeException implements ExceptionInterface
|
|
||||||
{
|
{
|
||||||
public static function fromCredentials($username, $password)
|
public static function fromCredentials($username, $password)
|
||||||
{
|
{
|
||||||
|
|
8
module/Rest/src/Exception/ExceptionInterface.php
Normal file
8
module/Rest/src/Exception/ExceptionInterface.php
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Rest\Exception;
|
||||||
|
|
||||||
|
interface ExceptionInterface extends \Throwable
|
||||||
|
{
|
||||||
|
}
|
8
module/Rest/src/Exception/RuntimeException.php
Normal file
8
module/Rest/src/Exception/RuntimeException.php
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Shlinkio\Shlink\Rest\Exception;
|
||||||
|
|
||||||
|
class RuntimeException extends \RuntimeException implements ExceptionInterface
|
||||||
|
{
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||||
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Shlinkio\Shlink\Common\Exception\RuntimeException;
|
use Shlinkio\Shlink\Rest\Exception\RuntimeException;
|
||||||
|
|
||||||
class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface
|
class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface
|
||||||
{
|
{
|
||||||
|
@ -27,7 +27,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
|
||||||
$currentParams = $request->getParsedBody();
|
$currentParams = $request->getParsedBody();
|
||||||
|
|
||||||
// In requests that do not allow body or if the body has already been parsed, continue to next middleware
|
// In requests that do not allow body or if the body has already been parsed, continue to next middleware
|
||||||
if (! empty($currentParams) || in_array($method, [
|
if (! empty($currentParams) || \in_array($method, [
|
||||||
self::METHOD_GET,
|
self::METHOD_GET,
|
||||||
self::METHOD_HEAD,
|
self::METHOD_HEAD,
|
||||||
self::METHOD_OPTIONS,
|
self::METHOD_OPTIONS,
|
||||||
|
@ -37,7 +37,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
|
||||||
|
|
||||||
// If the accepted content is JSON, try to parse the body from JSON
|
// If the accepted content is JSON, try to parse the body from JSON
|
||||||
$contentType = $this->getRequestContentType($request);
|
$contentType = $this->getRequestContentType($request);
|
||||||
if (in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) {
|
if (\in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) {
|
||||||
return $delegate->process($this->parseFromJson($request));
|
return $delegate->process($this->parseFromJson($request));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,27 +48,28 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function getRequestContentType(Request $request)
|
private function getRequestContentType(Request $request): string
|
||||||
{
|
{
|
||||||
$contentType = $request->getHeaderLine('Content-type');
|
$contentType = $request->getHeaderLine('Content-type');
|
||||||
$contentTypes = explode(';', $contentType);
|
$contentTypes = \explode(';', $contentType);
|
||||||
return trim(array_shift($contentTypes));
|
return \trim(\array_shift($contentTypes));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Request
|
* @return Request
|
||||||
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
protected function parseFromJson(Request $request)
|
private function parseFromJson(Request $request): Request
|
||||||
{
|
{
|
||||||
$rawBody = (string) $request->getBody();
|
$rawBody = (string) $request->getBody();
|
||||||
if (empty($rawBody)) {
|
if (empty($rawBody)) {
|
||||||
return $request;
|
return $request;
|
||||||
}
|
}
|
||||||
|
|
||||||
$parsedJson = json_decode($rawBody, true);
|
$parsedJson = \json_decode($rawBody, true);
|
||||||
if (json_last_error() !== JSON_ERROR_NONE) {
|
if (\json_last_error() !== JSON_ERROR_NONE) {
|
||||||
throw new RuntimeException(sprintf('Error when parsing JSON request body: %s', json_last_error_msg()));
|
throw new RuntimeException(\sprintf('Error when parsing JSON request body: %s', \json_last_error_msg()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $request->withParsedBody($parsedJson);
|
return $request->withParsedBody($parsedJson);
|
||||||
|
@ -78,7 +79,7 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @return Request
|
* @return Request
|
||||||
*/
|
*/
|
||||||
protected function parseFromUrlEncoded(Request $request)
|
private function parseFromUrlEncoded(Request $request): Request
|
||||||
{
|
{
|
||||||
$rawBody = (string) $request->getBody();
|
$rawBody = (string) $request->getBody();
|
||||||
if (empty($rawBody)) {
|
if (empty($rawBody)) {
|
||||||
|
|
|
@ -20,7 +20,7 @@ class RestUtils
|
||||||
const NOT_FOUND_ERROR = 'NOT_FOUND';
|
const NOT_FOUND_ERROR = 'NOT_FOUND';
|
||||||
const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
|
const UNKNOWN_ERROR = 'UNKNOWN_ERROR';
|
||||||
|
|
||||||
public static function getRestErrorCodeFromException(Common\ExceptionInterface $e)
|
public static function getRestErrorCodeFromException(\Throwable $e)
|
||||||
{
|
{
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case $e instanceof Core\InvalidShortCodeException:
|
case $e instanceof Core\InvalidShortCodeException:
|
||||||
|
|
Loading…
Add table
Reference in a new issue