2017 © Pedro Peláez
 

symfony-bundle subscription-bundle

Symfony2 Bundle for managing subscriptions.

image

gremo/subscription-bundle

Symfony2 Bundle for managing subscriptions.

  • Friday, December 14, 2012
  • by gremo
  • Repository
  • 2 Watchers
  • 5 Stars
  • 24 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

GremoSubscriptionBundle Build Status

Symfony2 Bundle for managing subscriptions., (*1)

Installation

Add the following to your deps file (for Symfony 2.0.*):, (*2)

[GremoSubscriptionBundle]
    git=https://github.com/gremo/GremoSubscriptionBundle.git
    target=bundles/Gremo/SubscriptionBundle

Then register the namespaces with the autoloader (app/autoload.php):, (*3)

$loader->registerNamespaces(array(
    // ...
    'Gremo' => __DIR__.'/../vendor/bundles',
    // ...
));

Or, if you are using Composer and Symfony 2.1.*, add to composer.json file:, (*4)

{
    "require": {
        "gremo/subscription-bundle": "*"
    }
}

Finally register the bundle with your kernel in app/appKernel.php:, (*5)

public function registerBundles()
{
    $bundles = array(
        // ...
        new Gremo\SubscriptionBundle\GremoSubscriptionBundle(),
        // ...
    );

    // ...
}

Configuration

Bundle configuration is simple: you first need to specify an interval for the subscription periods. Use the format of \DateInterval interval specification. For example, P30D, that is 30 days. Interval should be, at least, one day long., (*6)

Then implement Gremo\SubscriptionBundle\Provider\ActivationDateProviderInterface, in order to provide an activation date. Make this class a service and set its name as the activation_provider in the configuration:, (*7)

gremo_subscription:
    interval: P30D
    activation_provider: my_activation_provider

Activation date provider example

An example activation date provider, where activation date is the current logged user creation date:, (*8)

use Gremo\SubscriptionBundle\Provider\ActivationDateProviderInterface;
use JMS\DiExtraBundle\Annotation as DI;
use Symfony\Component\Security\Core\SecurityContext;

/**
 * @DI\Service("my_activation_provider")
 */
class MyActivationProvider implements ActivationDateProviderInterface
{
    /**
     * @var \Symfony\Component\Security\Core\SecurityContext
     */
    private $context;

    /**
     * @DI\InjectParams({"context" = @DI\Inject("security.context")})
     */
    public function __construct(SecurityContext $context)
    {
        $this->context = $context;
    }

    /**
     * @return \DateTime
     */
    public function getActivationDate()
    {
        $user = $this->context->getToken()->getUser();

        return $user->getCreatedAt();
    }
}

Usage

You can access the subscription service using the service container, for example in your controller code:, (*9)

$subscription = $this->get('gremo_subscription'):

Say that today is 2012-12-12, interval is 30 days and activation date is 2012-09-01. Periods will be: - From 2012-09-01 to 2012-09-30 inclusive, the first period - From 2012-10-01 to 2012-10-30 inclusive - From 2012-10-31 to 2012-11-29 inclusive - From 2012-11-30 to 2012-12-29 inclusive, that is the current period, (*10)

Access the current period from subscription:, (*11)

$currentPeriod = $subscription->getCurrentPeriod(); // Period from 2012-11-30 to 2012-12-29

$firstDate = $currentPeriod->getFirstDate(); // DateTime object (2012-11-30)
$lastDate  = $currentPeriod->getLastDate();  // DateTime object (2012-12-29)

Class BaseSubscription implements Countable, ArrayAccess, Iterator PHP interfaces, so you can easly count, access and loop over each period. BaseSubscriptionPeriod inherits from PHP DatePeriod object, allowing to loop over each day of the period:, (*12)

// Get periods count
$numPeriods = count($subscription); // 4

// Get the previous period
$previusPeriod = $subscription[$numPeriods - 1]; // Period from 2012-10-31 to 2012-11-29

// Loop over each day of the previous period
foreach($previusPeriod as date)
{
    // ...
}

// Loop over each period
foreach($subscription as $period)
{
    // ...
}

// Find out a period for the given date (may return null)
$period = $subscription->getPeriod(new \DateTime('2012-11-25')); // Period form 2012-10-01 to 2012-10-30

The Versions

14/12 2012

dev-master

9999999-dev

Symfony2 Bundle for managing subscriptions.

  Sources   Download

MIT

The Requires

 

subscription recurrences

14/12 2012

v1.0.0

1.0.0.0

Symfony2 Bundle for managing subscriptions.

  Sources   Download

MIT

The Requires

 

subscription recurrences