<commit_before><?php namespace Mmanos\Billing\Gateways\Stripe;

use Mmanos\Billing\Gateways\CustomerInterface;
use Illuminate\Support\Arr;
use Stripe_Customer;
use Stripe_Invoice;
use Stripe_Charge;

class Customer implements CustomerInterface
{
	/**
	 * The gateway instance.
	 *
	 * @var Gateway
	 */
	protected $gateway;
	
	/**
	 * Primary identifier.
	 *
	 * @var mixed
	 */
	protected $id;
	
	/**
	 * Stripe customer object.
	 *
	 * @var Stripe_Customer
	 */
	protected $stripe_customer;
	
	/**
	 * Create a new Stripe customer instance.
	 *
	 * @param Gateway $gateway
	 * @param mixed   $id
	 * 
	 * @return void
	 */
	public function __construct(Gateway $gateway, $id = null)
	{
		$this->gateway = $gateway;
		
		if ($id instanceof Stripe_Customer) {
			$this->stripe_customer = $id;
			$this->id = $this->stripe_customer->id;
		}
		else if (null !== $id) {
			$this->id = $id;
		}
	}
	
	/**
	 * Gets the id of this instance.
	 * 
	 * @return mixed
	 */
	public function id()
	{
		return $this->id;
	}
	
	/**
	 * Gets info for a customer.
	 * 
	 * @return array|null
	 */
	public function info()
	{
		if (!$this->id) {
			return null;
		}
		
		if (!$this->stripe_customer) {
			$this->stripe_customer = Stripe_Customer::retrieve($this->id);
		}
		
		if (!$this->stripe_customer || $this->stripe_customer->deleted) {
			return null;
		}
		
		$discounts = array();
		if ($this->stripe_customer->discount) {
			$discounts[] = array(
				'coupon'      => $this->stripe_customer->discount->coupon->id,
				'amount_off'  => $this->stripe_customer->discount->coupon->amount_off,
				'percent_off' => $this->stripe_customer->discount->coupon->percent_off,
				'started_at'  => date('Y-m-d H:i:s', $this->stripe_customer->discount->start),
				'ends_at'     => $this->stripe_customer->discount->end ? date('Y-m-d H:i:s', $this->stripe_customer->discount->end) : null,
			);
		}
		
		return array(
			'id'          => $this->id,
			'description' => $this->stripe_customer->description,
			'email'       => $this->stripe_customer->email,
			'created_at'  => date('Y-m-d H:i:s', $this->stripe_customer->created),
			'discounts'   => $discounts,
		);
	}
	
	/**
	 * Create a new customer.
	 *
	 * @param array $properties
	 * 
	 * @return Customer
	 */
	public function create(array $properties = array())<commit_msg>Added checking deleted property exists for customer in stripe gateway<commit_after><?php namespace Mmanos\Billing\Gateways\Stripe;

use Mmanos\Billing\Gateways\CustomerInterface;
use Illuminate\Support\Arr;
use Stripe_Customer;
use Stripe_Invoice;
use Stripe_Charge;

class Customer implements CustomerInterface
{
	/**
	 * The gateway instance.
	 *
	 * @var Gateway
	 */
	protected $gateway;
	
	/**
	 * Primary identifier.
	 *
	 * @var mixed
	 */
	protected $id;
	
	/**
	 * Stripe customer object.
	 *
	 * @var Stripe_Customer
	 */
	protected $stripe_customer;
	
	/**
	 * Create a new Stripe customer instance.
	 *
	 * @param Gateway $gateway
	 * @param mixed   $id
	 * 
	 * @return void
	 */
	public function __construct(Gateway $gateway, $id = null)
	{
		$this->gateway = $gateway;
		
		if ($id instanceof Stripe_Customer) {
			$this->stripe_customer = $id;
			$this->id = $this->stripe_customer->id;
		}
		else if (null !== $id) {
			$this->id = $id;
		}
	}
	
	/**
	 * Gets the id of this instance.
	 * 
	 * @return mixed
	 */
	public function id()
	{
		return $this->id;
	}
	
	/**
	 * Gets info for a customer.
	 * 
	 * @return array|null
	 */
	public function info()
	{
		if (!$this->id) {
			return null;
		}
		
		if (!$this->stripe_customer) {
			$this->stripe_customer = Stripe_Customer::retrieve($this->id);
		}
		
		if (!$this->stripe_customer || property_exists($this->stripe_customer, 'deleted')) {
			return null;
		}
		
		$discounts = array();
		if ($this->stripe_customer->discount) {
			$discounts[] = array(
				'coupon'      => $this->stripe_customer->discount->coupon->id,
				'amount_off'  => $this->stripe_customer->discount->coupon->amount_off,
				'percent_off' => $this->stripe_customer->discount->coupon->percent_off,
				'started_at'  => date('Y-m-d H:i:s', $this->stripe_customer->discount->start),
				'ends_at'     => $this->stripe_customer->discount->end ? date('Y-m-d H:i:s', $this->stripe_customer->discount->end) : null,
			);
		}
		
		return array(
			'id'          => $this->id,
			'description' => $this->stripe_customer->description,
			'email'       => $this->stripe_customer->email,
			'created_at'  => date('Y-m-d H:i:s', $this->stripe_customer->created),
			'discounts'   => $discounts,
		);
	}
	
	/**
	 * Create a new customer.
	 *
	 * @param array $properties
	 * 
	 * @return Customer
	 */
	public function create(array $properties = array())
{'fraction_non_alphanumeric': 0.1667021503087077, 'fraction_numerical': 0.0, 'mean_word_length': 3.1282952548330405, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4477', 'n_tokens_mistral': 1892, 'n_tokens_neox': 1651, 'n_words': 327}