Skip to content

Commit

Permalink
2906332 Refactor message handeling method
Browse files Browse the repository at this point in the history
  • Loading branch information
haringsrob committed Sep 9, 2017
1 parent 7507595 commit 958f297
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/checkout/commerce_checkout.module
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ function commerce_checkout_theme() {
'commerce_checkout_completion_message' => [
'variables' => [
'order_entity' => NULL,
'completion_messages' => NULL,
'payment_instructions' => NULL,
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,71 @@
*/
class CompletionMessage extends CheckoutPaneBase {

/**
* @var \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages
*/
private $completionMessags;

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, \Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface $checkout_flow, \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager);
$this->completionMessags = new CompletionMessages();
}

/**
* {@inheritdoc}
*/
public function buildPaneForm(array $pane_form, FormStateInterface $form_state, array &$complete_form) {
$this->preparePaneForm();

$pane_form['#theme'] = 'commerce_checkout_completion_message';
$pane_form['#order_entity'] = $this->order;
$pane_form['#completion_messages'] = $this->completionMessags;

return $pane_form;
}

/**
* Prepares the necessary data for the completion messages.
*/
public function preparePaneForm() {
$this->populateCompletionMessages();
}

/**
* Gets the completion messages.
*/
private function populateCompletionMessages() {
$this->populateCompletionMessagesWithDefaultMessage();
$this->populateCompletionMessagesWithLoggedInMessageIfLoggedIn();

$this->allowOthersToModifyMessages();
}

/**
* Gets the default completion message.
*/
private function populateCompletionMessagesWithDefaultMessage() {
return $this->completionMessags->addMessage($this->t('Your order number is @number.', ['@number' => $this->order->id()]));
}

/**
* Populate the completion messages with the logged in message.
*/
private function populateCompletionMessagesWithLoggedInMessageIfLoggedIn() {
if (\Drupal::currentUser()->isAuthenticated()) {
$this->completionMessags->addMessage($this->t('You can view your order on your account page when logged in.'));
}
}

/**
* Allow other modules to alter the messages.
*/
private function allowOthersToModifyMessages() {
\Drupal::moduleHandler()
->alter('checkout_completion_messages', $this->completionMessags, $this->order);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane;

use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\TranslatableInterface;

/**
* Acts as a container to collect all completion messages.
*
* @package Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane
*/
class CompletionMessages implements \Iterator, \Countable {

/**
* @var \Drupal\Core\TypedData\TranslatableInterface[]
*/
private $messages;

/**
* @var int
*/
private $position;

/**
* Sets up the position to 0.
*/
public function __construct() {
$this->position = 0;
}

/**
* Return the current element
*
* @link http://php.net/manual/en/iterator.current.php
* @return mixed Can return any type.
* @since 5.0.0
*/
public function current() {
return $this->messages[$this->position];
}

/**
* Move forward to next element
*
* @link http://php.net/manual/en/iterator.next.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function next() {
++$this->position;
}

/**
* Return the key of the current element
*
* @link http://php.net/manual/en/iterator.key.php
* @return mixed scalar on success, or null on failure.
* @since 5.0.0
*/
public function key() {
return $this->position;
}

/**
* Checks if current position is valid
*
* @link http://php.net/manual/en/iterator.valid.php
* @return boolean The return value will be casted to boolean and then
* evaluated. Returns true on success or false on failure.
* @since 5.0.0
*/
public function valid() {
return isset($this->messages[$this->position]);
}

/**
* Rewind the Iterator to the first element
*
* @link http://php.net/manual/en/iterator.rewind.php
* @return void Any returned value is ignored.
* @since 5.0.0
*/
public function rewind() {
$this->position = 0;
}

/**
* Adds a message to the array.
*
* @param \Drupal\Core\StringTranslation\TranslatableMarkup $message
*/
public function addMessage(TranslatableMarkup $message) {
$this->messages[] = $message;
}

/**
* Count elements of an object
*
* @link http://php.net/manual/en/countable.count.php
* @return int The custom count as an integer.
* </p>
* <p>
* The return value is cast to an integer.
* @since 5.1.0
*/
public function count() {
return count($this->messages);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
*/
#}
<div class="checkout-complete">
{% for message in completion_messages %}
{{ message }}
{% endfor %}

{{ 'Your order number is @number.'|t({'@number': order_entity.getOrderNumber}) }} <br>
{{ 'You can view your order on your account page when logged in.'|t }} <br>

Expand Down
47 changes: 47 additions & 0 deletions modules/checkout/tests/src/Kernel/CompletionMessagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* Created by PhpStorm.
* User: robharings
* Date: 09/09/2017
* Time: 13:31
*/

namespace Drupal\Tests\commerce_checkout\Kernel;

use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;

/**
* Tests the completion messages class.
*
* @covers \Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CompletionMessages
*/
class CompletionMessagesTest extends CommerceKernelTestBase {

/**
* @var CompletionMessages
*/
private $completionMessages;

public function setUp() {
parent::setUp();
$this->completionMessages = new CompletionMessages();
}

public function testAddMessage() {
$this->completionMessages->addMessage(t('Message 1'));
$this->completionMessages->addMessage(t('Message 2'));

$this->assertCount(2, $this->completionMessages);
}

public function testMessagesIterator() {
$this->completionMessages->addMessage(t('Message 1'));
$this->completionMessages->addMessage(t('Message 2'));

$this->assertEquals('Message 1', $this->completionMessages->current()->render());
$this->completionMessages->next();
$this->assertEquals('Message 2', $this->completionMessages->current()->render());
}

}

0 comments on commit 958f297

Please sign in to comment.