-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
1,252 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Adapter\Controllers; | ||
|
||
use App\Domain\Repository\LongUrlRepository; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\RequestInterface as Request; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Slim\Views\Twig; | ||
|
||
final class AccessUrlController | ||
{ | ||
private Twig $view; | ||
private LongUrlRepository $longUrlRepo; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->view = $container->get('view'); | ||
$this->longUrlRepo = $container->get(LongUrlRepository::class); | ||
} | ||
|
||
public function __invoke(Request $request, Response $response, array $args): Response | ||
{ | ||
$url = $this->longUrlRepo->getUrlByPath($args['path']); | ||
|
||
if (is_null($url)) { | ||
return $this->view->render($response, 'notfound.html.twig', []); | ||
} | ||
|
||
$this->longUrlRepo->registerAccess($url, [ | ||
'REMOTE_ADDR' => $_SERVER['REMOTE_ADDR'], | ||
'REMOTE_PORT' => $_SERVER['REMOTE_PORT'], | ||
'SERVER_NAME' => $_SERVER['SERVER_NAME'], | ||
'REQUEST_URI' => $_SERVER['REQUEST_URI'], | ||
'HTTP_HOST' => $_SERVER['HTTP_HOST'], | ||
'HTTP_USER_AGENT' => $_SERVER['HTTP_USER_AGENT'] | ||
]); | ||
|
||
$newResponse = $response | ||
->withHeader('Content-type', 'application/json') | ||
->withStatus(302) | ||
->withHeader('Location', $url->longUrl->value()); | ||
|
||
return $newResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Adapter\Controllers; | ||
|
||
use App\Domain\UseCase\ShortenUrl\InputData; | ||
use App\Domain\UseCase\ShortenUrl\ShortenUrl; | ||
use App\Domain\ValueObject\LongUrlType; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Http\Message\RequestInterface as Request; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
|
||
final class ShortenUrlController | ||
{ | ||
private array $config; | ||
private ShortenUrl $useCase; | ||
|
||
public function __construct(ContainerInterface $container) | ||
{ | ||
$this->config = $container->get('config'); | ||
$this->useCase = $container->get(ShortenUrl::class); | ||
} | ||
|
||
public function __invoke(Request $request, Response $response, array $args): Response | ||
{ | ||
$contents = $request->getParsedBody(); | ||
|
||
if (empty($contents) || !array_key_exists('long_url', $contents)) { | ||
$newResponse = $response | ||
->withHeader('Content-type', 'application/json') | ||
->withStatus(400); | ||
|
||
$newResponse->getBody()->write(json_encode([ | ||
'status' => 'fail', | ||
'data' => ['long_url' => 'missing-param'] | ||
])); | ||
|
||
return $newResponse; | ||
} | ||
|
||
if (empty($contents['long_url'])) { | ||
$newResponse = $response | ||
->withHeader('Content-type', 'application/json') | ||
->withStatus(400); | ||
|
||
$newResponse->getBody()->write(json_encode([ | ||
'status' => 'fail', | ||
'data' => ['long_url' => 'empty-value'] | ||
])); | ||
|
||
return $newResponse; | ||
} | ||
|
||
$result = $this->useCase->execute(InputData::create([ | ||
'longUrl' => $contents['long_url'], | ||
'type' => LongUrlType::TYPE_RANDOM, | ||
'baseUrl' => $this->config['baseUrl'], | ||
])); | ||
|
||
$newResponse = $response | ||
->withHeader('Content-type', 'application/json') | ||
->withStatus(200); | ||
|
||
$newResponse->getBody()->write(json_encode([ | ||
'status' => 'success', | ||
'data' => $result->values() | ||
])); | ||
|
||
return $newResponse; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Middleware/SlimFlashMiddleware.php → ...dapter/Middleware/SlimFlashMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.