Skip to content

Commit

Permalink
Merge pull request #6 from swarming/master
Browse files Browse the repository at this point in the history
Updates for webhook and subscription
  • Loading branch information
garthbrantley authored Sep 9, 2016
2 parents f79340e + bc2e0fd commit e5d881c
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 89 deletions.
11 changes: 11 additions & 0 deletions src/SubscribePro/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,15 @@ protected function processResponse($response)

throw new HttpException($response);
}

/**
* @codeCoverageIgnore
*
* @return array|bool
*/
public function getRawRequest()
{
$rawRequest = file_get_contents('php://input');
return $rawRequest ? json_decode($rawRequest, true) : false;
}
}
57 changes: 52 additions & 5 deletions src/SubscribePro/Service/Subscription/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Subscription extends DataObject implements SubscriptionInterface
self::SHIPPING_ADDRESS_ID => false,
self::SHIPPING_ADDRESS => false,
self::PRODUCT_SKU => true,
self::REQUIRES_SHIPPING => true,
self::QTY => true,
self::USE_FIXED_PRICE => true,
self::FIXED_PRICE => false,
Expand All @@ -32,8 +33,9 @@ class Subscription extends DataObject implements SubscriptionInterface
self::EXPIRATION_DATE => false,
self::COUPON_CODE => false,
self::MAGENTO_STORE_CODE => false,
self::MAGENTO_SHIPPING_METHOD_CODE => true,
self::USER_DEFINED_FIELDS => false
self::MAGENTO_SHIPPING_METHOD_CODE => false,
self::USER_DEFINED_FIELDS => false,
self::PLATFORM_SPECIFIC_FIELDS => false
];

/**
Expand All @@ -44,6 +46,7 @@ class Subscription extends DataObject implements SubscriptionInterface
self::SHIPPING_ADDRESS_ID => false,
self::SHIPPING_ADDRESS => false,
self::PRODUCT_SKU => true,
self::REQUIRES_SHIPPING => true,
self::QTY => true,
self::USE_FIXED_PRICE => true,
self::FIXED_PRICE => false,
Expand All @@ -53,8 +56,9 @@ class Subscription extends DataObject implements SubscriptionInterface
self::EXPIRATION_DATE => false,
self::COUPON_CODE => false,
self::MAGENTO_STORE_CODE => false,
self::MAGENTO_SHIPPING_METHOD_CODE => true,
self::USER_DEFINED_FIELDS => false
self::MAGENTO_SHIPPING_METHOD_CODE => false,
self::USER_DEFINED_FIELDS => false,
self::PLATFORM_SPECIFIC_FIELDS => false
];

/**
Expand Down Expand Up @@ -126,10 +130,19 @@ public function getFormData()
*/
public function isValid()
{
return ($this->getData(self::SHIPPING_ADDRESS_ID, false) || $this->getShippingAddress()->isAsChildValid($this->isNew()))
return (!$this->getData(self::REQUIRES_SHIPPING) || $this->isShippingValid())
&& $this->checkRequiredFields($this->getFormFields());
}

/**
* @return bool
*/
protected function isShippingValid()
{
return ($this->getData(self::SHIPPING_ADDRESS_ID, false) || $this->getShippingAddress()->isAsChildValid($this->isNew()))
&& $this->getData(self::MAGENTO_SHIPPING_METHOD_CODE, false);
}

//@codeCoverageIgnoreStart

/**
Expand Down Expand Up @@ -186,6 +199,23 @@ public function setProductSku($productSku)
return $this->setData(self::PRODUCT_SKU, $productSku);
}

/**
* @return bool
*/
public function getRequiresShipping()
{
return $this->getData(self::REQUIRES_SHIPPING, false);
}

/**
* @param bool $useShipping
* @return $this
*/
public function setRequiresShipping($useShipping)
{
return $this->setData(self::REQUIRES_SHIPPING, $useShipping);
}

/**
* @return array
*/
Expand Down Expand Up @@ -508,6 +538,23 @@ public function setUserDefinedFields(array $userDefinedFields)
return $this->setData(self::USER_DEFINED_FIELDS, $userDefinedFields);
}

/**
* @return array
*/
public function getPlatformSpecificFields()
{
return $this->getData(self::PLATFORM_SPECIFIC_FIELDS, []);
}

