forked from psmb/Psmb.Newsletter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TASK: refactor sending and generation code to be async
- Loading branch information
Showing
7 changed files
with
123 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,11 @@ | ||
<?php | ||
namespace Psmb\Newsletter\Command; | ||
|
||
use TYPO3\Flow\Annotations as Flow; | ||
use Psmb\Newsletter\Domain\Model\Subscriber; | ||
use Psmb\Newsletter\Domain\Repository\SubscriberRepository; | ||
use Psmb\Newsletter\Service\FusionMailService; | ||
use TYPO3\Flow\Annotations as Flow; | ||
use TYPO3\Flow\Cli\CommandController; | ||
use TYPO3\Flow\Http\Request; | ||
use TYPO3\Flow\Http\Response; | ||
use TYPO3\Flow\Http\Uri; | ||
use TYPO3\Flow\Mvc\ActionRequest; | ||
use TYPO3\Flow\Mvc\Routing\UriBuilder; | ||
use TYPO3\Flow\Mvc\Controller\Arguments; | ||
use TYPO3\Flow\Mvc\Controller\ControllerContext; | ||
use TYPO3\Flow\Persistence\PersistenceManagerInterface; | ||
|
||
/** | ||
* @Flow\Scope("singleton") | ||
|
@@ -44,22 +36,12 @@ class NewsletterCommandController extends CommandController | |
*/ | ||
protected $subscriptions; | ||
|
||
/** | ||
* We can't do this in constructor as we need configuration to be injected | ||
*/ | ||
public function initializeObject() { | ||
$request = $this->createRequest(); | ||
$request->setFormat('html'); | ||
$controllerContext = $this->createControllerContext($request); | ||
$this->fusionMailService->setupObject($controllerContext, $request); | ||
} | ||
|
||
/** | ||
* Import newsletter subscribers from CSV. | ||
* CSV should be in the format `"[email protected]","User Name","subscriptionId1|sibscriptionId2"` | ||
* CSV should be in the format `"[email protected]","User Name","subscriptionId1|subscriptionId2"` | ||
* | ||
* @param string $filename | ||
* @return string | ||
* @return void | ||
*/ | ||
public function importCsvCommand($filename) | ||
{ | ||
|
@@ -96,7 +78,7 @@ public function importCsvCommand($filename) | |
* @param string $subscription Subscription id to send newsletter to | ||
* @param string $interval Alternatively select all subscriptions with the given interval (useful for cron jobs) | ||
* @param bool $dryRun DryRun: generate messages but don't send | ||
* @return string | ||
* @return void | ||
*/ | ||
public function sendCommand($subscription = null, $interval = null, $dryRun = null) | ||
{ | ||
|
@@ -115,72 +97,34 @@ public function sendCommand($subscription = null, $interval = null, $dryRun = nu | |
$this->sendAndExit(1); | ||
} | ||
|
||
$nestedLetters = array_map([$this, 'generateLettersForSubscription'], $subscriptions); | ||
$letters = array_reduce($nestedLetters, function ($acc, $item) { | ||
return array_merge($acc, $item); | ||
}, []); | ||
|
||
if ($dryRun) { | ||
$this->outputLine(print_r($letters, 1)); | ||
} else { | ||
array_map(function($letter) { | ||
$this->fusionMailService->sendLetter($letter); | ||
}, $letters); | ||
} | ||
array_walk($subscriptions, function ($subscription) use ($dryRun) { | ||
$this->sendLettersForSubscription($subscription, $dryRun); | ||
}); | ||
} | ||
|
||
/** | ||
* Generate a letter for each subscriber in the subscription | ||
* | ||
* @param array $subscription | ||
* @return array Array of letters | ||
* @param bool $dryRun | ||
* @return void | ||
*/ | ||
protected function generateLettersForSubscription($subscription) | ||
protected function sendLettersForSubscription($subscription, $dryRun) | ||
{ | ||
$subscribers = $this->subscriberRepository->findBySubscriptionId($subscription['identifier'])->toArray(); | ||
|
||
$this->outputLine('Sending letters for subscription %s (%s subscribers)', [$subscription['identifier'], count($subscribers)]); | ||
$this->outputLine('-------------------------------------------------------------------------------'); | ||
$letters = array_map(function ($subscriber) use ($subscription) { | ||
|
||
array_walk($subscribers, function ($subscriber) use ($subscription, $dryRun) { | ||
$this->outputLine('Sending a letter for %s', [$subscriber->getEmail()]); | ||
$letter = $this->fusionMailService->generateSubscriptionLetter($subscriber, $subscription); | ||
if (!$letter) { | ||
$this->outputLine('<error>Nothing to send</error>'); | ||
if ($dryRun) { | ||
$letter = $this->fusionMailService->generateSubscriptionLetter($subscriber, $subscription); | ||
$this->outputLine(print_r($letter, true)); | ||
} else { | ||
$this->fusionMailService->generateSubscriptionLetterAndSend($subscriber, $subscription); | ||
} | ||
return $letter; | ||
}, $subscribers); | ||
return array_filter($letters); | ||
}); | ||
} | ||
|
||
/** | ||
* @return ActionRequest | ||
*/ | ||
protected function createRequest() { | ||
$_SERVER['FLOW_REWRITEURLS'] = 1; | ||
$httpRequest = Request::createFromEnvironment(); | ||
if ($this->baseUri) { | ||
$baseUri = new Uri($this->baseUri); | ||
$httpRequest->setBaseUri($baseUri); | ||
} | ||
return new ActionRequest($httpRequest); | ||
} | ||
|
||
/** | ||
* Creates a controller content context for live dimension | ||
* | ||
* @param ActionRequest $request | ||
* @return ControllerContext | ||
*/ | ||
protected function createControllerContext($request) | ||
{ | ||
$uriBuilder = new UriBuilder(); | ||
$uriBuilder->setRequest($request); | ||
$controllerContext = new ControllerContext( | ||
$request, | ||
new Response(), | ||
new Arguments([]), | ||
$uriBuilder | ||
); | ||
return $controllerContext; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.