Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds messagehandling for form outputs #13

Merged
merged 4 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/Controller/FormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Symfony\Component\Serializer\Exception\ExceptionInterface as SerializerException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Valantic\PimcoreFormsBundle\Http\ApiResponse;
use Valantic\PimcoreFormsBundle\Model\Message;
use Valantic\PimcoreFormsBundle\Service\FormService;

class FormController extends AbstractController
Expand Down Expand Up @@ -80,21 +81,27 @@ public function apiAction(string $name, FormService $formService, Request $reque
if ($form->isValid()) {
$data = $form->getData();

$outputSuccess = $formService->outputs($form);
$outputResponse = $formService->outputs($form);

$redirectUrl = $formService->getRedirectUrl($form, $outputSuccess);
$redirectUrl = $formService->getRedirectUrl($form, $outputResponse->getOverallStatus());

$messages = $outputSuccess
$messages = $outputResponse->getOverallStatus()
? [
'type' => ApiResponse::MESSAGE_TYPE_SUCCESS,
'message' => $translator->trans('valantic.pimcoreForms.formSubmitSuccess'),
(new Message())
->setType(ApiResponse::MESSAGE_TYPE_SUCCESS)
->setMessage($translator->trans('valantic.pimcoreForms.formSubmitSuccess')),
]
: [
'type' => ApiResponse::MESSAGE_TYPE_ERROR,
'message' => $translator->trans('valantic.pimcoreForms.formSubmitError'),
(new Message())
->setType(ApiResponse::MESSAGE_TYPE_ERROR)
->setMessage($translator->trans('valantic.pimcoreForms.formSubmitError')),
];

$statusCode = $outputSuccess
if (!empty($outputResponse->getMessages())) {
$messages = $outputResponse->getMessages();
}

$statusCode = $outputResponse->getOverallStatus()
? JsonResponse::HTTP_OK
: JsonResponse::HTTP_PRECONDITION_FAILED;

Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
*/
class Configuration implements ConfigurationInterface
{
const SYMFONY_CONSTRAINTS_NAMESPACE = 'Symfony\\Component\\Validator\\Constraints\\';
const SYMFONY_FORMTYPES_NAMESPACE = 'Symfony\\Component\\Form\\Extension\\Core\\Type\\';
public const SYMFONY_CONSTRAINTS_NAMESPACE = 'Symfony\\Component\\Validator\\Constraints\\';
public const SYMFONY_FORMTYPES_NAMESPACE = 'Symfony\\Component\\Form\\Extension\\Core\\Type\\';

/**
* {@inheritdoc}
Expand Down
5 changes: 3 additions & 2 deletions src/Form/Output/AssetOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Pimcore\Model\Asset\Folder;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Valantic\PimcoreFormsBundle\Model\OutputResponse;
use voku\helper\ASCII;

class AssetOutput extends AbstractOutput
Expand All @@ -18,7 +19,7 @@ public static function name(): string
return 'asset';
}

public function handle(): bool
public function handle(OutputResponse $outputResponse): OutputResponse
{
$path = Folder::getByPath($this->getPath());

Expand Down Expand Up @@ -50,7 +51,7 @@ public function handle(): bool

OutputScratchpad::set($this->key, ['path' => $subfolder->getFullPath(), 'count' => $count]);

return true;
return $outputResponse->addStatus(true);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Form/Output/DataObjectOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use InvalidArgumentException;
use Pimcore\Model\DataObject\Concrete;
use Valantic\PimcoreFormsBundle\Model\OutputResponse;

class DataObjectOutput extends AbstractOutput
{
Expand All @@ -14,7 +15,7 @@ public static function name(): string
return 'data_object';
}

public function handle(): bool
public function handle(OutputResponse $outputResponse): OutputResponse
{
$objClass = 'Pimcore\\Model\\DataObject\\' . $this->config['class'];

Expand Down Expand Up @@ -46,7 +47,7 @@ public function handle(): bool
$obj->setParentId($pathId);
$obj->save();

return true;
return $outputResponse->addStatus(true);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Form/Output/EmailOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Pimcore\Mail;
use Pimcore\Model\Document;
use Valantic\PimcoreFormsBundle\Model\OutputResponse;

class EmailOutput extends AbstractOutput
{
Expand All @@ -14,7 +15,7 @@ public static function name(): string
return 'email';
}

public function handle(): bool
public function handle(OutputResponse $outputResponse): OutputResponse
{
$mail = new Mail();
$mail->addTo($this->getTo());
Expand All @@ -35,7 +36,7 @@ public function handle(): bool

$mail->send();

return true;
return $outputResponse->addStatus(true);
}

protected function getTo(): string
Expand Down
5 changes: 3 additions & 2 deletions src/Form/Output/HttpOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Valantic\PimcoreFormsBundle\Form\Output;

use RuntimeException;
use Valantic\PimcoreFormsBundle\Model\OutputResponse;

class HttpOutput extends AbstractOutput
{
Expand All @@ -13,7 +14,7 @@ public static function name(): string
return 'http';
}

public function handle(): bool
public function handle(OutputResponse $outputResponse): OutputResponse
{
$ch = curl_init($this->config['url']);
if ($ch === false) {
Expand All @@ -26,6 +27,6 @@ public function handle(): bool
$status = curl_exec($ch);
curl_close($ch);

return $status !== false;
return $outputResponse->addStatus($status !== false);
}
}
7 changes: 4 additions & 3 deletions src/Form/Output/LogOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LoggerInterface;
use Valantic\PimcoreFormsBundle\Model\OutputResponse;

class LogOutput extends AbstractOutput implements LoggerAwareInterface
{
Expand All @@ -17,14 +18,14 @@ public static function name(): string
return 'log';
}

public function handle(): bool
public function handle(OutputResponse $outputResponse): OutputResponse
{
if (!$this->logger instanceof LoggerInterface) {
return false;
return $outputResponse->addStatus(false);
}

$this->logger->log($this->config['level'] ?? 'debug', $this->form->getName(), $this->form->getData());

return true;
return $outputResponse->addStatus(true);
}
}
3 changes: 2 additions & 1 deletion src/Form/Output/OutputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Valantic\PimcoreFormsBundle\Form\Output;

use Symfony\Component\Form\FormInterface;
use Valantic\PimcoreFormsBundle\Model\OutputResponse;

interface OutputInterface
{
Expand All @@ -22,7 +23,7 @@ public function initialize(string $key, FormInterface $form, array $config): voi
*/
public function setOutputHandlers(array $handlers): void;

public function handle(): bool;
public function handle(OutputResponse $outputResponse): OutputResponse;

public static function name(): string;
}
103 changes: 103 additions & 0 deletions src/Model/AbstractMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreFormsBundle\Model;

use Iterator;
use JsonSerializable;
use ReturnTypeWillChange;
use RuntimeException;

/**
* @implements Iterator<int, mixed>
*/
abstract class AbstractMessage implements JsonSerializable, Iterator, \Stringable
limenet marked this conversation as resolved.
Show resolved Hide resolved
{
protected ?string $position = null;

public function __toString(): string
{
return (string) json_encode($this->jsonSerialize(), \JSON_THROW_ON_ERROR);
}

/**
* @return array<mixed>
*/
public function jsonSerialize(): array
{
return $this->arraySerialize();
}

public function valid(): bool
{
return in_array($this->position, $this->validKeys(), true);
}

public function next(): void
{
$keys = $this->validKeys();
$pos = array_search($this->position, $keys, true);

$this->position = $keys[(int) $pos + 1] ?? null;
}

#[ReturnTypeWillChange]
public function current()
{
return $this->{$this->position};
}

public function rewind(): void
{
$this->position = $this->validKeys()[0];
}

/**
* @return array<mixed>
*/
public function arraySerialize(): array
{
$data = [];

foreach ($this->requiredAttributes() as $attribute) {
if (!isset($this->{$attribute})) {
throw new RuntimeException();
}
$data[$attribute] = $this->{$attribute};
}

foreach ($this->optionalAttributes() as $attribute) {
if (!isset($this->{$attribute})) {
continue;
}

$data[$attribute] = $this->{$attribute};
}

return $data;
}

public function key(): ?string
{
return $this->position;
}

/**
* @return array<mixed>
*/
protected function validKeys(): array
{
return array_keys($this->arraySerialize());
}

/**
* @return array<mixed>
*/
abstract protected function requiredAttributes(): array;

/**
* @return array<mixed>
*/
abstract protected function optionalAttributes(): array;
}
83 changes: 83 additions & 0 deletions src/Model/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreFormsBundle\Model;

use InvalidArgumentException;
use Valantic\PimcoreFormsBundle\Http\ApiResponse;

class Message extends AbstractMessage
{
protected string $type;
protected string $message;
protected bool $expire;
protected int $delay;

/**
* @var array<mixed>
*/
protected array $source;
protected string $field;

public function setType(string $type): self
{
if (!in_array($type, ApiResponse::MESSAGE_TYPES, true)) {
limenet marked this conversation as resolved.
Show resolved Hide resolved
throw new InvalidArgumentException();
}

$this->type = $type;

return $this;
}

public function setMessage(string $message): self
{
$this->message = $message;

return $this;
}

public function setExpire(bool $expire): self
{
$this->expire = $expire;

return $this;
}

public function setDelay(int $seconds): self
{
$this->delay = $seconds;

return $this;
}

/**
* @param array<mixed> $source
*
* @return $this
*/
public function setSource(array $source): self
{
$this->source = $source;

return $this;
}

public function setField(string $field): self
{
$this->field = $field;

return $this;
}

protected function requiredAttributes(): array
{
return ['type', 'message'];
}

protected function optionalAttributes(): array
{
return ['expire', 'delay', 'source', 'field'];
}
}
Loading