Skip to content

Commit

Permalink
namespace Import -> ChartMogul, backwards compatible (#17)
Browse files Browse the repository at this point in the history
* namespace Import -> ChartMogul, backwards compatible
  • Loading branch information
pkopac committed Mar 10, 2017
1 parent 1b50fa8 commit 084186c
Show file tree
Hide file tree
Showing 23 changed files with 400 additions and 326 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ or in `composer.json`:
```json
{
"require": {
"chartmogul/chartmogul-php": "1.0.0"
"chartmogul/chartmogul-php": "1.1.2"
}
}
```
Expand Down
37 changes: 35 additions & 2 deletions src/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function customAttributes()
*/
public static function findByExternalId($externalId)
{
return static::all(['external_id' => $externalId])->entries->first();
if (gettype($externalId) == 'string') {
$externalId = ['external_id' => $externalId];
}
return static::all($externalId)->entries->first();
}

/**
Expand Down Expand Up @@ -132,7 +135,7 @@ public static function search($email, ClientInterface $client = null)
*/
public static function merge($from, $into, ClientInterface $client = null)
{
$response = (new static([], $client))
(new static([], $client))
->getClient()
->setResourcekey(static::class)
->send('/v1/customers/merges', 'POST', [
Expand Down Expand Up @@ -229,4 +232,34 @@ public function updateCustomAttributes($custom)
$this->attributes['custom'] = $result['custom'];
return $result['custom'];
}

/**
* Find a Customer Subscriptions
* @param array $options
* @return \Doctrine\Common\Collections\ArrayCollection | Customer
* @deprecated Use Import\Subscription.
*/
public function subscriptions(array $options = [])
{
if (!isset($this->subscriptions)) {
$options['customer_uuid'] = $this->uuid;
$this->subscriptions = Subscription::all($options);
}
return $this->subscriptions;
}

/**
* Find customer's invoices
* @param array $options
* @return \Doctrine\Common\Collections\ArrayCollection | Customer
* @deprecated Use Import\CustomerInvoices.
*/
public function invoices(array $options = [])
{
if (!isset($this->invoices)) {
$options['customer_uuid'] = $this->uuid;
$this->invoices = CustomerInvoices::all($options)->invoices;
}
return $this->invoices;
}
}
47 changes: 47 additions & 0 deletions src/CustomerInvoices.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace ChartMogul;

use ChartMogul\Resource\AbstractResource;
use ChartMogul\Http\ClientInterface;
use Doctrine\Common\Collections\ArrayCollection;

class CustomerInvoices extends AbstractResource
{

use \ChartMogul\Service\CreateTrait;
use \ChartMogul\Service\AllTrait;

/**
* @ignore
*/
const RESOURCE_PATH = '/v1/import/customers/:customer_uuid/invoices';
/**
* @ignore
*/
const RESOURCE_NAME = 'Invoices';

public $invoices = [];

public $customer_uuid;

public function __construct(array $attr = [], ClientInterface $client = null)
{
parent::__construct($attr, $client);

$this->invoices = new ArrayCollection($this->invoices);
foreach ($this->invoices as $key => $item) {
$this->setInvoice($key, $item);
}
}

protected function setInvoice($index, $invoice)
{

if ($invoice instanceof \ChartMogul\Import\Invoice) {
$this->invoices[$index] =$invoice;
} elseif (is_array($invoice)) {
$this->invoices[$index] = new \ChartMogul\Import\Invoice($invoice);
}
}
}
77 changes: 1 addition & 76 deletions src/Import/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,84 +2,9 @@

namespace ChartMogul\Import;

use ChartMogul\Resource\AbstractResource;

/**
* @deprecated Use ChartMogul\Customer, Import\CustomerInvoices or Import\Subscription.
* @property-read string $uuid
*/
class Customer extends AbstractResource
{

use \ChartMogul\Service\CreateTrait;
use \ChartMogul\Service\AllTrait;
use \ChartMogul\Service\DestroyTrait;

/**
* @ignore
*/
const RESOURCE_PATH = '/v1/import/customers';
/**
* @ignore
*/
const RESOURCE_NAME = 'Customer';
/**
* @ignore
*/
const ROOT_KEY = 'customers';

protected $uuid;

public $external_id;
public $name;
public $email;
public $company;
public $country;
public $state;
public $city;
public $zip;
public $data_source_uuid;
public $lead_created_at;
public $free_trial_started_at;

/**
* Find a Customer by External ID
* @param string $externalId
* @return Customer
* @deprecated Use ChartMogul\Customer
*/
public static function findByExternalId(array $options = [])
{
return static::all($options)->first();
}

/**
* Find a Customer Subscriptions
* @param array $options
* @return \Doctrine\Common\Collections\ArrayCollection | Customer
* @deprecated Use Import\Subscription.
*/
public function subscriptions(array $options = [])
{
if (!isset($this->subscriptions)) {
$options['customer_uuid'] = $this->uuid;
$this->subscriptions = Subscription::all($options);
}
return $this->subscriptions;
}
class Customer extends ChartMogul\Customer {

/**
* Find a Customer Invoices
* @param array $options
* @return \Doctrine\Common\Collections\ArrayCollection | Customer
* @deprecated Use Import\CustomerInvoices.
*/
public function invoices(array $options = [])
{
if (!isset($this->invoices)) {
$options['customer_uuid'] = $this->uuid;
$this->invoices = CustomerInvoices::all($options)->invoices;
}
return $this->invoices;
}
}
45 changes: 4 additions & 41 deletions src/Import/CustomerInvoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,9 @@

namespace ChartMogul\Import;

use ChartMogul\Resource\AbstractResource;
use ChartMogul\Http\ClientInterface;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @deprecated Use \ChartMogul\CustomerInvoices
*/
class CustomerInvoices extends \ChartMogul\CustomerInvoices {

class CustomerInvoices extends AbstractResource
{

use \ChartMogul\Service\CreateTrait;
use \ChartMogul\Service\AllTrait;

/**
* @ignore
*/
const RESOURCE_PATH = '/v1/import/customers/:customer_uuid/invoices';
/**
* @ignore
*/
const RESOURCE_NAME = 'Invoices';

public $invoices = [];

public $customer_uuid;

public function __construct(array $attr = [], ClientInterface $client = null)
{
parent::__construct($attr, $client);

$this->invoices = new ArrayCollection($this->invoices);
foreach ($this->invoices as $key => $item) {
$this->setInvoice($key, $item);
}
}

protected function setInvoice($index, $invoice)
{

if ($invoice instanceof \ChartMogul\Import\Invoice) {
$this->invoices[$index] =$invoice;
} elseif (is_array($invoice)) {
$this->invoices[$index] = new \ChartMogul\Import\Invoice($invoice);
}
}
}
10 changes: 10 additions & 0 deletions src/Import/DataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ChartMogul\Import;

/**
* @deprecated Use ChartMogul\DataSource
*/
class DataSource extends ChartMogul\DataSource {

}
60 changes: 2 additions & 58 deletions src/Import/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,9 @@

namespace ChartMogul\Import;

use ChartMogul\Import\LineItems\AbstractLineItem;
use ChartMogul\Import\LineItems\OneTime;
use ChartMogul\Import\LineItems\Subscription as SubsItem;
use ChartMogul\Import\Transactions\AbstractTransaction;
use ChartMogul\Import\Transactions\Payment;
use ChartMogul\Import\Transactions\Refund;
use Doctrine\Common\Collections\ArrayCollection;

/**
* @property-read string $uuid
* @deprecated Use \ChartMogul\Invoice
*/
class Invoice extends \ChartMogul\Resource\AbstractModel
{

protected $uuid;

public $date;
public $currency;
public $line_items = [];
public $transactions = [];
public $external_id;
public $due_date;

public function __construct(array $attr = [])
{
parent::__construct($attr);

$this->line_items = new ArrayCollection($this->line_items);
foreach ($this->line_items as $key => $item) {
$this->setLineItem($key, $item);
}

$this->transactions = new ArrayCollection($this->transactions);
foreach ($this->transactions as $key => $item) {
$this->setTransaction($key, $item);
}
}

protected function setLineItem($index, $line)
{

if ($line instanceof AbstractLineItem) {
$this->line_items[$index] = $line;
} elseif (is_array($line) && isset($line['type']) && $line['type'] === 'one_time') {
$this->line_items[$index] = new OneTime($line);
} elseif (is_array($line) && isset($line['type']) && $line['type'] === 'subscription') {
$this->line_items[$index] = new SubsItem($line);
}
}

protected function setTransaction($index, $tr)
{
class Invoice extends \ChartMogul\Invoice {

if ($tr instanceof AbstractTransaction) {
$this->transactions[$index] = $tr;
} elseif (is_array($tr) && isset($tr['type']) && $tr['type'] === 'payment') {
$this->transactions[$index] = new Payment($tr);
} elseif (is_array($tr) && isset($tr['type']) && $tr['type'] === 'refund') {
$this->transactions[$index] = new Refund($tr);
}
}
}
16 changes: 2 additions & 14 deletions src/Import/LineItems/AbstractLineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,8 @@

/**
* @codeCoverageIgnore
* @property-read string $uuid
* @deprecated
*/
abstract class AbstractLineItem extends \ChartMogul\Resource\AbstractModel
abstract class AbstractLineItem extends \ChartMogul\AbstractLineItem
{

protected $uuid;

public $type;
public $amount_in_cents;
public $quantity;
public $discount_amount_in_cents;
public $discount_code;
public $tax_amount_in_cents;
public $external_id;

public $invoice_uuid;
}
6 changes: 2 additions & 4 deletions src/Import/LineItems/OneTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

/**
* @codeCoverageIgnore
* @deprecated Use ChartMogul\OneTime
*/
class OneTime extends AbstractLineItem
{
class OneTime extends ChartMogul\OneTime {

public $type = 'one_time';
public $description;
}
15 changes: 3 additions & 12 deletions src/Import/LineItems/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,8 @@

/**
* @codeCoverageIgnore
* @deprecated Use ChartMogul\Subscription
*/
class Subscription extends AbstractLineItem
{

public $type = 'subscription';
public $subscription_external_id;
public $service_period_start;
public $service_period_end;
public $cancelled_at;
public $prorated;

protected $subscription_uuid;
public $plan_uuid;
class Subscription extends Subscription{

}
10 changes: 10 additions & 0 deletions src/Import/Plan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace ChartMogul\Import;

/**
* @deprecated Use ChartMogul\Plan
*/
class Plan extends ChartMogul\Plan {

}

0 comments on commit 084186c

Please sign in to comment.