Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade the way a free pack is created in countable #59

Open
2 tasks done
Aerendir opened this issue Sep 4, 2020 · 0 comments
Open
2 tasks done

Upgrade the way a free pack is created in countable #59

Aerendir opened this issue Sep 4, 2020 · 0 comments

Comments

@Aerendir
Copy link
Owner

Aerendir commented Sep 4, 2020

Currently it is possible to set a free plan with this config:

shq_features:
    ...
    sets:
       ....
            features:
                feature_name:
                    ...
                    packs:
                        50: ~
                        ...

This causes an empty array in the config that causes a lot of troubles in the code because we have to deal with an empty array.

What to do

Do not allow an empty configuration. Instead, require an explicit configuration set to 0:

shq_features:
    ...
    sets:
       ....
            features:
                feature_name:
                    ...
                    packs:
                        50:
                            EUR:
                                monthly: 0
                                yearly: 0
                        ...

v0.12.0

  • SerendipityHQ\Bundle\FeaturesBundle\DependencyInjection\Configuration not allow an empty config
  • Revert changes made in c7b854f

Configuration.php

from

    private function validateRecurringPrice(string $set, string $feature, array $price): void
    {
        // If emmpty, may be because it doesn't exist and the TreeBuilder created it as an empty array, else...
        if (false === empty($price)) {
            // ... It contains Currency codes: validate each one of them and their subscription periods
            foreach ($price as $currency => $subscriptions) {
                // Validate the currency
                $this->validateCurrency($set, $feature, $currency);

                // Validate the subscription periods
                $this->validateSubscriptionPeriods($set, $feature, $currency, $subscriptions);
            }
        }
    }

to

    private function validateRecurringPrice(string $set, string $feature, array $price): void
    {
        if (empty($price)) {
            throw new InvalidConfigurationException(\Safe\sprintf("You MUST set a price for %s.features.%s.", $set, $feature));
        }

        // ... It contains Currency codes: validate each one of them and their subscription periods
        foreach ($price as $currency => $subscriptions) {
            // Validate the currency
            $this->validateCurrency($set, $feature, $currency);

            // Validate the subscription periods
            $this->validateSubscriptionPeriods($set, $feature, $currency, $subscriptions);
        }
    }

from

    private function processPackages(array $packs, $subscriptionType)
    {
        $subscriptionHasFreePackage = false;
        foreach ($packs as $numOfUnits => $prices) {
            switch ($subscriptionType) {
                case self::RECURRING:
                    $packs[$numOfUnits] = $this->processRecurringPrice($prices);

                    // If this is a free package
                    if (empty($prices)) {
                        // We have a free package so we haven't to create it
                        $subscriptionHasFreePackage = true;
                    }
                    break;
                case self::UNATANTUM:
                    $packs[$numOfUnits] = $this->processUnatantumPrice($prices);
                    break;
            }
        }

        // If we are processing a recurring feature that hasn't a free package...
        if (self::RECURRING === $subscriptionType && false === $subscriptionHasFreePackage) {
            // ... We have to create it with 0 $numOfUnits as we always need a free package for a subscribed feature
            $packs[0] = [];
        }

        $packs['_pricesType'] = $this->pricesType;

        return $packs;

to

    private function processPackages(array $packs, $subscriptionType)
    {
        $subscriptionHasFreePackage = false;
        foreach ($packs as $numOfUnits => $prices) {
            switch ($subscriptionType) {
                case self::RECURRING:
                    $packs[$numOfUnits] = $this->processRecurringPrice($prices);

                    // If this is a free package
                    if (empty($prices)) {
                        // We have a free package so we haven't to create it
                        $subscriptionHasFreePackage = true;
                    }
                    break;
                case self::UNATANTUM:
                    $packs[$numOfUnits] = $this->processUnatantumPrice($prices);
                    break;
            }
        }

        $packs['_pricesType'] = $this->pricesType;

        return $packs;

Also, understand why we always need a free package for subscribed features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant