Skip to content

Commit

Permalink
Import namespace for class Clockwork + CS
Browse files Browse the repository at this point in the history
  • Loading branch information
richardhj committed Jun 10, 2018
1 parent c8c3d2b commit de6f5fc
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 34 deletions.
11 changes: 8 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
"require": {
"php": "^5.6 || ^7.0",
"contao/core-bundle": "~4.4",
"terminal42/notification_center": "^1.3.5",
"richardhj/contao-notification_center_checksendinterface": "^1.0",
"giggsey/libphonenumber-for-php": "^7.0 || ^8.0",
"mediaburst/clockworksms": "~2.0",
"giggsey/libphonenumber-for-php": "^7.0 || ^8.0"
"psr/log": "^1.0",
"richardhj/contao-notification_center_checksendinterface": "^1.0",
"symfony/http-kernel": "^3.3 || ^4.0",
"terminal42/notification_center": "^1.3.5"
},
"require-dev": {
"contao/manager-plugin": "^2.2"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 6 additions & 1 deletion src/ContaoManager/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create(RichardhjNotificationCenterClockworkSmsBundle::class)
->setLoadAfter([ContaoCoreBundle::class])
->setLoadAfter(
[
ContaoCoreBundle::class,
'notification_center'
]
)
->setReplace(['notification_center_clockworksms']),
];
}
Expand Down
65 changes: 46 additions & 19 deletions src/Gateway/ClockworkSms.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Richardhj\NotificationCenterClockworkSmsBundle\Gateway;

use Contao\CoreBundle\Monolog\ContaoContext;
use Contao\MemberModel;
use Contao\System;
use mediaburst\ClockworkSMS\Clockwork;
use mediaburst\ClockworkSMS\ClockworkException;
use NotificationCenter\Gateway\Base;
Expand All @@ -24,6 +26,8 @@
use NotificationCenter\Model\Gateway;
use NotificationCenter\Model\Language;
use NotificationCenter\Model\Message;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
use Richardhj\NotificationCenterClockworkSmsBundle\MessageDraft\ClockworkSmsMessageDraft;


Expand All @@ -43,6 +47,20 @@ class ClockworkSms extends Base
*/
protected $objModel;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param Gateway $model
*/
public function __construct(Gateway $model)
{
parent::__construct($model);

$this->logger = System::getContainer()->get('monolog.logger.contao');
}

