Skip to content

Commit

Permalink
Simplify Mailer class.
Browse files Browse the repository at this point in the history
Properties have been removed and __call() is used to forward method
calls to Email class. This avoid ambiguity whether values set for Mailer
properties should take precedence or those set directly for Email class.
  • Loading branch information
ADmad committed Aug 26, 2015
1 parent fa47273 commit 26f6521
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 122 deletions.
136 changes: 16 additions & 120 deletions src/Mailer/Mailer.php
Expand Up @@ -12,7 +12,6 @@
*/
namespace Cake\Mailer;

use ArrayAccess;
use Cake\Datasource\ModelAwareTrait;
use Cake\Event\EventListenerInterface;
use Cake\Mailer\Exception\MissingActionException;
Expand Down Expand Up @@ -80,7 +79,7 @@
* Our mailer could either be registered in the application bootstrap, or
* in the Table class' initialize() hook.
*/
abstract class Mailer implements ArrayAccess, EventListenerInterface
abstract class Mailer implements EventListenerInterface
{

use ModelAwareTrait;
Expand All @@ -92,21 +91,6 @@ abstract class Mailer implements ArrayAccess, EventListenerInterface
*/
static public $name;

/**
* Layout.
*
* @var string
*/
public $layout;

/**
* Email view template to render, defaults to the triggered mailer
* action's name.
*
* @var string
*/
public $template;

/**
* Email instance.
*
Expand Down Expand Up @@ -146,52 +130,32 @@ public function getName()
}

/**
* Sets layout to use. Defaults to configured layout template if a custom layout
* could not be found.
* Sets layout to use.
*
* @param string $layout Name of the layout to use.
* @return $this object.
*/
public function layout($layout)
{
$this->layout = $layout;
return $this;
}

/**
* Sets headers.
*
* @param array $headers Headers to set.
* @return $this object.
*/
public function setHeaders(array $headers)
{
$this->_email->setHeaders($headers);
$this->_email->viewBuilder()->layout($layout);
return $this;
}

/**
* Adds headers.
*
* @param array $headers Headers to set.
* @return $this object.
*/
public function addHeaders(array $headers)
public function viewBuilder()
{
$this->_email->addHeaders($headers);
return $this;
return $this->_email->viewBuilder();
}

/**
* Sets attachments.
* Magic method to forward method class to Email instance.
*
* @param string|array $attachments String with the filename or array with filenames
* @return $this object.
* @throws \InvalidArgumentException
* @param string $method Method name.
* @param array $args Method arguments
* @return $this
*/
public function attachments($attachments)
public function __call($method, $args)
{
$this->_email->attachments($attachments);
call_user_func_array([$this->_email, $method], $args);
return $this;
}

Expand Down Expand Up @@ -227,87 +191,19 @@ public function send($action, $args = [], $headers = [])
]);
}

$this->setHeaders($headers);
$this->_email->setHeaders($headers);
if (!$this->_email->viewBuilder()->template()) {
$this->_email->viewBuilder()->template($action);
}

call_user_func_array([$this, $action], $args);

if ($this->template === null) {
$this->template = $action;
}

$result = $this->_email
->profile((array)$this)
->send();
$result = $this->_email->send();

$this->reset();
return $result;
}

/**
* Resets email instance to original config.
*
* @return $this object.
*/
public function reset()
{
$this->_email->reset();
return $this;
}

/**
* Checks if the property exists.
*
* @param string $offset Property name.
* @return bool True if it exists.
*/
public function offsetExists($offset)
{
return property_exists($this, $offset) ||
method_exists($this->_email, $offset);
}

/**
* Gets the property value if it exists.
*
* @param string $offset Property name.
* @return mixed Value.
*/
public function offsetGet($offset)
{
if (!$this->offsetExists($offset)) {
return null;
}

if (isset($this->{$offset})) {
return $this->{$offset};
}

return call_user_func([$this->_email, $offset]);
}

/**
* Sets property's value.
*
* @param string $offset Property name.
* @param mixed $value Value.
* @return void
*/
public function offsetSet($offset, $value)
{
$this->{$offset} = $value;
}

/**
* Unset a property.
*
* @param string $offset Property name.
* @return void
*/
public function offsetUnset($offset)
{
unset($this->{$offset});
}

/**
* Implemented events.
*
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Mailer/MailerTest.php
Expand Up @@ -50,7 +50,7 @@ public function testLayout()
{
$result = (new TestMailer())->layout('foo');
$this->assertInstanceOf('TestApp\Mailer\TestMailer', $result);
$this->assertEquals('foo', $result->layout);
$this->assertEquals('foo', $result->viewBuilder()->layout());
}

public function testProxies()
Expand Down Expand Up @@ -110,7 +110,7 @@ public function testSend()
->method('test')
->with('foo', 'bar');

$mailer->template = 'foobar';
$mailer->template('foobar');
$mailer->send('test', ['foo', 'bar']);
$this->assertEquals($mailer->template, 'foobar');
}
Expand Down
10 changes: 10 additions & 0 deletions tests/test_app/TestApp/Mailer/TestMailer.php
Expand Up @@ -27,4 +27,14 @@ public function getEmailForAssertion()
{
return $this->_email;
}

public function __call($method, $args)
{
if ($method === 'reset') {
$this->template = $this->viewBuilder()->template();
}

return parent::__call($method, $args);
}

}

0 comments on commit 26f6521

Please sign in to comment.