Skip to content

Commit

Permalink
#40 Sender instance passed to the Factory
Browse files Browse the repository at this point in the history
  • Loading branch information
danbilauca authored and Sylvain Schellenberger committed Mar 2, 2021
1 parent 974458f commit 33bf5b3
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 8 deletions.
37 changes: 30 additions & 7 deletions includes/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class WPNotify_Factory {

const MESSAGE = 'message';
const MESSAGE = 'message';
const RECIPIENTS = 'recipients';

/**
Expand All @@ -19,28 +19,45 @@ class WPNotify_Factory {
*/
private $recipient_factory;

/**
* Sender factory implementation to use
*
* @var WPNotify_SenderFactory
*/
private $sender_factory;

public function __construct(
WPNotify_MessageFactory $message_factory,
WPNotify_RecipientFactory $recipient_factory
WPNotify_RecipientFactory $recipient_factory,
WPNotify_SenderFactory $sender_factory
) {

$this->message_factory = $message_factory;
$this->recipient_factory = $recipient_factory;
$this->sender_factory = $sender_factory;
}

/**
* Create a notification instance
*
* @param $args
*
* @return WPNotify_BaseNotification
*/
public function create( $args ) {
list( $message_args, $recipients_args ) = $this->validate( $args );

$message = $this->message_factory->create( $message_args );
[ $message_args, $recipients_args, $sender_args ] = $this->validate( $args );

$sender = $this->sender_factory->create( $sender_args );
$recipients = new WPNotify_RecipientCollection();
$message = $this->message_factory->create( $message_args );

foreach ( $recipients_args as $type => $value ) {
$recipients->add(
$this->recipient_factory->create( $value, $type )
);
}

return new WPNotify_BaseNotification( new BaseSender(), $message, $recipients );
return new WPNotify_BaseNotification( $sender, $recipients, $message );
}

private function validate( $args ) {
Expand All @@ -49,11 +66,12 @@ private function validate( $args ) {
$args = json_decode( $args );
}

list( $message_args, $recipients_args ) = $args;
list( $message_args, $recipients_args, $sender_args ) = $args;

return array(
$this->get_message_args( $message_args ),
$this->get_recipients_args( $recipients_args ),
$this->get_sender_args( $sender_args ),
);
}

Expand All @@ -66,4 +84,9 @@ private function get_recipients_args( $args ) {
// TODO: Parse recipients args.
return $args;
}

public function get_sender_args( $args ) {
// TODO: Parse sender args.
return $args;
}
}
13 changes: 13 additions & 0 deletions includes/senders/SenderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

interface WPNotify_SenderFactory {

/**
* Create a new instance of notification sender
*
* @param string $name
*
* @return WPNotify_Sender
*/
public function create( string $name ): WPNotify_Sender;
}
1 change: 0 additions & 1 deletion tests/phpunit/includes/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,3 @@

require dirname( __FILE__ ) . '/stubs.php';
require dirname( __FILE__ ) . '/TestCase.php';

38 changes: 38 additions & 0 deletions tests/phpunit/tests/test-factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

class Test_WPNotify_Factory extends WPNotify_TestCase {


public function test_create_with_vendor_sender() {

$vendor_sender = $this->createMock( 'WPNotify_Sender' );

$message_factory = $this->getMockBuilder( 'WPNotify_MessageFactory' )
->setMethods( array( 'create', 'accepts' ) )
->getMock();

$message_factory->method( 'create' )->willReturn( $this->createMock( 'WPNotify_Message' ) );
$message_factory->method( 'accepts' )->willReturn( true );

$sender_factory = $this->getMockBuilder( 'WPNotify_SenderFactory' )
->setMethods( array( 'create' ) )
->getMock();

$sender_factory->method( 'create' )->willReturn( $this->createMock( 'WPNotify_Sender' ) );

$factory = new WPNotify_Factory(
$message_factory,
$this->createMock( 'WPNotify_RecipientFactory' ),
$sender_factory
);

$args = array(
array(),
array(),
array(),
);

$notification = $factory->create( $args );
$this->assertEquals( $vendor_sender, $notification->get_sender() );
}
}

0 comments on commit 33bf5b3

Please sign in to comment.