Skip to content

Commit

Permalink
Refactor URL generation in display helper
Browse files Browse the repository at this point in the history
  • Loading branch information
ImanuelBertrand committed Sep 18, 2023
1 parent a68c062 commit 0a545db
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions Helper/Display.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class Display extends AbstractHelper
private UrlInterface $urlBuilder;

public function __construct(
Context $context,
UrlInterface $urlBuilder
Context $context,
UrlInterface $urlBuilder,
) {
parent::__construct($context);
$this->urlBuilder = $urlBuilder;
Expand All @@ -44,27 +44,54 @@ public function getCustomerNamesForListing(Order $order): string
);
}

/**
* @param DataObject $object
* @return string
*/
public function getUrl(DataObject $object): string
{
/** @noinspection PhpPossiblePolymorphicInvocationInspection */
$id = $object->getId();
if (empty($id)) {
return '';
}

$classMappings = [
Invoice::class => ['invoice_id', 'sales/invoice/view'],
Creditmemo::class => ['creditmemo_id', 'sales/creditmemo/view'],
Order::class => ['order_id', 'sales/order/view'],
Customer::class => ['id', 'customer/index/edit'],
];

foreach ($classMappings as $class => [$idName, $route]) {
if ($object instanceof $class) {
return $this->urlBuilder->getUrl($route, [$idName => $id]);
}
}

return '';
}


/**
* @param DataObject $object
* @param array $matchedTexts
* @return string
*/
public function getObjectLink(DataObject $object, array $matchedTexts): string
{
if ($object instanceof Invoice) {
$url = $this->urlBuilder->getUrl('sales/invoice/view', ['invoice_id' => $object->getId()]);
} elseif ($object instanceof Creditmemo) {
$url = $this->urlBuilder->getUrl('sales/creditmemo/view', ['creditmemo_id' => $object->getId()]);
} elseif ($object instanceof Order) {
$url = $this->urlBuilder->getUrl('sales/order/view', ['order_id' => $object->getId()]);
} elseif ($object instanceof Customer) {
$url = $this->urlBuilder->getUrl('customer/index/edit', ['id' => $object->getId()]);
} else {
$url = $this->getUrl($object);
if (empty($url)) {
return '';
}

/** @noinspection PhpPossiblePolymorphicInvocationInspection */
$incrementId = $object->getIncrementId();
$cssClass = in_array($incrementId, array_keys($matchedTexts)) ? 'banksync-matched-text' : '';
if ($cssClass == '' && str_ends_with($incrementId, '00')) {
$incrementId = substr($incrementId, 0, -2);
$cssClass = in_array($incrementId, array_keys($matchedTexts)) ? 'banksync-matched-text' : '';
if (empty($incrementId)) {
return '';
}

$cssClass = in_array($incrementId, array_keys($matchedTexts)) ? 'banksync-matched-text' : '';
return "<a class='$cssClass' href='$url'>$incrementId</a>";
}
}

0 comments on commit 0a545db

Please sign in to comment.