mirror of
https://github.com/shlinkio/shlink.git
synced 2026-02-28 20:23:12 +08:00
Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
361d987f47 | ||
|
|
6017db260a | ||
|
|
f9c9b3d981 | ||
|
|
e7b876f4e6 | ||
|
|
554b948775 | ||
|
|
9bdbb59401 | ||
|
|
377861c5f1 | ||
|
|
26c2aaf567 | ||
|
|
62b54ceaaf | ||
|
|
625eba76c7 | ||
|
|
e12bda3f42 | ||
|
|
0f0301ae5c | ||
|
|
8d1776af98 | ||
|
|
c597738915 | ||
|
|
639329dbe4 | ||
|
|
92b0525b6e | ||
|
|
06306aabd5 | ||
|
|
225905fcdb | ||
|
|
8ca2b3c641 | ||
|
|
ac1737492b |
2
.github/workflows/ci-db-tests.yml
vendored
2
.github/workflows/ci-db-tests.yml
vendored
@@ -36,7 +36,7 @@ jobs:
|
||||
- name: Run tests
|
||||
run: composer test:db:${{ inputs.platform }}
|
||||
- name: Upload code coverage
|
||||
uses: actions/upload-artifact@v3
|
||||
uses: actions/upload-artifact@v4
|
||||
if: ${{ matrix.php-version == '8.2' && inputs.platform == 'sqlite:ci' }}
|
||||
with:
|
||||
name: coverage-db
|
||||
|
||||
2
.github/workflows/ci-mutation-tests.yml
vendored
2
.github/workflows/ci-mutation-tests.yml
vendored
@@ -22,7 +22,7 @@ jobs:
|
||||
php-version: ${{ matrix.php-version }}
|
||||
php-extensions: openswoole-22.1.0
|
||||
extensions-cache-key: mutation-tests-extensions-${{ matrix.php-version }}-${{ inputs.test-group }}
|
||||
- uses: actions/download-artifact@v3
|
||||
- uses: actions/download-artifact@v4
|
||||
with:
|
||||
name: coverage-${{ inputs.test-group }}
|
||||
path: build
|
||||
|
||||
2
.github/workflows/ci-tests.yml
vendored
2
.github/workflows/ci-tests.yml
vendored
@@ -29,7 +29,7 @@ jobs:
|
||||
php-extensions: openswoole-22.1.0
|
||||
extensions-cache-key: tests-extensions-${{ matrix.php-version }}-${{ inputs.test-group }}
|
||||
- run: composer test:${{ inputs.test-group }}:ci
|
||||
- uses: actions/upload-artifact@v3
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: ${{ matrix.php-version == '8.2' }}
|
||||
with:
|
||||
name: coverage-${{ inputs.test-group }}
|
||||
|
||||
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -146,7 +146,7 @@ jobs:
|
||||
php-version: ${{ matrix.php-version }}
|
||||
coverage: pcov
|
||||
ini-values: pcov.directory=module
|
||||
- uses: actions/download-artifact@v3
|
||||
- uses: actions/download-artifact@v4
|
||||
with:
|
||||
path: build
|
||||
- run: mv build/coverage-unit/coverage-unit.cov build/coverage-unit.cov
|
||||
|
||||
4
.github/workflows/publish-release.yml
vendored
4
.github/workflows/publish-release.yml
vendored
@@ -24,7 +24,7 @@ jobs:
|
||||
run: ./build.sh ${GITHUB_REF#refs/tags/v}
|
||||
- if: ${{ matrix.swoole == 'no' }}
|
||||
run: ./build.sh ${GITHUB_REF#refs/tags/v} --no-swoole
|
||||
- uses: actions/upload-artifact@v3
|
||||
- uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: dist-files-${{ matrix.php-version }}-${{ matrix.swoole }}
|
||||
path: build
|
||||
@@ -34,7 +34,7 @@ jobs:
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/download-artifact@v3
|
||||
- uses: actions/download-artifact@v4
|
||||
with:
|
||||
path: build
|
||||
- name: Publish release with assets
|
||||
|
||||
37
CHANGELOG.md
37
CHANGELOG.md
@@ -4,12 +4,48 @@ All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).
|
||||
|
||||
## [3.7.3] - 2024-01-04
|
||||
### Added
|
||||
* *Nothing*
|
||||
|
||||
### Changed
|
||||
* [#1968](https://github.com/shlinkio/shlink/issues/1968) Move migrations from `data` to `module/Core`.
|
||||
* *Nothing*
|
||||
|
||||
### Deprecated
|
||||
* *Nothing*
|
||||
|
||||
### Removed
|
||||
* *Nothing*
|
||||
|
||||
### Fixed
|
||||
* [#1967](https://github.com/shlinkio/shlink/issues/1967) Allow an empty dir to be mounted in `data` when using the docker image.
|
||||
|
||||
|
||||
## [3.7.2] - 2023-12-26
|
||||
### Added
|
||||
* *Nothing*
|
||||
|
||||
### Changed
|
||||
* *Nothing*
|
||||
|
||||
### Deprecated
|
||||
* *Nothing*
|
||||
|
||||
### Removed
|
||||
* *Nothing*
|
||||
|
||||
### Fixed
|
||||
* [#1960](https://github.com/shlinkio/shlink/issues/1960) Allow QR codes to be optionally resolved even when corresponding short URL is not enabled.
|
||||
|
||||
|
||||
## [3.7.1] - 2023-12-17
|
||||
### Added
|
||||
* *Nothing*
|
||||
|
||||
### Changed
|
||||
* Remove dependency on functional-php library
|
||||
* [#1939](https://github.com/shlinkio/shlink/issues/1939) Fine-tune RoadRunner logs to avoid too many useless info.
|
||||
|
||||
### Deprecated
|
||||
* *Nothing*
|
||||
@@ -19,7 +55,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
|
||||
|
||||
### Fixed
|
||||
* [#1947](https://github.com/shlinkio/shlink/issues/1947) Fix error when importing short URLs while using Postgres.
|
||||
* [#1939](https://github.com/shlinkio/shlink/issues/1939) Fine-tune RoadRunner logs to avoid too many useless info.
|
||||
|
||||
|
||||
## [3.7.0] - 2023-11-25
|
||||
|
||||
@@ -46,17 +46,18 @@ This is a simplified version of the project structure:
|
||||
```
|
||||
shlink
|
||||
├── bin
|
||||
│ └── cli
|
||||
│ ├── cli
|
||||
│ └── [...]
|
||||
├── config
|
||||
│ ├── autoload
|
||||
│ ├── params
|
||||
│ ├── config.php
|
||||
│ └── container.php
|
||||
│ ├── container.php
|
||||
│ └── [...]
|
||||
├── data
|
||||
│ ├── cache
|
||||
│ ├── locks
|
||||
│ ├── log
|
||||
│ ├── migrations
|
||||
│ └── proxies
|
||||
├── docs
|
||||
│ ├── adr
|
||||
@@ -67,6 +68,7 @@ shlink
|
||||
│ ├── Core
|
||||
│ └── Rest
|
||||
├── public
|
||||
│ └── [...]
|
||||
├── composer.json
|
||||
└── README.md
|
||||
```
|
||||
@@ -75,7 +77,7 @@ The purposes of every folder are:
|
||||
|
||||
* `bin`: It contains the CLI tools. The `cli` one is the main entry point to run Shlink from the command line.
|
||||
* `config`: Contains application-wide configurations, which are later merged with the ones provided by every module.
|
||||
* `data`: Common runtime-generated git-ignored assets, like logs, caches, etc.
|
||||
* `data`: Common git-ignored assets, like logs, caches, lock files, GeoLite DB files, etc. It's the only location where Shlink may need to write at runtime.
|
||||
* `docs`: Any project documentation is stored here, like API spec definitions or architectural decision records.
|
||||
* `module`: Contains a sub-folder for every module in the project. Modules contain the source code, tests and configurations for every context in the project.
|
||||
* `public`: Few assets (like `favicon.ico` or `robots.txt`) and the web entry point are stored here. This web entry point is not used when serving the app with RoadRunner or openswoole.
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
[](https://packagist.org/packages/shlinkio/shlink)
|
||||
[](https://hub.docker.com/r/shlinkio/shlink/)
|
||||
[](https://github.com/shlinkio/shlink/blob/main/LICENSE)
|
||||
[](https://twitter.com/shlinkio)
|
||||
[](https://twitter.com/shlinkio)
|
||||
[](https://fosstodon.org/@shlinkio)
|
||||
[](https://slnk.to/donate)
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@
|
||||
"shlinkio/shlink-config": "^2.5",
|
||||
"shlinkio/shlink-event-dispatcher": "^3.1",
|
||||
"shlinkio/shlink-importer": "^5.2.1",
|
||||
"shlinkio/shlink-installer": "^8.6.1",
|
||||
"shlinkio/shlink-installer": "^8.7",
|
||||
"shlinkio/shlink-ip-geolocation": "^3.4",
|
||||
"shlinkio/shlink-json": "^1.1",
|
||||
"spiral/roadrunner": "^2023.2",
|
||||
@@ -75,7 +75,7 @@
|
||||
"phpunit/phpunit": "^10.4",
|
||||
"roave/security-advisories": "dev-master",
|
||||
"shlinkio/php-coding-standard": "~2.3.0",
|
||||
"shlinkio/shlink-test-utils": "^3.8",
|
||||
"shlinkio/shlink-test-utils": "^3.8.1",
|
||||
"symfony/var-dumper": "^6.3",
|
||||
"veewee/composer-run-parallel": "^1.3"
|
||||
},
|
||||
@@ -116,7 +116,7 @@
|
||||
],
|
||||
"cs": "phpcs -s",
|
||||
"cs:fix": "phpcbf",
|
||||
"stan": "APP_ENV=test php vendor/bin/phpstan analyse module/*/src module/*/test* module/*/config config docker/config data/migrations --level=8",
|
||||
"stan": "APP_ENV=test php vendor/bin/phpstan analyse module/*/src module/*/test* module/*/config module/*/migrations config docker/config --level=8",
|
||||
"test": [
|
||||
"@parallel test:unit test:db",
|
||||
"@parallel test:api test:cli"
|
||||
|
||||
@@ -62,6 +62,7 @@ return [
|
||||
Option\QrCode\DefaultFormatConfigOption::class,
|
||||
Option\QrCode\DefaultErrorCorrectionConfigOption::class,
|
||||
Option\QrCode\DefaultRoundBlockSizeConfigOption::class,
|
||||
Option\QrCode\EnabledForDisabledShortUrlsConfigOption::class,
|
||||
Option\RabbitMq\RabbitMqEnabledConfigOption::class,
|
||||
Option\RabbitMq\RabbitMqHostConfigOption::class,
|
||||
Option\RabbitMq\RabbitMqUseSslConfigOption::class,
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ENABLED_FOR_DISABLED_SHORT_URLS;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ERROR_CORRECTION;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_FORMAT;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_MARGIN;
|
||||
@@ -22,6 +23,9 @@ return [
|
||||
'round_block_size' => (bool) EnvVars::DEFAULT_QR_CODE_ROUND_BLOCK_SIZE->loadFromEnv(
|
||||
DEFAULT_QR_CODE_ROUND_BLOCK_SIZE,
|
||||
),
|
||||
'enabled_for_disabled_short_urls' => (bool) EnvVars::QR_CODE_FOR_DISABLED_SHORT_URLS->loadFromEnv(
|
||||
DEFAULT_QR_CODE_ENABLED_FOR_DISABLED_SHORT_URLS,
|
||||
),
|
||||
],
|
||||
|
||||
];
|
||||
|
||||
@@ -11,7 +11,9 @@ return [
|
||||
'base_path' => EnvVars::BASE_PATH->loadFromEnv(''),
|
||||
|
||||
'fastroute' => [
|
||||
FastRouteRouter::CONFIG_CACHE_ENABLED => true,
|
||||
// Disabling config cache for cli, ensures it's never used for openswoole/RoadRunner, and also that console
|
||||
// commands don't generate a cache file that's then used by php-fpm web executions
|
||||
FastRouteRouter::CONFIG_CACHE_ENABLED => PHP_SAPI !== 'cli',
|
||||
FastRouteRouter::CONFIG_CACHE_FILE => 'data/cache/fastroute_cached_routes.php',
|
||||
],
|
||||
],
|
||||
|
||||
@@ -11,7 +11,7 @@ use Doctrine\Migrations\DependencyFactory;
|
||||
return (static function () {
|
||||
$migrationsConfig = [
|
||||
'migrations_paths' => [
|
||||
'ShlinkMigrations' => 'data/migrations',
|
||||
'ShlinkMigrations' => 'module/Core/migrations',
|
||||
],
|
||||
'table_storage' => [
|
||||
'table_name' => 'migrations',
|
||||
|
||||
@@ -19,4 +19,6 @@ const DEFAULT_QR_CODE_MARGIN = 0;
|
||||
const DEFAULT_QR_CODE_FORMAT = 'png';
|
||||
const DEFAULT_QR_CODE_ERROR_CORRECTION = 'l';
|
||||
const DEFAULT_QR_CODE_ROUND_BLOCK_SIZE = true;
|
||||
// Deprecated. Shlink 4.0.0 should change default value to `true`
|
||||
const DEFAULT_QR_CODE_ENABLED_FOR_DISABLED_SHORT_URLS = false;
|
||||
const MIN_TASK_WORKERS = 4;
|
||||
|
||||
@@ -35,7 +35,7 @@ logs:
|
||||
http:
|
||||
mode: 'off' # Disable logging as Shlink handles it internally
|
||||
server:
|
||||
level: debug
|
||||
level: info
|
||||
metrics:
|
||||
level: debug
|
||||
jobs:
|
||||
|
||||
@@ -29,10 +29,10 @@ register_shutdown_function(function () use ($httpClient): void {
|
||||
});
|
||||
|
||||
$testHelper->createTestDb(
|
||||
['bin/cli', 'db:create'],
|
||||
['bin/cli', 'db:migrate'],
|
||||
['bin/doctrine', 'orm:schema-tool:drop'],
|
||||
['bin/doctrine', 'dbal:run-sql'],
|
||||
createDbCommand: ['bin/cli', 'db:create'],
|
||||
migrateDbCommand: ['bin/cli', 'db:migrate'],
|
||||
dropSchemaCommand: ['bin/doctrine', 'orm:schema-tool:drop'],
|
||||
runSqlCommand: ['bin/doctrine', 'dbal:run-sql'],
|
||||
);
|
||||
ApiTest\ApiTestCase::setApiClient($httpClient);
|
||||
ApiTest\ApiTestCase::setSeedFixturesCallback(fn () => $testHelper->seedFixtures($em, $config['data_fixtures'] ?? []));
|
||||
|
||||
@@ -9,9 +9,9 @@ use Psr\Container\ContainerInterface;
|
||||
/** @var ContainerInterface $container */
|
||||
$container = require __DIR__ . '/../container.php';
|
||||
$container->get(Helper\TestHelper::class)->createTestDb(
|
||||
['bin/cli', 'db:create'],
|
||||
['bin/cli', 'db:migrate'],
|
||||
['bin/doctrine', 'orm:schema-tool:drop'],
|
||||
['bin/doctrine', 'dbal:run-sql'],
|
||||
createDbCommand: ['bin/cli', 'db:create'],
|
||||
migrateDbCommand: ['bin/cli', 'db:migrate'],
|
||||
dropSchemaCommand: ['bin/doctrine', 'orm:schema-tool:drop'],
|
||||
runSqlCommand: ['bin/doctrine', 'dbal:run-sql'],
|
||||
);
|
||||
DbTest\DatabaseTestCase::setEntityManager($container->get('em'));
|
||||
|
||||
@@ -3,6 +3,9 @@ set -e
|
||||
|
||||
cd /etc/shlink
|
||||
|
||||
# Create data directories if they do not exist. This allows data dir to be mounted as an empty dir if needed
|
||||
mkdir -p data/cache data/locks data/log data/proxies
|
||||
|
||||
flags="--no-interaction --clear-db-cache"
|
||||
|
||||
# Skip downloading GeoLite2 db file if the license key env var was not defined or skipping was explicitly set
|
||||
|
||||
@@ -18,13 +18,13 @@ use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifierInterface;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlResolverInterface;
|
||||
|
||||
class QrCodeAction implements MiddlewareInterface
|
||||
readonly class QrCodeAction implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(
|
||||
private ShortUrlResolverInterface $urlResolver,
|
||||
private ShortUrlStringifierInterface $stringifier,
|
||||
private LoggerInterface $logger,
|
||||
private QrCodeOptions $defaultOptions,
|
||||
private QrCodeOptions $options,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -33,13 +33,15 @@ class QrCodeAction implements MiddlewareInterface
|
||||
$identifier = ShortUrlIdentifier::fromRedirectRequest($request);
|
||||
|
||||
try {
|
||||
$shortUrl = $this->urlResolver->resolveEnabledShortUrl($identifier);
|
||||
$shortUrl = $this->options->enabledForDisabledShortUrls
|
||||
? $this->urlResolver->resolvePublicShortUrl($identifier)
|
||||
: $this->urlResolver->resolveEnabledShortUrl($identifier);
|
||||
} catch (ShortUrlNotFoundException $e) {
|
||||
$this->logger->warning('An error occurred while creating QR code. {e}', ['e' => $e]);
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
$params = QrCodeParams::fromRequest($request, $this->defaultOptions);
|
||||
$params = QrCodeParams::fromRequest($request, $this->options);
|
||||
$qrCodeBuilder = Builder::create()
|
||||
->data($this->stringifier->stringify($shortUrl))
|
||||
->size($params->size)
|
||||
|
||||
@@ -43,6 +43,7 @@ enum EnvVars: string
|
||||
case DEFAULT_QR_CODE_FORMAT = 'DEFAULT_QR_CODE_FORMAT';
|
||||
case DEFAULT_QR_CODE_ERROR_CORRECTION = 'DEFAULT_QR_CODE_ERROR_CORRECTION';
|
||||
case DEFAULT_QR_CODE_ROUND_BLOCK_SIZE = 'DEFAULT_QR_CODE_ROUND_BLOCK_SIZE';
|
||||
case QR_CODE_FOR_DISABLED_SHORT_URLS = 'QR_CODE_FOR_DISABLED_SHORT_URLS';
|
||||
case DEFAULT_INVALID_SHORT_URL_REDIRECT = 'DEFAULT_INVALID_SHORT_URL_REDIRECT';
|
||||
case DEFAULT_REGULAR_404_REDIRECT = 'DEFAULT_REGULAR_404_REDIRECT';
|
||||
case DEFAULT_BASE_URL_REDIRECT = 'DEFAULT_BASE_URL_REDIRECT';
|
||||
|
||||
@@ -4,20 +4,22 @@ declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Options;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ENABLED_FOR_DISABLED_SHORT_URLS;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ERROR_CORRECTION;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_FORMAT;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_MARGIN;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ROUND_BLOCK_SIZE;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_SIZE;
|
||||
|
||||
final class QrCodeOptions
|
||||
readonly final class QrCodeOptions
|
||||
{
|
||||
public function __construct(
|
||||
public readonly int $size = DEFAULT_QR_CODE_SIZE,
|
||||
public readonly int $margin = DEFAULT_QR_CODE_MARGIN,
|
||||
public readonly string $format = DEFAULT_QR_CODE_FORMAT,
|
||||
public readonly string $errorCorrection = DEFAULT_QR_CODE_ERROR_CORRECTION,
|
||||
public readonly bool $roundBlockSize = DEFAULT_QR_CODE_ROUND_BLOCK_SIZE,
|
||||
public int $size = DEFAULT_QR_CODE_SIZE,
|
||||
public int $margin = DEFAULT_QR_CODE_MARGIN,
|
||||
public string $format = DEFAULT_QR_CODE_FORMAT,
|
||||
public string $errorCorrection = DEFAULT_QR_CODE_ERROR_CORRECTION,
|
||||
public bool $roundBlockSize = DEFAULT_QR_CODE_ROUND_BLOCK_SIZE,
|
||||
public bool $enabledForDisabledShortUrls = DEFAULT_QR_CODE_ENABLED_FOR_DISABLED_SHORT_URLS,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,9 +10,9 @@ use Symfony\Component\Console\Input\InputInterface;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class ShortUrlIdentifier
|
||||
final readonly class ShortUrlIdentifier
|
||||
{
|
||||
private function __construct(public readonly string $shortCode, public readonly ?string $domain = null)
|
||||
private function __construct(public string $shortCode, public ?string $domain = null)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -12,11 +12,11 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Repository\ShortUrlRepository;
|
||||
use Shlinkio\Shlink\Rest\Entity\ApiKey;
|
||||
|
||||
class ShortUrlResolver implements ShortUrlResolverInterface
|
||||
readonly class ShortUrlResolver implements ShortUrlResolverInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly UrlShortenerOptions $urlShortenerOptions,
|
||||
private EntityManagerInterface $em,
|
||||
private UrlShortenerOptions $urlShortenerOptions,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -39,11 +39,21 @@ class ShortUrlResolver implements ShortUrlResolverInterface
|
||||
* @throws ShortUrlNotFoundException
|
||||
*/
|
||||
public function resolveEnabledShortUrl(ShortUrlIdentifier $identifier): ShortUrl
|
||||
{
|
||||
$shortUrl = $this->resolvePublicShortUrl($identifier);
|
||||
if (! $shortUrl->isEnabled()) {
|
||||
throw ShortUrlNotFoundException::fromNotFound($identifier);
|
||||
}
|
||||
|
||||
return $shortUrl;
|
||||
}
|
||||
|
||||
public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl
|
||||
{
|
||||
/** @var ShortUrlRepository $shortUrlRepo */
|
||||
$shortUrlRepo = $this->em->getRepository(ShortUrl::class);
|
||||
$shortUrl = $shortUrlRepo->findOneWithDomainFallback($identifier, $this->urlShortenerOptions->mode);
|
||||
if (! $shortUrl?->isEnabled()) {
|
||||
if ($shortUrl === null) {
|
||||
throw ShortUrlNotFoundException::fromNotFound($identifier);
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,19 @@ interface ShortUrlResolverInterface
|
||||
public function resolveShortUrl(ShortUrlIdentifier $identifier, ?ApiKey $apiKey = null): ShortUrl;
|
||||
|
||||
/**
|
||||
* Resolves a public short URL matching provided identifier.
|
||||
* When trying to match public short URLs, if provided domain is default one, it gets ignored.
|
||||
* If provided domain is not default, but the short code is found in default domain, we fall back to that short URL.
|
||||
*
|
||||
* @throws ShortUrlNotFoundException
|
||||
*/
|
||||
public function resolvePublicShortUrl(ShortUrlIdentifier $identifier): ShortUrl;
|
||||
|
||||
/**
|
||||
* Resolves a public short URL matching provided identifier, only if it's not disabled.
|
||||
* Disabled short URLs are those which received the max amount of visits, have a `validSince` in the future or have
|
||||
* a `validUntil` in the past.
|
||||
*
|
||||
* @throws ShortUrlNotFoundException
|
||||
*/
|
||||
public function resolveEnabledShortUrl(ShortUrlIdentifier $identifier): ShortUrl;
|
||||
|
||||
27
module/Core/test-api/Action/QrCodeTest.php
Normal file
27
module/Core/test-api/Action/QrCodeTest.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ShlinkioApiTest\Shlink\Core\Action;
|
||||
|
||||
use PHPUnit\Framework\Attributes\Test;
|
||||
use Shlinkio\Shlink\TestUtils\ApiTest\ApiTestCase;
|
||||
|
||||
class QrCodeTest extends ApiTestCase
|
||||
{
|
||||
#[Test]
|
||||
public function returnsNotFoundWhenShortUrlIsNotEnabled(): void
|
||||
{
|
||||
// The QR code successfully resolves at first
|
||||
$response = $this->callShortUrl('custom/qr-code');
|
||||
self::assertEquals(200, $response->getStatusCode());
|
||||
|
||||
// This short URL allow max 2 visits
|
||||
$this->callShortUrl('custom');
|
||||
$this->callShortUrl('custom');
|
||||
|
||||
// After 2 visits, the QR code should return a 404
|
||||
$response = $this->callShortUrl('custom/qr-code');
|
||||
self::assertEquals(404, $response->getStatusCode());
|
||||
}
|
||||
}
|
||||
@@ -230,6 +230,35 @@ class QrCodeActionTest extends TestCase
|
||||
];
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideEnabled')]
|
||||
public function qrCodeIsResolvedBasedOnOptions(bool $enabledForDisabledShortUrls): void
|
||||
{
|
||||
|
||||
if ($enabledForDisabledShortUrls) {
|
||||
$this->urlResolver->expects($this->once())->method('resolvePublicShortUrl')->willThrowException(
|
||||
ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')),
|
||||
);
|
||||
$this->urlResolver->expects($this->never())->method('resolveEnabledShortUrl');
|
||||
} else {
|
||||
$this->urlResolver->expects($this->once())->method('resolveEnabledShortUrl')->willThrowException(
|
||||
ShortUrlNotFoundException::fromNotFound(ShortUrlIdentifier::fromShortCodeAndDomain('')),
|
||||
);
|
||||
$this->urlResolver->expects($this->never())->method('resolvePublicShortUrl');
|
||||
}
|
||||
|
||||
$options = new QrCodeOptions(enabledForDisabledShortUrls: $enabledForDisabledShortUrls);
|
||||
$this->action($options)->process(
|
||||
ServerRequestFactory::fromGlobals(),
|
||||
$this->createMock(RequestHandlerInterface::class),
|
||||
);
|
||||
}
|
||||
|
||||
public static function provideEnabled(): iterable
|
||||
{
|
||||
yield 'always enabled' => [true];
|
||||
yield 'only enabled short URLs' => [false];
|
||||
}
|
||||
|
||||
public function action(?QrCodeOptions $options = null): QrCodeAction
|
||||
{
|
||||
return new QrCodeAction(
|
||||
|
||||
@@ -59,7 +59,7 @@ class ShortUrlResolverTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test, DataProviderExternal(ApiKeyDataProviders::class, 'adminApiKeysProvider')]
|
||||
public function exceptionIsThrownIfShortcodeIsNotFound(?ApiKey $apiKey): void
|
||||
public function exceptionIsThrownIfShortCodeIsNotFound(?ApiKey $apiKey): void
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$identifier = ShortUrlIdentifier::fromShortCodeAndDomain($shortCode);
|
||||
@@ -73,7 +73,7 @@ class ShortUrlResolverTest extends TestCase
|
||||
}
|
||||
|
||||
#[Test]
|
||||
public function shortCodeToEnabledShortUrlProperlyParsesShortCode(): void
|
||||
public function resolveEnabledShortUrlProperlyParsesShortCode(): void
|
||||
{
|
||||
$shortUrl = ShortUrl::withLongUrl('https://expected_url');
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
@@ -89,8 +89,30 @@ class ShortUrlResolverTest extends TestCase
|
||||
self::assertSame($shortUrl, $result);
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideResolutionMethods')]
|
||||
public function resolutionThrowsExceptionIfUrlIsNotEnabled(string $method): void
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
|
||||
$this->repo->expects($this->once())->method('findOneWithDomainFallback')->with(
|
||||
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
||||
ShortUrlMode::STRICT,
|
||||
)->willReturn(null);
|
||||
$this->em->expects($this->once())->method('getRepository')->with(ShortUrl::class)->willReturn($this->repo);
|
||||
|
||||
$this->expectException(ShortUrlNotFoundException::class);
|
||||
|
||||
$this->urlResolver->{$method}(ShortUrlIdentifier::fromShortCodeAndDomain($shortCode));
|
||||
}
|
||||
|
||||
public static function provideResolutionMethods(): iterable
|
||||
{
|
||||
yield 'resolveEnabledShortUrl' => ['resolveEnabledShortUrl'];
|
||||
yield 'resolvePublicShortUrl' => ['resolvePublicShortUrl'];
|
||||
}
|
||||
|
||||
#[Test, DataProvider('provideDisabledShortUrls')]
|
||||
public function shortCodeToEnabledShortUrlThrowsExceptionIfUrlIsNotEnabled(ShortUrl $shortUrl): void
|
||||
public function resolveEnabledShortUrlThrowsExceptionIfUrlIsNotEnabled(ShortUrl $shortUrl): void
|
||||
{
|
||||
$shortCode = $shortUrl->getShortCode();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user