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

Add actions #364

Merged
merged 2 commits into from
Feb 18, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"php": "^7.3 || ^8.0",
"doctrine/dbal": "^2.5",
"doctrine/orm": "^2.7",
"symfony/config": "^4.4 || ^5.2"
"symfony/config": "^4.4 || ^5.2",
"twig/twig": "^2.6 || ^3.0"
},
"conflict": {
"doctrine/dbal": "2.7.0",
Expand Down
62 changes: 62 additions & 0 deletions src/Action/CompareAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SimpleThings\EntityAudit\Action;

use SimpleThings\EntityAudit\AuditReader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class CompareAction
{
/**
* @var AuditReader
*/
private $auditReader;

/**
* @var Environment
*/
private $twig;

public function __construct(Environment $twig, AuditReader $auditReader)
{
$this->twig = $twig;
$this->auditReader = $auditReader;
}

public function __invoke(Request $request, string $className, string $id, ?int $oldRev = null, ?int $newRev = null): Response
{
if (null === $oldRev) {
$oldRev = $request->query->get('oldRev');
}

if (null === $newRev) {
$newRev = $request->query->get('newRev');
}

$ids = explode(',', $id);
$diff = $this->auditReader->diff($className, $ids, $oldRev, $newRev);

$content = $this->twig->render('@SimpleThingsEntityAudit/Audit/compare.html.twig', [
'className' => $className,
'id' => $id,
'oldRev' => $oldRev,
'newRev' => $newRev,
'diff' => $diff,
]);

return new Response($content);
}
}
48 changes: 48 additions & 0 deletions src/Action/IndexAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SimpleThings\EntityAudit\Action;

use SimpleThings\EntityAudit\AuditReader;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class IndexAction
{
/**
* @var AuditReader
*/
private $auditReader;

/**
* @var Environment
*/
private $twig;

public function __construct(Environment $twig, AuditReader $auditReader)
{
$this->twig = $twig;
$this->auditReader = $auditReader;
}

public function __invoke(int $page = 1): Response
{
$revisions = $this->auditReader->findRevisionHistory(20, 20 * ($page - 1));

$content = $this->twig->render('@SimpleThingsEntityAudit/Audit/index.html.twig', [
'revisions' => $revisions,
]);

return new Response($content);
}
}
56 changes: 56 additions & 0 deletions src/Action/ViewDetailAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SimpleThings\EntityAudit\Action;

use SimpleThings\EntityAudit\AuditReader;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class ViewDetailAction
{
/**
* @var AuditReader
*/
private $auditReader;

/**
* @var Environment
*/
private $twig;

public function __construct(Environment $twig, AuditReader $auditReader)
{
$this->twig = $twig;
$this->auditReader = $auditReader;
}

public function __invoke(string $className, string $id, int $rev): Response
{
$ids = explode(',', $id);
$entity = $this->auditReader->find($className, $ids, $rev);

$data = $this->auditReader->getEntityValues($className, $entity);
krsort($data);

$content = $this->twig->render('@SimpleThingsEntityAudit/Audit/view_detail.html.twig', [
'id' => $id,
'rev' => $rev,
'className' => $className,
'entity' => $entity,
'data' => $data,
]);

return new Response($content);
}
}
51 changes: 51 additions & 0 deletions src/Action/ViewEntityAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SimpleThings\EntityAudit\Action;

use SimpleThings\EntityAudit\AuditReader;
use Symfony\Component\HttpFoundation\Response;
use Twig\Environment;

final class ViewEntityAction
{
/**
* @var AuditReader
*/
private $auditReader;

/**
* @var Environment
*/
private $twig;

public function __construct(Environment $twig, AuditReader $auditReader)
{
$this->twig = $twig;
$this->auditReader = $auditReader;
}

public function __invoke(string $className, string $id): Response
{
$ids = explode(',', $id);
$revisions = $this->auditReader->findRevisions($className, $ids);

$content = $this->twig->render('@SimpleThingsEntityAudit/Audit/view_entity.html.twig', [
'id' => $id,
'className' => $className,
'revisions' => $revisions,
]);

return new Response($content);
}
}
55 changes: 55 additions & 0 deletions src/Action/ViewRevisionAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace SimpleThings\EntityAudit\Action;

use SimpleThings\EntityAudit\AuditReader;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Twig\Environment;

final class ViewRevisionAction
{
/**
* @var AuditReader
*/
private $auditReader;

/**
* @var Environment
*/
private $twig;

public function __construct(Environment $twig, AuditReader $auditReader)
{
$this->twig = $twig;
$this->auditReader = $auditReader;
}

public function __invoke(int $rev): Response
{
$revision = $this->auditReader->findRevision($rev);
if (!$revision) {
throw new NotFoundHttpException(sprintf('Revision %i not found', $rev));
}

$changedEntities = $this->auditReader->findEntitiesChangedAtRevision($rev);

$content = $this->twig->render('@SimpleThingsEntityAudit/Audit/view_revision.html.twig', [
'revision' => $revision,
'changedEntities' => $changedEntities,
]);

return new Response($content);
}
}
Loading