Skip to content

Commit

Permalink
Abandon mcrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhj committed May 26, 2018
1 parent b60e792 commit 381473f
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 203 deletions.
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@
"source": "https://github.com/richardhj/contao-newsletter2go-sync"
},
"require": {
"php": "^5.6|^7.0",
"php": "^7.2",
"contao/core-bundle": "~4.4",
"contao/manager-plugin": "^2.2",
"codefog/contao-haste": "~4.12",
"doctrine/dbal": "^2.7",
"friends-of-contao/contao-associategroups": "~1.3",
"richardhj/newsletter2go-api": "~2.0"
"paragonie/halite": "^4.4",
"richardhj/newsletter2go-api": "~2.0",
"richardhj/oauth2-newsletter2go": "~2.0",
"symfony/config": "^3.3 || ^4.0",
"symfony/dependency-injection": "^3.3 || ^4.0"
},
"autoload": {
"psr-4": {
Expand Down
6 changes: 3 additions & 3 deletions src/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ abstract class AbstractHelper
*
* @return array
*/
public function getNewsletter2GoGroups()
public function getNewsletter2GoGroups(): array
{
$return = [];
try {
Expand Down Expand Up @@ -65,7 +65,7 @@ public function getNewsletter2GoGroups()
* @throws \LogicException
* @throws \InvalidArgumentException
*/
protected static function getListId()
protected static function getListId(): string
{
/** @var NewsletterList[] $lists */
$lists = NewsletterList::findAll(null, self::getApiCredentials());
Expand All @@ -78,7 +78,7 @@ protected static function getListId()
/**
* @return ApiCredentials|null
*/
protected static function getApiCredentials()
protected static function getApiCredentials(): ?ApiCredentials
{
/** @var BackendUser|\User $backendUser */
$backendUser = BackendUser::getInstance();
Expand Down
8 changes: 6 additions & 2 deletions src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ class Plugin implements BundlePluginInterface
*
* @return ConfigInterface[]
*/
public function getBundles(ParserInterface $parser)
public function getBundles(ParserInterface $parser): array
{
return [
BundleConfig::create(RichardhjContaoNewsletter2GoSyncBundle::class)
->setLoadAfter([ContaoCoreBundle::class])
->setLoadAfter(
[
ContaoCoreBundle::class,
]
)
->setReplace(['newsletter2go-sync']),
];
}
Expand Down
66 changes: 46 additions & 20 deletions src/Dca/Member.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

namespace Richardhj\Newsletter2Go\Contao\SyncBundle\Dca;

use Contao\Database;
use Contao\DataContainer;
use Contao\MemberModel;
use Contao\System;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Richardhj\Newsletter2Go\Api\Model\NewsletterRecipient;
use Richardhj\Newsletter2Go\Contao\SyncBundle\AbstractHelper;

Expand All @@ -28,6 +30,19 @@
class Member extends AbstractHelper
{

/**
* @var Connection
*/
private $connection;

/**
* Member constructor.
*/
public function __construct()
{
$this->connection = System::getContainer()->get('database_connection');
}

/**
* Remove a member from all groups after deleting
*
Expand All @@ -39,7 +54,7 @@ class Member extends AbstractHelper
* @throws \LogicException
* @throws \InvalidArgumentException
*/
public function deleteMember(DataContainer $dc)
public function deleteMember(DataContainer $dc): void
{
if (!$dc->id) {
return;
Expand All @@ -58,14 +73,16 @@ public function deleteMember(DataContainer $dc)
// Fetch id
$recipient->save();

$groups = Database::getInstance()
->prepare(
'SELECT mg.n2g_group_id FROM tl_member_group AS mg INNER JOIN tl_member_to_group mtg ON mg.id=mtg.group_id WHERE mtg.member_id=? AND mg.n2g_sync=1'
)
->execute($dc->id)
->fetchEach('n2g_group_id');
$statement = $this->connection->createQueryBuilder()
->select('mg.n2g_group_id')
->from('tl_member_group', 'mg')
->innerJoin('mg', 'tl_member_to_group', 'mtg', 'mg.id=mtg.group_id')
->where('mtg.member_id=:member_id')
->andWhere('mg.n2g_sync=1')
->setParameter('member_id', $dc->id)
->execute();

foreach ($groups as $group) {
foreach ($statement->fetchAll(FetchMode::COLUMN, 0) as $group) {
$recipient->removeFromGroup($group);
}
}
Expand Down Expand Up @@ -94,19 +111,25 @@ public function syncMemberGroupsWithNewsletter2Go($value, $dc)
}

$groupsNew = $groups ?
Database::getInstance()
->query(
'SELECT n2g_group_id FROM tl_member_group WHERE id IN('.implode(',', $groups).') AND n2g_sync=1'
)
->fetchEach('n2g_group_id')
$this->connection->createQueryBuilder()
->select('n2g_group_id')
->from('tl_member_group')
->where('id IN (:groups)')
->andWhere('n2g_sync=1')
->setParameter('groups', $groups, Connection::PARAM_STR_ARRAY)
->execute()
->fetchAll(FetchMode::COLUMN, 0)
: [];

$groupsOld = Database::getInstance()
->prepare(
'SELECT g.n2g_group_id FROM tl_member_to_group AS mtg INNER JOIN tl_member_group g ON g.id=mtg.group_id WHERE mtg.member_id=? AND g.n2g_sync=1'
)
->execute($dc->id)
->fetchEach('n2g_group_id');
$groupsOld = $this->connection->createQueryBuilder()
->select('g.n2g_group_id')
->from('tl_member_to_group', 'mtg')
->innerJoin('mtg', 'tl_member_group', 'g', 'g.id=mtg.group_id')
->where('mtg.member_id=:member')
->andWhere('n2g_sync=1')
->setParameter('member', $dc->id)
->execute()
->fetchAll(FetchMode::COLUMN, 0);

// Nothing to sync here
if (0 === \count($groupsNew) && 0 === \count($groupsOld)) {
Expand All @@ -115,6 +138,9 @@ public function syncMemberGroupsWithNewsletter2Go($value, $dc)

/** @type \Model $member */
$member = MemberModel::findByPk($dc->id);
if (null === $member) {
return $value;
}

# $member contains obsolete data (pre save)
# $dc->activeRecord contains current data
Expand Down
35 changes: 26 additions & 9 deletions src/Dca/MemberGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

namespace Richardhj\Newsletter2Go\Contao\SyncBundle\Dca;

use Contao\Database;
use Contao\DataContainer;
use Contao\MemberGroupModel;
use Contao\System;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\FetchMode;
use Richardhj\Newsletter2Go\Api\Model\NewsletterGroup;
use Richardhj\Newsletter2Go\Api\Model\NewsletterRecipient;
use Richardhj\Newsletter2Go\Contao\SyncBundle\AbstractHelper;
Expand All @@ -29,6 +31,19 @@
class MemberGroup extends AbstractHelper
{

/**
* @var Connection
*/
private $connection;

/**
* Member constructor.
*/
public function __construct()
{
$this->connection = System::getContainer()->get('database_connection');
}

/**
* Create a group on Newsletter2Go if sync for member group was enabled but no existing group was selected
*
Expand All @@ -40,7 +55,7 @@ class MemberGroup extends AbstractHelper
* @throws \RuntimeException
* @throws \LogicException
*/
public function createNewsletter2GoGroup(DataContainer $dc)
public function createNewsletter2GoGroup(DataContainer $dc): void
{
if (!$dc->id) {
return;
Expand Down Expand Up @@ -76,7 +91,7 @@ public function createNewsletter2GoGroup(DataContainer $dc)
* @throws \RuntimeException
* @throws \LogicException
*/
public function deleteMemberGroup(DataContainer $dc)
public function deleteMemberGroup(DataContainer $dc): void
{
if (!$dc->id || !$dc->activeRecord->n2g_sync || !$dc->activeRecord->n2g_group_id) {
return;
Expand All @@ -87,12 +102,14 @@ public function deleteMemberGroup(DataContainer $dc)
return;
}

$members = Database::getInstance()
->prepare(
'SELECT m.email FROM tl_member AS m INNER JOIN tl_member_to_group mg ON m.id=mg.member_id WHERE mg.group_id=?'
)
->execute($dc->id)
->fetchEach('email');
$members = $this->connection->createQueryBuilder()
->select('m.email')
->from('tl_member', 'm')
->innerJoin('m', 'tl_member_to_group', 'mg', 'm.id=mg.member_id')
->where('mg.group_id=:group')
->setParameter('group', $dc->id)
->execute()
->fetchAll(FetchMode::COLUMN, 0);

foreach ($members as $member) {
$recipient = new NewsletterRecipient();
Expand Down
3 changes: 1 addition & 2 deletions src/Dca/Newsletter2GoUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Newsletter2GoUser extends AbstractHelper
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
public function authenticateUser(DataContainer $dc)
public function authenticateUser(DataContainer $dc): string
{
if (!$dc->id) {
return '';
Expand Down Expand Up @@ -97,7 +97,6 @@ public function authenticateUser(DataContainer $dc)

$form = new Form(
'authenticate_n2g_user', 'POST', function ($haste) {
/** @noinspection PhpUndefinedMethodInspection */
return $haste->getFormId() === Input::post('FORM_SUBMIT');
}
);
Expand Down
68 changes: 0 additions & 68 deletions src/Dca/NewsletterChannel.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class RichardhjContaoNewsletter2GoSyncExtension extends Extension
*
* @throws \Exception
*/
public function load(array $configs, ContainerBuilder $container)
public function load(array $configs, ContainerBuilder $container): void
{
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
Expand Down
Loading

0 comments on commit 381473f

Please sign in to comment.