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.
- Loading branch information
Showing
8 changed files
with
221 additions
and
10 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
33 changes: 33 additions & 0 deletions
33
Classes/Psmb/Newsletter/Service/DataSource/AbstractDataSource.php
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
namespace Psmb\Newsletter\Service\DataSource; | ||
|
||
/** | ||
* Data source interface for getting data. | ||
* | ||
* @api | ||
*/ | ||
abstract class AbstractDataSource implements DataSourceInterface | ||
{ | ||
|
||
/** | ||
* The identifier of the operation | ||
* | ||
* @var string | ||
* @api | ||
*/ | ||
protected static $identifier = null; | ||
|
||
/** | ||
* @return string the short name of the operation | ||
* @api | ||
* @throws \Exception | ||
*/ | ||
public static function getIdentifier() | ||
{ | ||
if (!is_string(static::$identifier)) { | ||
throw new \Exception('Identifier in class ' . __CLASS__ . ' is empty.', 1414091236); | ||
} | ||
|
||
return static::$identifier; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Classes/Psmb/Newsletter/Service/DataSource/DataSourceInterface.php
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Psmb\Newsletter\Service\DataSource; | ||
|
||
/** | ||
* Data source interface for providing generic data | ||
* | ||
* This is used in the user interface to generate dynamic option lists. | ||
* | ||
* @api | ||
*/ | ||
interface DataSourceInterface | ||
{ | ||
/** | ||
* @return string The identifier of the data source | ||
* @api | ||
*/ | ||
public static function getIdentifier(); | ||
|
||
/** | ||
* Get data | ||
* | ||
* The return value must be JSON serializable data structure. | ||
* | ||
* @param array $subscription Subscription data | ||
* @return mixed data | ||
* @api | ||
*/ | ||
public function getData(array $subscription); | ||
} |
26 changes: 26 additions & 0 deletions
26
Classes/Psmb/Newsletter/Service/DataSource/JsonDataSource.php
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
namespace Psmb\Newsletter\Service\DataSource; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
|
||
class JsonDataSource extends AbstractDataSource | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected static $identifier = 'Json'; | ||
|
||
/** | ||
* Get subscribers from an external json endpoint | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function getData(array $subscription) | ||
{ | ||
if (!isset($subscription['dataSourceOptions']['uri'])) { | ||
throw new \Exception('dataSourceOptions.uri must be set for the Json datasource' . print_r($subscription, 1)); | ||
} | ||
$response = file_get_contents($subscription['dataSourceOptions']['uri']); | ||
return json_decode($response, true); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
Classes/Psmb/Newsletter/Service/DataSource/RepositoryDataSource.php
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
namespace Psmb\Newsletter\Service\DataSource; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Psmb\Newsletter\Domain\Repository\SubscriberRepository; | ||
|
||
class RepositoryDataSource extends AbstractDataSource | ||
{ | ||
/** | ||
* @Flow\Inject | ||
* @var SubscriberRepository | ||
*/ | ||
protected $subscriberRepository; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected static $identifier = 'Repository'; | ||
|
||
/** | ||
* Get subscribers from the repository | ||
* | ||
* {@inheritdoc} | ||
*/ | ||
public function getData(array $subscription) | ||
{ | ||
$subscribers = $this->subscriberRepository->findBySubscriptionId($subscription['identifier'])->toArray(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
namespace Psmb\Newsletter\Service; | ||
|
||
use Neos\Flow\Annotations as Flow; | ||
use Neos\Flow\ObjectManagement\ObjectManagerInterface; | ||
use Neos\Flow\Reflection\ReflectionService; | ||
use Psmb\Newsletter\Service\DataSource\DataSourceInterface; | ||
|
||
/** | ||
* @Flow\Scope("singleton") | ||
*/ | ||
class SubscribersService { | ||
|
||
/** | ||
* @Flow\Inject | ||
* @var ObjectManagerInterface | ||
*/ | ||
protected $objectManager; | ||
|
||
/** | ||
* Get subscribers data | ||
* | ||
* @param array $subscription | ||
* @return array Subscribers data | ||
*/ | ||
public function getSubscribers($subscription) | ||
{ | ||
$dataSources = static::getDataSources($this->objectManager); | ||
// throw new \Exception(print_r($dataSources, 1)); | ||
$dataSourceIdentifier = $subscription['dataSourceIdentifier'] ?? 'Repository'; | ||
|
||
if (!isset($dataSources[$dataSourceIdentifier])) { | ||
throw new \Exception(sprintf('Data source with identifier "%s" does not exist.', $dataSourceIdentifier), 1414082186); | ||
} | ||
|
||
/** @var $dataSource DataSourceInterface */ | ||
$dataSource = new $dataSources[$dataSourceIdentifier]; | ||
|
||
return $dataSource->getData($subscription); | ||
} | ||
|
||
/** | ||
* Get available data source implementations | ||
* | ||
* @param ObjectManagerInterface $objectManager | ||
* @return array Data source class names indexed by identifier | ||
* @Flow\CompileStatic | ||
*/ | ||
public static function getDataSources($objectManager) | ||
{ | ||
$reflectionService = $objectManager->get(ReflectionService::class); | ||
|
||
$dataSources = array(); | ||
$dataSourceClassNames = $reflectionService->getAllImplementationClassNamesForInterface(DataSourceInterface::class); | ||
/** @var $dataSourceClassName DataSourceInterface */ | ||
foreach ($dataSourceClassNames as $dataSourceClassName) { | ||
$identifier = $dataSourceClassName::getIdentifier(); | ||
if (isset($dataSources[$identifier])) { | ||
throw new \Exception(sprintf('Data source with identifier "%s" is already defined in class %s.', $identifier, $dataSourceClassName), 14140348185); | ||
} | ||
$dataSources[$identifier] = $dataSourceClassName; | ||
} | ||
|
||
return $dataSources; | ||
} | ||
} |
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