Skip to content

Commit

Permalink
feat: Update QR-Code after Node-Publish
Browse files Browse the repository at this point in the history
- Update the QR-Code Image after a node was published
- Update Commands
- Use an own Model for the QR-Code

BREAKING CHANGE: new structure and updated dependencies with breaking change
  • Loading branch information
erkenes committed Apr 11, 2023
1 parent 5799167 commit 09ffd79
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 112 deletions.
65 changes: 55 additions & 10 deletions Classes/Command/QrCodeCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use NextBox\Neos\QrCode\Domain\Model\QrCode;
use NextBox\Neos\QrCode\Services\QrCodeService;
use NextBox\Neos\UrlShortener\Domain\Model\UrlShortener;
use NextBox\Neos\UrlShortener\Domain\Repository\UrlShortenerRepository;

class QrCodeCommandController extends CommandController
{
Expand All @@ -21,6 +24,12 @@ class QrCodeCommandController extends CommandController
*/
protected $persistenceManager;

/**
* @Flow\Inject
* @var UrlShortenerRepository
*/
protected $urlShortenerRepository;

/**
* Generate a new qr code if it is missing
* Force remove the existing qr code if the argument `force` is set
Expand All @@ -32,19 +41,19 @@ class QrCodeCommandController extends CommandController
*/
public function generateCommand(string $shortIdentifier, string $shortType = 'default', bool $force = false): void
{
$resource = $this->qrCodeService->getFile($shortIdentifier, $shortType);
$qrCode = $this->getQrCode($shortIdentifier, $shortType);

$resource = $this->qrCodeService->getFileForQrCode($qrCode);

if ($resource && !$force) {
$this->output->outputLine('<comment>There is an image already existing. Use --force to overwrite this image and create a new one.</comment>');
} else {
if ($force) {
$this->qrCodeService->deleteFile($shortIdentifier, $shortType);
$this->qrCodeService->deleteQrCodeResource($qrCode);
$this->output->outputLine('<comment>The qr-code was force removed</comment>');
$this->persistenceManager->persistAll();
}

// ToDo: Uri has `/./` after the TLD
$this->qrCodeService->generateAndSendToUri($shortIdentifier, $shortType);
$this->qrCodeService->getOrCreateQrCodeResource($qrCode->getUrlShortener());
$this->output->outputLine('<success>The qr-code with the `url-short-identifier` ' . $shortIdentifier . ' was created successfully.</success>');
}
}
Expand All @@ -56,14 +65,50 @@ public function generateCommand(string $shortIdentifier, string $shortType = 'de
* @param string $shortType
* @return void
*/
public function removeImageCommand(string $shortIdentifier, string $shortType = 'default'): void
{
$qrCode = $this->getQrCode($shortIdentifier, $shortType);

$this->qrCodeService->deleteQrCodeResource($qrCode);
$this->output->outputLine('<success>The qr-code image for the `short-identifier` ' . $shortIdentifier . ' was removed successfully.</success>');
}

/**
* Delete a qr-code
*
* @param string $shortIdentifier the value of the short identifier
* @param string $shortType
* @return void
*/
public function removeCommand(string $shortIdentifier, string $shortType = 'default'): void
{
$state = $this->qrCodeService->deleteFile($shortIdentifier, $shortType);
$qrCode = $this->getQrCode($shortIdentifier, $shortType);

if ($state) {
$this->output->outputLine('<success>The qr-code with the `short-identifier` ' . $shortIdentifier . ' was removed successfully.</success>');
} else {
$this->output->outputLine('<error>Can not find a qr-code with the `short-identifier` ' . $shortIdentifier . '</error>');
$this->qrCodeService->remove($qrCode);
$this->output->outputLine('<success>The qr-code for the `short-identifier` ' . $shortIdentifier . ' was removed successfully.</success>');
}

/**
* Check for UrlShortener and return qr code
*
* @param string $shortIdentifier
* @param string $shortType
* @return QrCode
*/
protected function getQrCode(string $shortIdentifier, string $shortType = 'default'): QrCode
{
$urlShortener = $this->urlShortenerRepository->findOneByShortIdentifierAndShortType($shortIdentifier, $shortType);
if (!$urlShortener instanceof UrlShortener) {
$this->output->outputLine('<error>Can not find a shortened url with those values.</error>');
$this->sendAndExit(1);
}

$qrCode = $this->qrCodeService->findQrCode($urlShortener);
if (!$qrCode instanceof QrCode) {
$this->output->outputLine('<error>Can not find a Qr-Code for the shortened url.</error>');
$this->sendAndExit(1);
}

return $qrCode;
}
}
11 changes: 7 additions & 4 deletions Classes/Controller/QrCodeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,18 @@ public function generateQrCodeAction(string $shortIdentifier, string $shortType
throw new PageNotFoundException();
}

$resource = $this->qrCodeService->getQrCodeResource($this->redirectService->createShortUri($shortIdentifier, $shortType), $shortIdentifier, $shortType);
$urlShortener = $this->qrCodeService->findUrlShortener($shortIdentifier, $shortType);
$resource = $this->qrCodeService->getOrCreateQrCodeResource($urlShortener);

$fileName = 'qrcode_' . $shortIdentifier . '_' . ($documentNode->getProperty('title') ?: '') . '.' . QrCodeService::FILE_EXTENSION;

$this->response->setContentType('image/png');
$this->response->setHttpHeader('Content-disposition', 'inline; filename="qrcode_' . $shortIdentifier . '_' . ($documentNode->getProperty('title') ?: '') . '.png"');
$this->response->setHttpHeader('Content-disposition', 'inline; filename="' . $fileName . '"');
$this->response->setHttpHeader('Content-Control', 'public, must-revalidate, max-age=0');
$this->response->setHttpHeader('Pragma', 'public');
$this->response->setHttpHeader('Last-Modified', gmdate('D, d M Y H:i:s') . ' GMT');
$this->response->setContent($resource->getStream());
-
throw new StopActionException();
-
throw new StopActionException();
}
}
73 changes: 73 additions & 0 deletions Classes/Domain/Model/QrCode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace NextBox\Neos\QrCode\Domain\Model;

