2022-06-20 14:47:33 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This Software is the property of Data Development and is protected
|
|
|
|
* by copyright law - it is NOT Freeware.
|
|
|
|
* Any unauthorized use of this software without a valid license
|
|
|
|
* is a violation of the license agreement and will be prosecuted by
|
|
|
|
* civil and criminal law.
|
|
|
|
* http://www.shopmodule.com
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
|
|
|
* @link http://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace D3\LinkmobilityClient\RecipientsList;
|
|
|
|
|
2022-07-01 09:24:16 +02:00
|
|
|
use D3\LinkmobilityClient\Client;
|
2022-06-24 16:00:56 +02:00
|
|
|
use D3\LinkmobilityClient\Exceptions\ExceptionMessages;
|
|
|
|
use D3\LinkmobilityClient\Exceptions\RecipientException;
|
2022-06-20 14:47:33 +02:00
|
|
|
use D3\LinkmobilityClient\ValueObject\Recipient;
|
2022-06-24 16:00:56 +02:00
|
|
|
use Iterator;
|
|
|
|
use libphonenumber\NumberParseException;
|
2022-06-24 14:35:17 +02:00
|
|
|
use libphonenumber\PhoneNumberType;
|
2022-06-24 16:00:56 +02:00
|
|
|
use libphonenumber\PhoneNumberUtil;
|
2022-06-20 14:47:33 +02:00
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
class RecipientsList implements RecipientsListInterface, Iterator
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
2022-07-01 09:24:16 +02:00
|
|
|
/**
|
|
|
|
* @var Client
|
|
|
|
*/
|
|
|
|
private $client;
|
|
|
|
|
2022-06-20 14:47:33 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $recipients = [];
|
|
|
|
|
2022-07-01 09:24:16 +02:00
|
|
|
public function __construct( Client $client )
|
|
|
|
{
|
|
|
|
$this->setClient($client);
|
|
|
|
}
|
|
|
|
|
2022-06-24 14:35:17 +02:00
|
|
|
public function add(Recipient $recipient) : RecipientsListInterface
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
2022-06-24 16:00:56 +02:00
|
|
|
$phoneUtil = PhoneNumberUtil::getInstance();
|
2022-06-24 14:35:17 +02:00
|
|
|
try {
|
2022-06-24 16:00:56 +02:00
|
|
|
$phoneNumber = $phoneUtil->parse( $recipient->get(), $recipient->getCountryCode() );
|
|
|
|
|
|
|
|
if ( false === $phoneUtil->isValidNumber( $phoneNumber ) ) {
|
|
|
|
throw new RecipientException( ExceptionMessages::INVALID_RECIPIENT_PHONE );
|
|
|
|
} elseif (
|
|
|
|
false === in_array(
|
|
|
|
$phoneUtil->getNumberType( $phoneNumber ),
|
|
|
|
[
|
|
|
|
PhoneNumberType::MOBILE,
|
|
|
|
PhoneNumberType::FIXED_LINE_OR_MOBILE
|
|
|
|
]
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
throw new RecipientException( ExceptionMessages::NOT_A_MOBILE_NUMBER );
|
2022-06-24 14:35:17 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
$this->recipients[ md5( serialize( $recipient ) ) ] = $recipient;
|
|
|
|
} catch (NumberParseException $e) {
|
2022-07-01 09:24:16 +02:00
|
|
|
if ($this->getClient()->hasLogger()) {
|
|
|
|
$this->getClient()->getLogger()->info($e->getMessage());
|
|
|
|
}
|
2022-06-24 16:00:56 +02:00
|
|
|
} catch (RecipientException $e) {
|
2022-07-01 09:24:16 +02:00
|
|
|
if ($this->getClient()->hasLogger()) {
|
|
|
|
$this->getClient()->getLogger()->info($e->getMessage());
|
|
|
|
}
|
2022-06-24 14:35:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|
|
|
|
|
2022-06-24 14:35:17 +02:00
|
|
|
public function clearRecipents() : RecipientsListInterface
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
|
|
|
$this->recipients = [];
|
2022-06-24 14:35:17 +02:00
|
|
|
|
|
|
|
return $this;
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRecipients() : array
|
|
|
|
{
|
|
|
|
return array_values(
|
|
|
|
array_map(
|
|
|
|
function (Recipient $recipient) {
|
|
|
|
return $recipient->get();
|
|
|
|
},
|
|
|
|
$this->recipients
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
2022-06-20 14:47:33 +02:00
|
|
|
public function getRecipientsList() : array
|
|
|
|
{
|
|
|
|
return $this->recipients;
|
|
|
|
}
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
/**
|
|
|
|
* @return false|mixed
|
|
|
|
*/
|
2022-06-20 14:47:33 +02:00
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
return current($this->recipients);
|
|
|
|
}
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
/**
|
|
|
|
* @return false|mixed|void
|
|
|
|
*/
|
2022-06-20 14:47:33 +02:00
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
return next($this->recipients);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return key($this->recipients);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
return reset($this->recipients);
|
|
|
|
}
|
|
|
|
|
2022-06-24 16:00:56 +02:00
|
|
|
public function valid(): bool
|
2022-06-20 14:47:33 +02:00
|
|
|
{
|
2022-06-24 16:00:56 +02:00
|
|
|
return current($this->recipients) instanceof Recipient;
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|
2022-07-01 09:24:16 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Client
|
|
|
|
*/
|
|
|
|
public function getClient(): Client
|
|
|
|
{
|
|
|
|
return $this->client;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Client $client
|
|
|
|
*/
|
|
|
|
public function setClient( Client $client )
|
|
|
|
{
|
|
|
|
$this->client = $client;
|
|
|
|
}
|
2022-06-20 14:47:33 +02:00
|
|
|
}
|