refactor: Add test controller.

This commit is contained in:
Pol Dellaiera 2021-08-19 16:25:53 +02:00 committed by Marc Ducobu
parent 7d74967330
commit 07c3594107
4 changed files with 244 additions and 9 deletions

View File

@ -9,22 +9,82 @@ declare(strict_types=1);
namespace Chill\WopiBundle\Controller;
use Chill\WopiBundle\Service\OpenStack\OpenStackClientInterface;
use ChampsLibres\WopiLib\Configuration\WopiConfigurationInterface;
use ChampsLibres\WopiLib\Discovery\WopiDiscoveryInterface;
use Chill\DocStoreBundle\Repository\StoredObjectRepository;
use Chill\WopiBundle\Service\Controller\ResponderInterface;
use Exception;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
final class Test
{
private OpenStackClientInterface $client;
private StoredObjectRepository $storedObjectRepository;
public function __construct(OpenStackClientInterface $client)
{
$this->client = $client;
private WopiDiscoveryInterface $wopiDiscovery;
private WopiConfigurationInterface $wopiConfiguration;
private ResponderInterface $responder;
private Security $security;
public function __construct(
StoredObjectRepository $storedObjectRepository,
WopiConfigurationInterface $wopiConfiguration,
WopiDiscoveryInterface $wopiDiscovery,
ResponderInterface $responder,
Security $security
) {
$this->storedObjectRepository = $storedObjectRepository;
$this->wopiConfiguration = $wopiConfiguration;
$this->wopiDiscovery = $wopiDiscovery;
$this->responder = $responder;
$this->security = $security;
}
public function __invoke()
public function __invoke(string $fileId): Response
{
dump($this->client);
$configuration = $this->wopiConfiguration->jsonSerialize();
$storedObject = $this->storedObjectRepository->findOneBy(['filename' => $fileId]);
if (null === $storedObject) {
throw new Exception(sprintf('Unable to find object named %s', $fileId));
}
if ([] === $discoverExtension = $this->wopiDiscovery->discoverMimeType($storedObject->getType())) {
throw new Exception(sprintf('Unable to find mime type %s', $storedObject->getType()));
}
$configuration['access_token'] = $this->security->getUser()->getUsername();
$configuration['server'] = $this
->psr17
->createUri($discoverExtension[0]['urlsrc'])
->withQuery(
http_build_query(
[
'WOPISrc' => $this
->router
->generate(
'checkFileInfo',
[
'fileId' => $storedObject->getId(),
],
UrlGeneratorInterface::ABSOLUTE_URL
),
]
)
);
return $this
->responder
->render(
'@WopiBundle/Editor/page.html.twig',
$configuration
);
return new Response('fobar');
}
}

View File

@ -12,6 +12,6 @@ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
return static function (RoutingConfigurator $routes) {
$routes
->add('testtest', '/test')
->add('testtest', '/edit/{fileId}')
->controller(Test::class);
};

View File

@ -0,0 +1,101 @@
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\WopiBundle\Service\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Twig\Environment;
final class Responder implements ResponderInterface
{
private SerializerInterface $serializer;
private Environment $twig;
private UrlGeneratorInterface $urlGenerator;
public function __construct(
Environment $twig,
UrlGeneratorInterface $urlGenerator,
SerializerInterface $serializer
) {
$this->twig = $twig;
$this->urlGenerator = $urlGenerator;
$this->serializer = $serializer;
}
public function file(
$file,
?string $filename = null,
string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT
): BinaryFileResponse {
$response = new BinaryFileResponse($file);
$filename ??= $response->getFile()->getFilename();
$response->setContentDisposition($disposition, $filename);
return $response;
}
public function json(
$data,
int $status = 200,
array $headers = [],
array $context = []
): JsonResponse {
return new JsonResponse(
$this
->serializer
->serialize(
$data,
'json',
array_merge(
[
'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
],
$context
)
),
$status,
$headers,
true
);
}
public function redirect(string $url, int $status = 302, array $headers = []): RedirectResponse
{
return new RedirectResponse($url, $status, $headers);
}
public function redirectToRoute(
string $route,
array $parameters = [],
int $status = 302,
array $headers = []
): RedirectResponse {
return $this->redirect($this->urlGenerator->generate($route, $parameters), $status, $headers);
}
public function render(string $template, array $context = [], int $status = 200, array $headers = []): Response
{
$response = new Response($this->twig->render($template, $context), $status, $headers);
if (!$response->headers->has('Content-Type')) {
$response->headers->set('Content-Type', 'text/html; charset=UTF-8');
}
return $response;
}
}

View File

@ -0,0 +1,74 @@
<?php
/**
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Chill\WopiBundle\Service\Controller;
use SplFileInfo;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Twig\Error\Error as TwigError;
interface ResponderInterface
{
/**
* Returns a BinaryFileResponse object with original or customized file name and disposition header.
*
* @param SplFileInfo|string $file
*/
public function file(
$file,
?string $filename = null,
string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT
): BinaryFileResponse;
/**
* Returns a JsonResponse that uses the serializer component if enabled, or json_encode.
*
* @param array<string, list<string>|string> $headers
* @param array<string, mixed> $context
*/
public function json(
mixed $data,
int $status = 200,
array $headers = [],
array $context = []
): JsonResponse;
/**
* Returns a RedirectResponse to the given URL.
*
* @param array<string, list<string>|string> $headers
*/
public function redirect(string $url, int $status = 302, array $headers = []): RedirectResponse;
/**
* Returns a RedirectResponse to the given route with the given parameters.
*
* @param array<array-key, scalar> $parameters
* @param array<string, list<string>> $headers
*/
public function redirectToRoute(
string $route,
array $parameters = [],
int $status = 302,
array $headers = []
): RedirectResponse;
/**
* Render the given twig template and return an HTML response.
*
* @param array<string, list<string>|string> $headers
*
* @throws TwigError
*/
public function render(string $template, array $context = [], int $status = 200, array $headers = []): Response;
}