use Doctrine\ORM\Mapping as ORM;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\ResourceManagement\PersistentResource;
use NextBox\Neos\UrlShortener\Domain\Model\UrlShortener;

/**
* @Flow\Entity
*/
class QrCode
{
/**
* @ORM\OneToOne(cascade={"all"})
* @var UrlShortener
*/
protected UrlShortener $urlShortener;

/**
* @ORM\OneToOne(cascade={"all"})
* @var PersistentResource|null
*/
protected ?PersistentResource $resource = null;


/**
* Getter for urlShortener
*
* @return UrlShortener
*/
public function getUrlShortener(): UrlShortener
{
return $this->urlShortener;
}

/**
* Setter for urlShortener
*
* @param UrlShortener $urlShortener
* @return QrCode
*/
public function setUrlShortener(UrlShortener $urlShortener): QrCode
{
$this->urlShortener = $urlShortener;

return $this;
}

/**
* Getter for resource
*
* @return PersistentResource|null
*/
public function getResource(): ?PersistentResource
{
return $this->resource;
}

/**
* Setter for resource
*
* @param PersistentResource|null $resource
* @return QrCode
*/
public function setResource(?PersistentResource $resource): QrCode
{
$this->resource = $resource;

return $this;
}
}
17 changes: 17 additions & 0 deletions Classes/Domain/Repository/QrCodeRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace NextBox\Neos\QrCode\Domain\Repository;

use Neos\Flow\Annotations as Flow;
use Neos\Flow\Persistence\Repository;
use NextBox\Neos\QrCode\Domain\Model\QrCode;
use NextBox\Neos\UrlShortener\Domain\Model\UrlShortener;

/**
* @Flow\Scope("singleton")
*
* @method QrCode|null findOneByUrlShortener(UrlShortener $urlShortener)
*/
class QrCodeRepository extends Repository
{
}
77 changes: 77 additions & 0 deletions Classes/Services/BackendService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace NextBox\Neos\QrCode\Services;

use Neos\ContentRepository\Domain\Model\NodeInterface;
use Neos\Flow\Annotations as Flow;
use NextBox\Neos\QrCode\Domain\Model\QrCode;
use NextBox\Neos\QrCode\Domain\Repository\QrCodeRepository;
use NextBox\Neos\UrlShortener\Domain\Model\UrlShortener;

/**
* @Flow\Scope("singleton")
*/
class BackendService extends \NextBox\Neos\UrlShortener\Services\BackendService
{
/**
* @Flow\Inject
* @var QrCodeService
*/
protected $qrCodeService;

/**
* @Flow\Inject
* @var QrCodeRepository
*/
protected $qrCodeRepository;

/**
* @Flow\InjectConfiguration(path="shortTypes", package="NextBox.Neos.UrlShortener")
* @var array
*/
protected array $typeSettings;

/**
* Loop to update the short identifier or create a new entry
*
* @param NodeInterface $node
* @return void
*/
public function updateNode(NodeInterface $node): void
{
foreach ($this->typeSettings as $shortType => $setting) {
$nodeTypeName = $setting['nodeType'];
$propertyName = $setting['property'];

$this->handleQrCode($node, $shortType, $nodeTypeName, $propertyName);
}
}

/**
* Update the short identifier or create a new entry
*
* @param NodeInterface $node
* @param string $shortType
* @param string $nodeTypeName
* @param string $propertyName
* @return UrlShortener|null
*/
protected function handleQrCode(NodeInterface $node, string $shortType, string $nodeTypeName, string $propertyName): ?QrCode
{
$urlShortener = $this->handleUrlShortener($node, $shortType, $nodeTypeName, $propertyName);

if (!$urlShortener instanceof UrlShortener) {
return null;
}

$qrCode = $this->qrCodeService->getQrCodeOfUrlShortener($urlShortener);
$this->qrCodeService->deleteQrCodeResource($qrCode);

$resource = $this->qrCodeService->createFileForUrlShortener($urlShortener);
$qrCode->setResource($resource);

$this->qrCodeRepository->update($qrCode);

return $qrCode;
}
}
Loading

0 comments on commit 09ffd79

Please sign in to comment.