/**
* @param array $platformSpecificFields
* @return $this
*/
public function setPlatformSpecificFields(array $platformSpecificFields)
{
return $this->setData(self::PLATFORM_SPECIFIC_FIELDS, $platformSpecificFields);
}

/**
* @param string|null $format
* @return string|null
Expand Down
24 changes: 24 additions & 0 deletions src/SubscribePro/Service/Subscription/SubscriptionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface SubscriptionInterface extends DataInterface
const CUSTOMER_ID = 'customer_id';
const STATUS = 'status';
const PRODUCT_SKU = 'product_sku';
const REQUIRES_SHIPPING = 'requires_shipping';
const SUBSCRIPTION_PRODUCTS = 'subscription_products';
const QTY = 'qty';
const USE_FIXED_PRICE = 'use_fixed_price';
Expand All @@ -36,6 +37,7 @@ interface SubscriptionInterface extends DataInterface
const EXPIRATION_DATE = 'expiration_date';
const COUPON_CODE = 'coupon_code';
const USER_DEFINED_FIELDS = 'user_defined_fields';
const PLATFORM_SPECIFIC_FIELDS = 'platform_specific_fields';
const RECURRING_ORDER_COUNT = 'recurring_order_count';
const ERROR_TIME = 'error_time';
const ERROR_CLASS = 'error_class';
Expand Down Expand Up @@ -98,6 +100,17 @@ public function getStatus();
*/
public function getProductSku();

/**
* @return bool
*/
public function getRequiresShipping();

/**
* @param bool $useShipping
* @return $this
*/
public function setRequiresShipping($useShipping);

/**
* @param string $productSku
* @return $this
Expand Down Expand Up @@ -307,6 +320,17 @@ public function getUserDefinedFields();
*/
public function setUserDefinedFields(array $userDefinedFields);

/**
* @return array
*/
public function getPlatformSpecificFields();

/**
* @param array $platformSpecificFields
* @return $this
*/
public function setPlatformSpecificFields(array $platformSpecificFields);

/**
* @param string|null $format
* @return string|null
Expand Down
32 changes: 14 additions & 18 deletions src/SubscribePro/Service/Webhook/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ class Event extends DataObject implements EventInterface
{
//@codeCoverageIgnoreStart

/**
* @return \SubscribePro\Service\Customer\CustomerInterface
*/
public function getCustomer()
{
return $this->getData(self::CUSTOMER);
}

/**
* @return \SubscribePro\Service\Subscription\SubscriptionInterface
*/
public function getSubscription()
{
return $this->getData(self::SUBSCRIPTION);
}

/**
* @return string
*/
Expand Down Expand Up @@ -61,14 +45,26 @@ public function getUpdated($format = null)

//@codeCoverageIgnoreEnd

/**
* @param string|null $field
* @return mixed|null
*/
public function getEventData($field = null)
{
$data = $this->getData(self::DATA);
if ($field && $data !== null) {
return isset($data[$field]) ? $data[$field] : null;
}

return $data;
}

