Skip to content

Commit

Permalink
Merge pull request #10 from magento-api/MAGETWO-46263-RPC-SAMPLE
Browse files Browse the repository at this point in the history
[API] API Out: RPC Integration. Part 1
  • Loading branch information
Korshenko, Oleksii(okorshenko) committed Feb 16, 2016
2 parents d61ee10 + f33da1c commit 1d1cb86
Show file tree
Hide file tree
Showing 17 changed files with 513 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sample-bundle-all/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Synopsis

This is a meta package that pulls in all the sample Magento 2 modules from [packages.magento.com](http://packages.magento.com/)
This is a meta package that pulls in all the sample Magento 2 modules from [repo.magento.com](http://repo.magento.com/)

## Installation

Expand All @@ -12,6 +12,6 @@ This can be done by adding the following to the 'require' section.

"magento/sample-bundle-all": "*"

Ensure you have the package.magento.com added as a repository in your composer.json and then run `composer update` to have composer download the sample modules.
Ensure you have the repo.magento.com added as a repository in your composer.json and then run `composer update` to have composer download the sample modules.

Once installed, run the Magento 2 setup application and enable each of the installed modules.
21 changes: 21 additions & 0 deletions sample-ee-bundle-all/LICENSE_MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2015 Magento

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
17 changes: 17 additions & 0 deletions sample-ee-bundle-all/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
## Synopsis

This is a meta package that pulls in all the sample Magento 2 EE modules from [repo.magento.com](http://repo.magento.com/)

## Installation

To use these samples you will first need to [install Magento 2 EE](http://devdocs.magento.com/guides/v1.0/install-gde/bk-install-guide.html).

Update the root composer.json for Magento 2 to add a dependency on this package.

This can be done by adding the following to the 'require' section.

"magento/sample-ee-bundle-all": "*"

Ensure you have the repo.magento.com added as a repository in your composer.json and then run `composer update` to have composer download the sample modules.

Once installed, run the Magento 2 setup application and enable each of the installed modules.
12 changes: 12 additions & 0 deletions sample-ee-bundle-all/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "magento/sample-ee-bundle-all",
"description": "A package that pulls in all Magento 2 EE sample modules",
"type": "metapackage",
"version": "1.0.0",
"license": [
"proprietary"
],
"require": {
"magento/sample-module-sample-message-queue": "~1.0"
}
}
21 changes: 21 additions & 0 deletions sample-module-sample-message-queue/LICENSE_MIT.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright © 2015 Magento

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
103 changes: 103 additions & 0 deletions sample-module-sample-message-queue/Model/AddToCartPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SampleMessageQueue\Model;

use Magento\Checkout\Model\Cart;

/**
* Test plugin to demonstrate sync and async queue messages
*/
class AddToCartPlugin
{
/**
* @var \Magento\Framework\MessageQueue\PublisherInterface
*/
protected $publisher;

/**
* @var \Psr\Log\LoggerInterface $logger
*/
private $logger;

/**
* @var \Magento\GiftCardAccount\Model\GiftcardaccountFactory
*/
protected $giftCardAccountFactory;

/**
* Initialize dependencies.
*
* @param \Magento\Framework\MessageQueue\PublisherInterface $publisher
* @param \Magento\GiftCardAccount\Model\GiftcardaccountFactory $giftCardAccountFactory
* @param \Psr\Log\LoggerInterface $logger
*/
public function __construct(
\Magento\Framework\MessageQueue\PublisherInterface $publisher,
\Magento\GiftCardAccount\Model\GiftcardaccountFactory $giftCardAccountFactory,
\Psr\Log\LoggerInterface $logger
) {
$this->publisher = $publisher;
$this->logger = $logger;
$this->giftCardAccountFactory = $giftCardAccountFactory;
}

/**
* Add gift card account to notify customer via email
*
* @param Cart $subject
* @param \Closure $proceed
* @return Cart
*/
public function aroundSave(
Cart $subject,
\Closure $proceed
) {
$before = $subject->getItemsQty();
$result = $proceed();
$after = $subject->getItemsQty();

/**
* Note. Logger is used to demonstrate different execution phases of synchronous and asynchronous messages
*/
if ($subject->getQuote()->getCustomerId() && $before == 0 && $after > $before) {
$this->logger->debug('Plugin Start: Before items QTY: ' . $before . '; After Items QTY: ' . $after);
try {
$customer = $subject->getQuote()->getCustomer();
$giftCardAccountCode = $this->publisher
->publish(
'add.to.cart.product.added',
$subject->getQuote()->getId()
);

/** @var \Magento\GiftCardAccount\Model\Giftcardaccount $giftCard */
$giftCard = $this->giftCardAccountFactory->create();
$giftCard->loadByCode($giftCardAccountCode);
if (!$giftCard->getId()) {
throw new \Exception('Invalid gift card code');
}
$payload = [
'amount' => $giftCard->getGiftCardsAmount(),
'customer_email' => $customer->getEmail(),
'customer_name' => $customer->getFirstname() . ' ' . $customer->getLastname(),
'cart_id' => $subject->getQuote()->getId(),
];

$this->publisher->publish('add.to.cart.giftcard.added', $payload);

$this->publisher->publish('add.to.cart.giftcard.added.success', $payload);

} catch (\Exception $e) {
$this->logger->debug('Plugin Error: ' . $e->getMessage());
}
$this->logger->debug('Plugin End');
} else {
//Just for debugging
$this->logger->debug('Plugin: do nothing. ' . $before .' != 0 :');
}
return $result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SampleMessageQueue\Model\Handler\Async;

use \Psr\Log\LoggerInterface;

class GiftCardAddedSuccess
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* Initialize dependencies.
*
* @param LoggerInterface $logger
*/
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

/**
* Log information about added gift card
*
* @param array $data
* @return void
*/
public function log(array $data)
{
$this->logger->debug('ASYNC Handler: Gift Card Added Successfully: ' . serialize($data));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\SampleMessageQueue\Model\Handler\Async;

use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use \Psr\Log\LoggerInterface;

class SendCustomerNotification
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* @var TransportBuilder
*/
private $transportBuilder;

/**
* @var CartRepositoryInterface
*/
protected $cartRepository;

/**
* @var StoreManagerInterface
*/
protected $storeManager;

/**
* Initialize dependencies.
*
* @param LoggerInterface $logger
* @param TransportBuilder $transportBuilder
* @param CartRepositoryInterface $cartRepository
* @param StoreManagerInterface $storeManager
*/
public function __construct(
LoggerInterface $logger,
TransportBuilder $transportBuilder,
CartRepositoryInterface $cartRepository,
StoreManagerInterface $storeManager
) {
$this->logger = $logger;
$this->transportBuilder = $transportBuilder;
$this->cartRepository = $cartRepository;
$this->storeManager = $storeManager;
}

/**
* Send customer notification
*
* @param array $payload
* @throws \InvalidArgumentException
* @return void
*/
public function send(array $payload)
{
if (!isset($payload['cart_id'])) {
throw new \InvalidArgumentException('Cart ID is required');
}
$quoteId = $payload['cart_id'];
$quote = $this->cartRepository->get($quoteId);
$store = $this->storeManager->getStore($quote->getStoreId());
$storeName = $store->getName();

if (!isset($payload['customer_email'])) {
throw new \InvalidArgumentException('Customer email is required');
}

$transport = $this->transportBuilder->setTemplateIdentifier('giftcard_email_template')
->setTemplateOptions(
[
'area' => \Magento\Framework\App\Area::AREA_FRONTEND,
'store' => $quote->getStoreId()
]
)
->setTemplateVars(
[
'name' => isset($payload['customer_name']) ? $payload['customer_name'] : '',
'sender_name' => 'Your Friends at ' . $storeName,
'balance' => '$' . isset($payload['amount']) ? $payload['amount'] : 0
]
)
->setFrom(['name' => 'Your Friends at ' . $storeName, 'email' => ''])
->addTo($payload['customer_email'])
->getTransport();
$transport->sendMessage();

$this->logger->debug('ASYNC Handler: Sent customer notification email to: ' . $payload['customer_email']);
}
}
Loading

0 comments on commit 1d1cb86

Please sign in to comment.