Skip to content

Commit

Permalink
Support messages in output handlers using OutputResponse (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
russo1337 committed Jun 29, 2022
1 parent d1eebb4 commit 9ae9286
Show file tree
Hide file tree
Showing 14 changed files with 451 additions and 149 deletions.
20 changes: 20 additions & 0 deletions src/Constant/MessageConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreFormsBundle\Constant;

interface MessageConstants
{
public const MESSAGE_TYPE_SUCCESS = 'success';
public const MESSAGE_TYPE_ERROR = 'error';
public const MESSAGE_TYPE_WARNING = 'warning';
public const MESSAGE_TYPE_INFO = 'info';

public const MESSAGE_TYPES = [
self::MESSAGE_TYPE_SUCCESS,
self::MESSAGE_TYPE_ERROR,
self::MESSAGE_TYPE_WARNING,
self::MESSAGE_TYPE_INFO,
];
}
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;
}
104 changes: 104 additions & 0 deletions src/Model/AbstractMessage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreFormsBundle\Model;

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

/**
* @implements Iterator<string, mixed>
*/
abstract class AbstractMessage implements JsonSerializable, Iterator, Stringable
{
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;
}
Loading

0 comments on commit 9ae9286

Please sign in to comment.