/**
* Returns a MessageDraft
Expand All @@ -60,14 +78,14 @@ public function createDraft(Message $message, array $tokens, $language = '')
}

if (null === ($objLanguage = Language::findByMessageAndLanguageOrFallback($message, $language))) {
\System::log(
$this->logger->log(
LogLevel::ERROR,
sprintf(
'Could not find matching language or fallback for message ID "%s" and language "%s".',
$message->id,
$language
),
__METHOD__,
TL_ERROR
array('contao' => new ContaoContext(__METHOD__, TL_ERROR))
);

return null;
Expand All @@ -85,16 +103,17 @@ public function createDraft(Message $message, array $tokens, $language = '')
* @param string $language
*
* @return bool
*
* @throws ClockworkException If api key is not set
*/
public function send(Message $message, array $tokens, $language = '')
{
if ('' === $this->objModel->clockwork_api_key) {
\System::log(
$this->logger->log(
LogLevel::ERROR,
sprintf('Please provide the Clockwork API key for message ID "%s"', $message->id),
__METHOD__,
TL_ERROR
array('contao' => new ContaoContext(__METHOD__, TL_ERROR))
);

return false;
}

Expand All @@ -109,10 +128,11 @@ public function send(Message $message, array $tokens, $language = '')
$messages = [];

$clockwork = new Clockwork(
$this->objModel->clockwork_api_key, [
$this->objModel->clockwork_api_key,
[
'from' => $draft->getFrom(),
'long' => (bool)$message->long,
'truncate' => (bool)$message->truncate,
'long' => (bool) $message->long,
'truncate' => (bool) $message->truncate,
]
);

Expand All @@ -127,33 +147,33 @@ public function send(Message $message, array $tokens, $language = '')
$result = $clockwork->send($messages);

} catch (ClockworkException $e) {
\System::log(
$this->logger->log(
LogLevel::ERROR,
sprintf(
'Error with message "%s" (Code %s) while sending the Clockwork request for message ID "%s" occurred.',
$e->getMessage(),
$e->getCode(),
$message->id
),
__METHOD__,
TL_ERROR
array('contao' => new ContaoContext(__METHOD__, TL_ERROR))
);

return false;
}

$blnError = false;

foreach ((array)$result as $msg) {
foreach ((array) $result as $msg) {
if (!$msg['success']) {
\System::log(
$this->logger->log(
LogLevel::ERROR,
sprintf(
'Error with message "%s" (Code %s) while sending the Clockwork request for message ID "%s" occurred.',
$msg['error_message'],
$msg['error_code'],
$message->id
),
__METHOD__,
TL_ERROR
array('contao' => new ContaoContext(__METHOD__, TL_ERROR))
);

$blnError = true;
Expand All @@ -165,7 +185,14 @@ public function send(Message $message, array $tokens, $language = '')


/**
* {@inheritdoc}
* Check whether an exemplary draft can be send by means of a given message and gateway. In most cases this check
* looks for existing recipients
*
* @param Message $objMessage
*
* @return bool
*
* @throws \LogicException Optional with an error message
*/
public function canSendDraft(Message $message)
{
Expand All @@ -180,7 +207,7 @@ public function canSendDraft(Message $message)
(
array_map(
function ($key) {
return 'member_'.$key;
return 'member_' . $key;
},
array_keys($memberModel->row())
),
Expand Down
26 changes: 19 additions & 7 deletions src/MessageDraft/ClockworkSmsMessageDraft.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
namespace Richardhj\NotificationCenterClockworkSmsBundle\MessageDraft;

use Contao\Controller;
use Contao\CoreBundle\Monolog\ContaoContext;
use Contao\System;
use libphonenumber\NumberParseException;
use libphonenumber\PhoneNumberFormat;
use libphonenumber\PhoneNumberUtil;
use mediaburst\ClockworkSMS\Clockwork;
use NotificationCenter\MessageDraft\MessageDraftInterface;
use NotificationCenter\Model\Language;
use NotificationCenter\Model\Message;
use NotificationCenter\Util\StringUtil;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;


/**
Expand Down Expand Up @@ -52,6 +57,11 @@ class ClockworkSmsMessageDraft implements MessageDraftInterface
*/
protected $arrTokens = [];

/**
* @var LoggerInterface
*/
private $logger;

/**
* Construct the object
*
Expand All @@ -64,6 +74,8 @@ public function __construct(Message $message, Language $language, array $tokens)
$this->arrTokens = $tokens;
$this->objLanguage = $language;
$this->objMessage = $message;

$this->logger = System::getContainer()->get('monolog.logger.contao');
}

/**
Expand Down Expand Up @@ -101,15 +113,15 @@ public function getRecipients()
$strRecipient = $this->normalizePhoneNumber($strRecipient);

// Address could become empty through invalid insert tag
if (false === $strRecipient || !\Clockwork::is_valid_msisdn($strRecipient)) {
\System::log(
if (false === $strRecipient || !Clockwork::is_valid_msisdn($strRecipient)) {
$this->logger->log(
LogLevel::ERROR,
sprintf(
'Recipient "%s" for message ID %s was skipped as it was no valid MSISDN.',
$strRecipient,
$this->objMessage->id
),
__METHOD__,
TL_ERROR
array('contao' => new ContaoContext(__METHOD__, TL_ERROR))
);

continue;
Expand Down Expand Up @@ -185,14 +197,14 @@ protected function normalizePhoneNumber($phone)
return ltrim($phoneNumberUtil->format($phoneNumber, PhoneNumberFormat::E164), '+');

} catch (NumberParseException $e) {
\System::log(
$this->logger->log(
LogLevel::ERROR,
sprintf(
'Failed to normalize phone number "%s" with message "%s"',
$phone,
$e->getMessage()
),
__METHOD__,
TL_ERROR
array('contao' => new ContaoContext(__METHOD__, TL_ERROR))
);
}

Expand Down
11 changes: 7 additions & 4 deletions src/Util/ClockworkSmsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
namespace Richardhj\NotificationCenterClockworkSmsBundle\Util;

use Contao\Validator;
use mediaburst\ClockworkSMS\Clockwork;


/**
* Class ClockworkSmsHelper
*
* @package NotificationCenter\Util
*/
class ClockworkSmsHelper
Expand All @@ -30,6 +32,7 @@ class ClockworkSmsHelper
* @param mixed $varValue
*
* @return mixed
*
* @throws \RuntimeException
*/
public function validateSmsSender($varValue)
Expand All @@ -39,7 +42,7 @@ public function validateSmsSender($varValue)
return $varValue;
}

if ((!Validator::isAlphanumeric($varValue) && !\Clockwork::is_valid_msisdn($varValue))
if ((!Validator::isAlphanumeric($varValue) && !Clockwork::is_valid_msisdn($varValue))
|| (Validator::isAlphanumeric($varValue) && \strlen($varValue) > 11)
) {
throw new \RuntimeException($GLOBALS['TL_LANG']['ERR']['invalidClockworkSmsSender']);
Expand All @@ -57,15 +60,15 @@ public function validateSmsSender($varValue)
* @param mixed $varValue
*
* @return mixed
*
* @throws \RuntimeException
*
* @internal param \DataContainer $dc
*/
public function validatePhoneNumberList($varValue)
{
if ('' !== $varValue) {
$chunks = trimsplit(',', $varValue);

foreach ($chunks as $chunk) {
foreach (trimsplit(',', $varValue) as $chunk) {
// Skip string with tokens or inserttags
if (false !== strpos($chunk, '##') || false !== strpos($chunk, '{{')) {
continue;
Expand Down

0 comments on commit de6f5fc

Please sign in to comment.