2019-02-06 11:34:55 +00:00
< ? php
/*
* This file is part of the Symfony package .
*
* ( c ) Fabien Potencier < fabien @ symfony . com >
*
* For the full copyright and license information , please view the LICENSE
* file that was distributed with this source code .
*/
namespace Symfony\Component\Mailer ;
use Symfony\Component\Mailer\Exception\InvalidArgumentException ;
2019-09-04 07:15:06 +01:00
use Symfony\Component\Mailer\Exception\LogicException ;
2019-02-06 11:34:55 +00:00
use Symfony\Component\Mime\Address ;
use Symfony\Component\Mime\RawMessage ;
/**
* @ author Fabien Potencier < fabien @ symfony . com >
*/
class SmtpEnvelope
{
private $sender ;
private $recipients = [];
/**
* @ param Address [] $recipients
*/
public function __construct ( Address $sender , array $recipients )
{
$this -> setSender ( $sender );
$this -> setRecipients ( $recipients );
}
public static function create ( RawMessage $message ) : self
{
2019-09-04 07:15:06 +01:00
if ( RawMessage :: class === \get_class ( $message )) {
throw new LogicException ( 'Cannot send a RawMessage instance without an explicit Envelope.' );
}
2019-05-31 14:32:37 +01:00
return new DelayedSmtpEnvelope ( $message );
2019-02-06 11:34:55 +00:00
}
public function setSender ( Address $sender ) : void
{
2019-06-12 05:53:11 +01:00
$this -> sender = new Address ( $sender -> getAddress ());
2019-02-06 11:34:55 +00:00
}
public function getSender () : Address
{
return $this -> sender ;
}
2019-06-12 05:53:11 +01:00
/**
* @ param Address [] $recipients
*/
2019-02-06 11:34:55 +00:00
public function setRecipients ( array $recipients ) : void
{
if ( ! $recipients ) {
throw new InvalidArgumentException ( 'An envelope must have at least one recipient.' );
}
$this -> recipients = [];
foreach ( $recipients as $recipient ) {
2019-06-12 05:53:11 +01:00
if ( ! $recipient instanceof Address ) {
2019-02-06 11:34:55 +00:00
throw new InvalidArgumentException ( sprintf ( 'A recipient must be an instance of "%s" (got "%s").' , Address :: class , \is_object ( $recipient ) ? \get_class ( $recipient ) : \gettype ( $recipient )));
}
2019-06-12 05:53:11 +01:00
$this -> recipients [] = new Address ( $recipient -> getAddress ());
2019-02-06 11:34:55 +00:00
}
}
/**
* @ return Address []
*/
public function getRecipients () : array
{
return $this -> recipients ;
}
}