Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Config/config.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
<?php

use MauticPlugin\MauticRssToEmailBundle\EventListener\EmailSubscriber;

return [
'name' => 'RSS To Email',
Expand All @@ -8,7 +10,7 @@
'services' => [
'events' => [
'mautic.plugin.rsstoemail.subscriber' => [
'class' => \MauticPlugin\MauticRssToEmailBundle\EventListener\EmailSubscriber::class,
'class' => EmailSubscriber::class,
],
],
],
Expand Down
1 change: 0 additions & 1 deletion Config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
return function (ContainerConfigurator $configurator): void {
$services = $configurator->services()
->defaults()
// ->autowire()
->autoconfigure()
->public();

Expand Down
5 changes: 2 additions & 3 deletions EventListener/EmailSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
class EmailSubscriber implements EventSubscriberInterface
{

/**
* @return array
*/
/** @return array<string, array{0: "onEmailGenerate", 1: int}> */
public static function getSubscribedEvents()
{
return [
Expand All @@ -27,6 +25,7 @@ public static function getSubscribedEvents()
* Search and replace tokens with content
*
* @param EmailSendEvent $event
* @return void
*/
public function onEmailGenerate(EmailSendEvent $event)
{
Expand Down
5 changes: 4 additions & 1 deletion Feed/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

class Feed
{
/**
* @param string $url
* @return \SimplePie|null
*/
public static function fetch(string $url)
{
if (FeedCache::exists($url)) {
Expand All @@ -22,5 +26,4 @@ public static function fetch(string $url)

return $feed;
}

}
44 changes: 28 additions & 16 deletions Feed/FeedCache.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,58 @@
<?php

namespace MauticPlugin\MauticRssToEmailBundle\Feed;

class FeedCache
{
private static $instance = null;

/** @var self */
private static $instance;
/** @var array<string, \SimplePie> */
protected $feeds = [];

/** @return self */
public static function getInstance()
{
if (self::$instance == null)
{
self::$instance = new self();
}

return self::$instance;
if (self::$instance === null) {
self::$instance = new self();
}

return self::$instance;
}

/** @return array<string, \SimplePie> */
public function getFeeds()
{
return $this->feeds;
}

/**
* @param string $url
* @param \SimplePie $feed
* @return void
*/
public static function push(string $url, $feed)
{
if (!self::getInstance()->exists($url)) {
if (!self::getInstance()::exists($url)) {
self::getInstance()->feeds[$url] = $feed;
}
}

public static function exists($url):bool
/**
* @param string $url
* @return bool
*/
public static function exists($url): bool
{
if (isset(self::getInstance()->getFeeds()[$url])) {
return true;
}

return false;
return isset(self::getInstance()->getFeeds()[$url]);
}

/**
* @param string $url
* @return \SimplePie|null
*/
public static function get($url)
{
if (self::getInstance()->exists($url)) {
if (self::getInstance()::exists($url)) {
return self::getInstance()->getFeeds()[$url];
}

Expand Down
9 changes: 5 additions & 4 deletions Helpers/ParamsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@

class ParamsHelper
{
/**
* @param string $paramsString
* @return array<string, string>
*/
public static function parse($paramsString)
{
$params = [];
preg_match_all('/ ?([^=]+)="([^"]+)"/', $paramsString, $paramMatches);

if (!empty($paramMatches[1])) {
foreach ($paramMatches[1] as $index => $paramKey) {
$key = $paramKey;
$value = $paramMatches[2][$index];

$params[$key] = $value;
$params[$paramKey] = $paramMatches[2][$index];
}
}

Expand Down
2 changes: 1 addition & 1 deletion MauticRssToEmailBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ class MauticRssToEmailBundle extends PluginBundleBase
{
}

include(dirname(__FILE__) . '/vendor/autoload.php');
include(__DIR__ . '/vendor/autoload.php');
12 changes: 12 additions & 0 deletions Parser/FeedParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ class FeedParser
{
use ParamsTrait;

/** @var \SimplePie */
public $feed;
/** @var string */
protected $content;

/**
* @param string $content
* @param \SimplePie $feed
*/
public function __construct($content, $feed)
{
$this->feed = $feed;
Expand All @@ -22,6 +28,10 @@ public function __construct($content, $feed)
$this->setContent($content);
}

/**
* @param string $content
* @return string
*/
public function parseInfo($content)
{
preg_match_all('/{feedinfo:([^}]+)}/', $content, $tags);
Expand Down Expand Up @@ -49,11 +59,13 @@ public function parseInfo($content)
return $content;
}

/** @param string $content */
public function setContent($content)
{
$this->content = $content;
}

/** @return string */
public function getContent()
{
return $this->content;
Expand Down
84 changes: 28 additions & 56 deletions Parser/ItemTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,104 +7,76 @@ class ItemTag
{
use ParamsTrait;

/** @var string */
public $tag;
/** @var \SimplePie_Item */
public $feedItem;

/**
* @param string $tag
* @param array<string, string> $params
* @param \SimplePie_Item $feedItem
*/
public function __construct($tag, $params, $feedItem)
{
$this->tag = $tag;
$this->setParams($params);

$this->feedItem = $feedItem;
}

/** @return string|null */
public function getValue()
{
$feedItem = $this->feedItem;

$value = "Unknown ({$this->tag})";

switch ($this->tag) {
case 'title':
$value = $feedItem->get_title();
break;
return $feedItem->get_title();
case 'link':
$value = $feedItem->get_link();
break;
return $feedItem->get_link();
case 'content':
$value = $feedItem->get_description();
break;
case 'description':
return $feedItem->get_description();
case 'content_full':
$value = $feedItem->get_content(true);
break;
return $feedItem->get_content(true);
case 'content_text':
$value = strip_tags($feedItem->get_description());
break;
return strip_tags($feedItem->get_description());
case 'content_full_text':
$value = strip_tags($feedItem->get_content(true));
break;
case 'description':
$value = $feedItem->get_description();
break;
return strip_tags($feedItem->get_content(true));
case 'date':
$format = 'j F Y, g:i a';
if (!empty($this->getParam('format'))) {
$format = $this->getParam('format');
}

$value = $feedItem->get_date($format);
break;
return $feedItem->get_date($this->getParam('format') ?: 'j F Y, g:i a');
case 'author':
$author = $feedItem->get_author();

if (!is_null($author)) {
$value = $author->get_link();
}

$value = $feedItem->get_author()->get_name();
break;
return ($author) ? $author->get_name() : null;
case 'categories':
$categories = $feedItem->get_categories();
$categoriesList = [];

if (!empty($categories)) foreach ($categories as $category)
{
$categoriesList[] = $category->get_label();
if ($categories) {
foreach ($categories as $category) {
$categoriesList[] = $category->get_label();
}
}

$value = implode(', ', $categoriesList);
break;
return implode(', ', $categoriesList);
case 'image':
$enclosure = $feedItem->get_enclosure();
$value = '';

if (!is_null($enclosure)) {
$value = $enclosure->get_link();
}
break;
return ($enclosure) ? $enclosure->get_link() : '';
case 'custom':
$key = null;

if (!empty($this->getParam('subTag'))) {
$key = $this->getParam('subTag');
} elseif (!empty($this->getParam('key'))) {
$key = $this->getParam('key');
}

$key = $this->getParam('subTag') ?: $this->getParam('key') ?: null;
$itemTag = $feedItem->get_item_tags('https://www.mautic.org/rss/mautic/', $key);

$value = $itemTag[0]['data'] ?? null;

break;
return $itemTag[0]['data'] ?? null;
default:
$itemTag = $feedItem->get_item_tags('', $this->tag);

if(!empty($itemTag)) {
$value = $itemTag[0]['data'];
if ($itemTag) {
return $itemTag[0]['data'];
}
break;
}

return $value;
return "Unknown ({$this->tag})";
}
}
Loading