Skip to content

Instantly share code, notes, and snippets.

@lakiboy
Last active March 22, 2019 07:37
Show Gist options
  • Save lakiboy/f203d17ccd63301d47ee30a733e16876 to your computer and use it in GitHub Desktop.
Save lakiboy/f203d17ccd63301d47ee30a733e16876 to your computer and use it in GitHub Desktop.
PLAT-284
<?php
namespace Api\Controller\Client\BankStatement;
use Api\Controller\Base\BaseRestController;
use Api\Form\Submit\Client\BankStatement\BankStatementType;
use Api\Response\ApiException;
use Api\Response\ApiFormException;
use FinCore\Domain\Client\Contract\CommandBus\Commands\UploadBankStatementCommand;
use MessageBus\Alias\CommandBusInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Validation;
final class ClientBankStatementUploadController extends BaseRestController
{
private $formFactory;
private $commandBus;
public function __construct(FormFactoryInterface $formFactory, CommandBusInterface $commandBus)
{
$this->formFactory = $formFactory;
$this->commandBus = $commandBus;
}
public function create($data)
{
$this->assertClientAuthenticated();
$files = $this->getRequest()->getFiles()->toArray();
$form = $this->formFactory
->create(BankStatementType::class)
->submit($files)
;
if (!$form->isValid()) {
throw new ApiFormException($form, Response::HTTP_BAD_REQUEST);
}
/** @var UploadedFile $file */
$file = $form->getData()['statement'];
$this->commandBus->handle(
new UploadBankStatementCommand(
$file->getClientOriginalName(),
$file->getPath(),
$this->getClient()
)
);
return $this->success(Response::HTTP_CREATED);
}
}
<?php
declare(strict_types=1);
namespace Api\Form\Submit\Client\BankStatement;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\NotBlank;
use Zend\Diactoros\UploadedFile;
final class BankStatementType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('statement', FileType::class, [
'constraints' => [
new NotBlank(),
new File([
'maxSize' => '5M',
'mimeTypes' => $options['mime_types'],
'mimeTypesMessage' => 'Valid document file are: jpg, jpeg, gif, png, pdf, csv, xls, xlsx, doc, docx (Max. 5Mb)',
]),
],
])
->addEventListener(FormEvents::PRE_SET_DATA, [$this, 'preSetData'])
;
}
public function preSetData(FormEvent $event): void
{
$data = $event->getData();
if (isset($data['statement'])) {
$event->setData([
'statement' => new UploadedFile(
$data['statement']['tmp_name'],
$data['statement']['name'],
$data['statement']['type'],
$data['statement']['size'],
$data['statement']['error']
)
]);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'mime_types' => [
'application/pdf',
'image/jpeg',
'image/png',
'image/gif',
'text/csv',
'application/vnd.ms-excel',
'application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
],
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment