<commit_before><?php
/**
 * Syncs the subscription to group linking. Note: this should be run after
 * {@link SyncChargifyMembersTask}.
 *
 * @package silverstripe-chargify
 */
class SyncChargifySubscriptionsTask extends BuildTask {

	protected $title = 'Sync Chargify Subscriptions';
	protected $description = 'Syncs the Chargify subscription to group linking.';

	public function run($request) {
		$subscribed = $deleted = 0;

		// Ensure we have an uncached connector.
		$connector = ChargifyService::instance()->getConnector();
		$connector->setCacheExpiry(-1);

		// Now loop through each page of API data until we reach the end.
		for ($page = 1; ; $page++) {
			if (!$subs = $connector->getSubscriptions($page)) {
				break;
			}

			foreach ($subs as $subscription) {
				$result = $this->processSubscription($subscription);

				if ($result == 'subscribed') {
					$subscribed++;
				} elseif ($result == 'deleted') {
					$deleted++;
				}
			}
		}

		echo "Subscribed $subscribed members and removed $deleted subscriptions.\n";
	}

	/**
	 * @param  ChargifySubscription $subscription
	 * @return string
	 */
	protected function processSubscription($subscription) {
		$id    = $subscription->id;
		$state = $subscription->state;

		if (in_array($state, array('canceled', 'unpaid', 'expired'))) {
			DB::query(sprintf(
				'DELETE FROM "Group_Members" WHERE "Chargify" = 1 AND "SubscriptionID" = %d',
				$id
			));

			return 'deleted';
		} else {
			$member = DataObject::get_one('Member', sprintf(
				'"ChargifyID" = %d', $subscription->customer->id
			));

			if (!$member) return;

			$groups = DataObject::get('Group', sprintf(
				'"ChargifyProductID" = %d', $subscription->product->id
			));

			$result = null;

			if ($groups) foreach ($groups as $group) {<commit_msg>Updated SyncChargifySubscriptionTask to use the customer link.<commit_after><?php
/**
 * Syncs the subscription to group linking. Note: this should be run after
 * {@link SyncChargifyMembersTask}.
 *
 * @package silverstripe-chargify
 */
class SyncChargifySubscriptionsTask extends BuildTask {

	protected $title = 'Sync Chargify Subscriptions';
	protected $description = 'Syncs the Chargify subscription to group linking.';

	public function run($request) {
		$subscribed = $deleted = 0;

		// Ensure we have an uncached connector.
		$connector = ChargifyService::instance()->getConnector();
		$connector->setCacheExpiry(-1);

		// Now loop through each page of API data until we reach the end.
		for ($page = 1; ; $page++) {
			if (!$subs = $connector->getSubscriptions($page)) {
				break;
			}

			foreach ($subs as $subscription) {
				$result = $this->processSubscription($subscription);

				if ($result == 'subscribed') {
					$subscribed++;
				} elseif ($result == 'deleted') {
					$deleted++;
				}
			}
		}

		echo "Subscribed $subscribed members and removed $deleted subscriptions.\n";
	}

	/**
	 * @param  ChargifySubscription $subscription
	 * @return string
	 */
	protected function processSubscription($subscription) {
		$id    = $subscription->id;
		$state = $subscription->state;

		if (in_array($state, array('canceled', 'unpaid', 'expired'))) {
			DB::query(sprintf(
				'DELETE FROM "Group_Members" WHERE "Chargify" = 1 AND "SubscriptionID" = %d',
				$id
			));

			return 'deleted';
		} else {
			$link = DataObject::get_one('ChargifyCustomerLink', sprintf(
				'"CustomerID" = %d', $subscription->customer->id
			));

			if (!$link) return;
			$member = $link->Member();

			$groups = DataObject::get('Group', sprintf(
				'"ChargifyProductID" = %d', $subscription->product->id
			));

			$result = null;

			if ($groups) foreach ($groups as $group) {
{'fraction_non_alphanumeric': 0.15143246930422918, 'fraction_numerical': 0.00218281036834925, 'mean_word_length': 3.8173455978975035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, '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': '664535', 'n_tokens_mistral': 1293, 'n_tokens_neox': 1141, 'n_words': 302}