Skip to content

Commit

Permalink
refactor: Cleanup Tellafriend controller
Browse files Browse the repository at this point in the history
  • Loading branch information
aragon999 committed Aug 6, 2023
1 parent 277d1ec commit 4bd7c9b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 83 deletions.
25 changes: 0 additions & 25 deletions .phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24645,31 +24645,6 @@ parameters:
count: 1
path: engine/Shopware/Controllers/Frontend/Sitemap.php

-
message: "#^Access to an undefined property Enlight_Controller_Request_RequestHttp\\:\\:\\$sArticle\\.$#"
count: 1
path: engine/Shopware/Controllers/Frontend/Tellafriend.php

-
message: "#^Method Shopware_Controllers_Frontend_Tellafriend\\:\\:indexAction\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Frontend/Tellafriend.php

-
message: "#^Method Shopware_Controllers_Frontend_Tellafriend\\:\\:init\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Frontend/Tellafriend.php

-
message: "#^Method Shopware_Controllers_Frontend_Tellafriend\\:\\:successAction\\(\\) has no return type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Frontend/Tellafriend.php

-
message: "#^Property Shopware_Controllers_Frontend_Tellafriend\\:\\:\\$sSYSTEM has no type specified\\.$#"
count: 1
path: engine/Shopware/Controllers/Frontend/Tellafriend.php

-
message: "#^Property Shopware_Controllers_Frontend_Tracking\\:\\:\\$testRepository has no type specified\\.$#"
count: 1
Expand Down
99 changes: 41 additions & 58 deletions engine/Shopware/Controllers/Frontend/Tellafriend.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,6 @@

class Shopware_Controllers_Frontend_Tellafriend extends Enlight_Controller_Action
{
public $sSYSTEM;

public function init()
{
$this->sSYSTEM = Shopware()->System();
}

public function preDispatch()
{
$config = $this->container->get(\Shopware_Components_Config::class);
Expand All @@ -43,95 +36,85 @@ public function preDispatch()
}
}

/**
* @return void
*/
public function successAction()
{
$this->View()->loadTemplate('frontend/tellafriend/index.tpl');
$this->View()->assign('sSuccess', true);
}

/**
* @return void
*/
public function indexAction()
{
if (empty($this->Request()->sDetails)) {
$id = $this->Request()->sArticle;
} else {
$id = $this->Request()->sDetails;
}
$productId = (int) ($this->Request()->get('sDetails') ?? $this->Request()->get('sArticle'));
if ($productId === 0) {
$this->forward('index', 'index');

if (empty($id)) {
return $this->forward('index', 'index');
return;
}

// Get Product-Information
$product = Shopware()->Modules()->Articles()->sGetPromotionById('fix', 0, (int) $id);
if (empty($product['articleName'])) {
return $this->forward('index', 'index');
$product = Shopware()->Modules()->Articles()->sGetPromotionById('fix', 0, $productId);
if (!isset($product['articleName']) || !isset($product['linkDetails'])
|| !\is_string($product['articleName']) || !\is_string($product['linkDetails'])
|| $product['articleName'] === '' || $product['linkDetails'] === '') {
$this->forward('index', 'index');

return;
}

if ($this->Request()->getPost('sMailTo')) {
$variables['sError'] = false;
if (!$this->Request()->getPost('sName')) {
$variables['sError'] = true;
}
if (!$this->Request()->getPost('sMail')) {
$variables['sError'] = true;
}
if (!$this->Request()->getPost('sRecipient')) {
$variables['sError'] = true;
}
$fromName = $this->Request()->getPost('sName');
$fromMail = $this->Request()->getPost('sMail');
$recipient = $this->Request()->getPost('sRecipient');
$comment = $this->Request()->getPost('sComment', '');

if (preg_match('/;/', $this->Request()->getPost('sRecipient')) || \strlen($this->Request()->getPost('sRecipient')) >= 50) {
$variables['sError'] = true;
}
$emailValidator = $this->container->get(EmailValidator::class);

$validator = $this->container->get(EmailValidator::class);
if (!$validator->isValid($this->Request()->getPost('sRecipient'))) {
$variables['sError'] = true;
}
$validInputParameters = \is_string($fromName) && \is_string($fromMail)
&& \is_string($recipient) && \is_string($comment)
&& !preg_match('/;/', $recipient) && \strlen($recipient) < 50
&& $emailValidator->isValid($fromMail) && $emailValidator->isValid($recipient)
;

if (!empty(Shopware()->Config()->get('CaptchaColor'))) {
if ($validInputParameters && !empty(Shopware()->Config()->get('CaptchaColor'))) {
/** @var \Shopware\Components\Captcha\CaptchaValidator $captchaValidator */
$captchaValidator = $this->container->get('shopware.captcha.validator');

if (!$captchaValidator->validate($this->Request())) {
$variables['sError'] = true;
}
$validInputParameters = $captchaValidator->validate($this->Request());
}

if ($variables['sError'] == false) {
// Prepare eMail
$product['linkDetails'] = $this->Front()->ensureRouter()->assemble(['sViewport' => 'detail', 'sArticle' => $product['articleID']]);

if ($validInputParameters) {
$context = [
'sName' => $this->sSYSTEM->_POST['sName'],
'sArticle' => html_entity_decode($product['articleName']),
'sName' => strip_tags($fromName),
'sArticle' => strip_tags($product['articleName']),
'sLink' => $product['linkDetails'],
'sComment' => strip_tags($comment),
];

if ($this->sSYSTEM->_POST['sComment']) {
$context['sComment'] = strip_tags(html_entity_decode($this->sSYSTEM->_POST['sComment']));
} else {
$context['sComment'] = '';
}

$mail = Shopware()->TemplateMail()->createMail('sTELLAFRIEND', $context, null, [
'fromMail' => $this->sSYSTEM->_POST['sMail'],
'fromName' => $this->sSYSTEM->_POST['sName'],
'fromMail' => $fromMail,
'fromName' => $fromName,
]);

$mail->addTo($this->sSYSTEM->_POST['sRecipient']);
$mail->addTo($recipient);
$mail->send();

$this->View()->assign('sSuccess', true);
$url = $this->Front()->ensureRouter()->assemble(['controller' => 'tellafriend', 'action' => 'success']);
$this->redirect($url);
} else {
$this->View()->assign('sError', true);
$this->View()->assign('sName', $this->Request()->getPost('sName'));
$this->View()->assign('sMail', $this->Request()->getPost('sMail'));
$this->View()->assign('sRecipient', $this->Request()->getPost('sRecipient'));
$this->View()->assign('sComment', $this->Request()->getPost('sComment'));
$this->View()->assign('sName', (string) $fromName);
$this->View()->assign('sMail', (string) $fromMail);
$this->View()->assign('sRecipient', (string) $recipient);
$this->View()->assign('sComment', (string) $comment);
}
}

$this->View()->assign('rand', Random::getAlphanumericString(32));
$this->View()->assign('sArticle', $product);
}
Expand Down

0 comments on commit 4bd7c9b

Please sign in to comment.