<commit_before><?php namespace Owlgrin\Cashew\Storage;

use Owlgrin\Cashew\Storage\Storage;
use Owlgrin\Cashew\Customer\Customer;
use Owlgrin\Cashew\Subscription\Subscription;
use Owlgrin\Cashew\Card\Card;
use Carbon\Carbon, Config, DB;

class DbStorage implements Storage {
	
	public function subscription($id)
	{
		if( ! $id) throw new \Exception('Cannot fetch subscription');
		
		return $this->subscriptionByUser($id);
	}

	public function create($userId, $trialEnd = null)
	{
		try
		{
			$id = DB::table(Config::get('cashew::table'))->insertGetId(array(
				'user_id' => $userId,
				'trial_ends_at' => $trialEnd,
				'status' => 'trialing',
				'created_at' => DB::raw('now()'),
				'updated_at' => DB::raw('now()')
			));

			return $id;
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function customer($userId, Customer $customer)
	{
		try
		{
			DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'customer_id' => $customer->id(),
					'last_four' => $customer->card()->lastFour(),
					'updated_at' => DB::raw('now()')
				));
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function subscribe($userId, Subscription $subscription)
	{
		try
		{
			DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'trial_ends_at' => $subscription->trialEnd(),
					'subscription_ends_at' => null,
					'plan' => $subscription->plan(),
					'quantity' => $subscription->quantity(),
					'status' => $subscription->status(),
					'updated_at' => DB::raw('now()'),
					'subscribed_at' => DB::raw('now()')
				));
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function update($userId, Customer $customer)
	{
		try
		{
			$subscription = $customer->subscription();

			$id = DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'plan' => $subscription->plan(),
					'quantity' => $subscription->quantity(),
					'last_four' => $customer->card()->lastFour(),
					'updated_at' => DB::raw('now()')
				));

			return $id;
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function cancel($userId, Subscription $subscription)
	{
		try
		{
			$id = DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'subscription_ends_at' => $subscription->end(),
					'status' => 'canceled',
					'updated_at' => DB::raw('now()'),
					'canceled_at' => DB::raw('now()')<commit_msg>updating the trial end date on updating<commit_after><?php namespace Owlgrin\Cashew\Storage;

use Owlgrin\Cashew\Storage\Storage;
use Owlgrin\Cashew\Customer\Customer;
use Owlgrin\Cashew\Subscription\Subscription;
use Owlgrin\Cashew\Card\Card;
use Carbon\Carbon, Config, DB;

class DbStorage implements Storage {
	
	public function subscription($id)
	{
		if( ! $id) throw new \Exception('Cannot fetch subscription');
		
		return $this->subscriptionByUser($id);
	}

	public function create($userId, $trialEnd = null)
	{
		try
		{
			$id = DB::table(Config::get('cashew::table'))->insertGetId(array(
				'user_id' => $userId,
				'trial_ends_at' => $trialEnd,
				'status' => 'trialing',
				'created_at' => DB::raw('now()'),
				'updated_at' => DB::raw('now()')
			));

			return $id;
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function customer($userId, Customer $customer)
	{
		try
		{
			DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'customer_id' => $customer->id(),
					'last_four' => $customer->card()->lastFour(),
					'updated_at' => DB::raw('now()')
				));
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function subscribe($userId, Subscription $subscription)
	{
		try
		{
			DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'trial_ends_at' => $subscription->trialEnd(),
					'subscription_ends_at' => null,
					'plan' => $subscription->plan(),
					'quantity' => $subscription->quantity(),
					'status' => $subscription->status(),
					'updated_at' => DB::raw('now()'),
					'subscribed_at' => DB::raw('now()')
				));
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function update($userId, Customer $customer)
	{
		try
		{
			$subscription = $customer->subscription();

			$id = DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'trial_ends_at' => $subscription->trialEnd(),
					'plan' => $subscription->plan(),
					'quantity' => $subscription->quantity(),
					'last_four' => $customer->card()->lastFour(),
					'status' => $subscription->status(),
					'updated_at' => DB::raw('now()')
				));

			return $id;
		}
		catch(\PDOException $e)
		{
			throw new \Exception($e->getMessage());
		}
	}

	public function cancel($userId, Subscription $subscription)
	{
		try
		{
			$id = DB::table(Config::get('cashew::table'))
				->where('user_id', '=', $userId)
				->update(array(
					'subscription_ends_at' => $subscription->end(),
					'status' => 'canceled',
					'updated_at' => DB::raw('now()'),
					'canceled_at' => DB::raw('now()')
{'fraction_non_alphanumeric': 0.20812465219810797, 'fraction_numerical': 0.0, 'mean_word_length': 3.7928888888888888, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 107, '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': '355742', 'n_tokens_mistral': 2263, 'n_tokens_neox': 1915, 'n_words': 315}