/**
* @return array
*/
public function toArray()
{
$data = parent::toArray();
$data[self::CUSTOMER] = $this->getCustomer()->toArray();
$data[self::SUBSCRIPTION] = $this->getSubscription()->toArray();
$data[self::DESTINATIONS] = array_map(function (DestinationInterface $destination) {
return $destination->toArray();
}, $this->getDestinations());
Expand Down
43 changes: 15 additions & 28 deletions src/SubscribePro/Service/Webhook/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,10 @@ class EventFactory implements DataFactoryInterface
protected $destinationFactory;

/**
* @var \SubscribePro\Service\DataFactoryInterface
*/
protected $customerFactory;

/**
* @var \SubscribePro\Service\DataFactoryInterface
*/
protected $subscriptionFactory;

/**
* @param \SubscribePro\Service\DataFactoryInterface $customerFactory
* @param \SubscribePro\Service\DataFactoryInterface $subscriptionFactory
* @param \SubscribePro\Service\DataFactoryInterface $destinationFactory
* @param string $instanceName
*/
public function __construct(
\SubscribePro\Service\DataFactoryInterface $customerFactory,
\SubscribePro\Service\DataFactoryInterface $subscriptionFactory,
\SubscribePro\Service\DataFactoryInterface $destinationFactory,
$instanceName = '\SubscribePro\Service\Webhook\Event'
) {
Expand All @@ -48,8 +34,6 @@ public function __construct(

$this->instanceName = $instanceName;
$this->destinationFactory = $destinationFactory;
$this->customerFactory = $customerFactory;
$this->subscriptionFactory = $subscriptionFactory;
}

/**
Expand All @@ -58,19 +42,12 @@ public function __construct(
*/
public function create(array $data = [])
{
$jsonData = $this->getJsonData($data, EventInterface::DATA);
$customerData = $this->getFieldData($jsonData, EventInterface::CUSTOMER);
$subscriptionData = $this->getFieldData($jsonData, EventInterface::SUBSCRIPTION);
$eventData = $this->getEventData($data);
$destinationsData = $this->getFieldData($data, EventInterface::DESTINATIONS);

$data[EventInterface::DESTINATIONS] = $this->createDestinationItems($destinationsData);
$data[EventInterface::CUSTOMER] = $this->customerFactory->create($customerData);
$data[EventInterface::SUBSCRIPTION] = $this->subscriptionFactory->create($subscriptionData);

if (isset($data[EventInterface::DATA])) {
unset($data[EventInterface::DATA]);
}

$data[EventInterface::DATA] = $eventData;

return new $this->instanceName($data);
}

Expand All @@ -86,10 +63,20 @@ protected function getFieldData($data, $field)

/**
* @param array $data
* @param string $field
* @return array
*/
protected function getJsonData($data, $field)
protected function getEventData($data)
{
$eventData = $this->getJsonData($data, EventInterface::DATA);
return is_array($eventData) ? $eventData : [];
}

/**
* @param array $data
* @param string $field
* @return mixed
*/
private function getJsonData($data, $field)
{
return isset($data[$field]) && is_string($data[$field]) ? json_decode($data[$field], true) : [];
}
Expand Down
12 changes: 3 additions & 9 deletions src/SubscribePro/Service/Webhook/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,15 @@ interface EventInterface extends DataInterface
const ID = 'id';
const TYPE = 'type';
const DATA = 'data';
const CUSTOMER = 'customer';
const SUBSCRIPTION = 'subscription';
const DESTINATIONS = 'destinations';
const CREATED = 'created';
const UPDATED = 'updated';

/**
* @return \SubscribePro\Service\Customer\CustomerInterface
* @param string|null $field
* @return mixed|null
*/
public function getCustomer();

/**
* @return \SubscribePro\Service\Subscription\SubscriptionInterface
*/
public function getSubscription();
public function getEventData($field = null);

/**
* @return string
Expand Down
14 changes: 14 additions & 0 deletions src/SubscribePro/Service/Webhook/WebhookService.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ public function ping()
return true;
}

/**
* Read webhook event from request
*
* @return \SubscribePro\Service\Webhook\EventInterface|bool
*/
public function readEvent()
{
$rawRequestBody = $this->httpClient->getRawRequest();
$webhookEvent = !empty($rawRequestBody['webhook_event'])
? json_decode($rawRequestBody['webhook_event'], true)
: false;
return $webhookEvent ? $this->dataFactory->create($webhookEvent) : false;
}

/**
* @param int $eventId
* @return \SubscribePro\Service\Webhook\EventInterface
Expand Down
4 changes: 0 additions & 4 deletions src/SubscribePro/Service/Webhook/WebhookServiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace SubscribePro\Service\Webhook;

use SubscribePro\Service\AbstractServiceFactory;
use SubscribePro\Service\Customer\CustomerService;
use SubscribePro\Service\Subscription\SubscriptionService;
use SubscribePro\Service\Webhook\Event\DestinationFactory;

/**
Expand Down Expand Up @@ -35,8 +33,6 @@ protected function createDataFactory()
);

return new EventFactory(
$this->serviceFactoryResolver->getServiceFactory(CustomerService::NAME)->createDataFactory(),
$this->serviceFactoryResolver->getServiceFactory(SubscriptionService::NAME)->createDataFactory(),
$destinationFactory,
$this->getConfigValue(WebhookService::CONFIG_INSTANCE_NAME, '\SubscribePro\Service\Webhook\Event')
);
Expand Down
Loading

0 comments on commit e5d881c

Please sign in to comment.