flagception/contentful-activator

Activator for manage feature toggles by contentful

1.0.0 2018-01-13 23:04 UTC

This package is auto-updated.

Last update: 2024-11-25 22:56:39 UTC


README

Manage feature flags for Flagception with Contentful!

Latest Stable Version Coverage Status Build Status Total Downloads License

SensioLabsInsight

Download the library

Open a command console, enter your project directory and execute the following command to download the latest stable version of this library:

$ composer require flagception/contentful-activator

Usage

Just create a new ContentfulActivator instance and commit it to your feature manager:

// YourClass.php

class YourClass
{
    public function run()
    {
        // We need two arguments:
        //  1. A contentful client
        //  2. The content type name in contentful
        $activator = new ContentfulActivator($this->client, 'FeatureToggle');
        
        $manager = new FeatureManager($activator);
        if ($manager->isActive('your_feature_name')) {
            // do something
        }
    }
}

The ContentfulActivator need three arguments:

  • An instance of a contentful client
  • The contentful model type id as string
  • The field mappings as array ('name' and 'state') - Optional (default values are set)

If your contentful model looks like this ...

{
  "name": "Feature Management",
  "description": "Features verwalten",
  "displayField": "featureName",
  "fields": [
    {
      "id": "featureName",
      "name": "Feature",
      "type": "Text",
      "localized": false,
      "required": true,
      "validations": [],
      "disabled": false,
      "omitted": false
    },
    {
      "id": "isActive",
      "name": "Aktiv",
      "type": "Boolean",
      "localized": false,
      "required": true,
      "validations": [],
      "disabled": false,
      "omitted": false
    }
  ],
  "sys": {
    "space": {
      "sys": {
        "type": "Link",
        "linkType": "Space",
        "id": "9d8smn39"
      }
    },
    "id": "myFeatureModel",
    "type": "ContentType",
    "createdAt": "2017-12-07T15:54:07.255Z",
    "updatedAt": "2018-01-11T16:08:47.283Z",
    //...
  }
}

... then your activator instance should be like this:

// YourClass.php

class YourClass
{
    public function run()
    {
        // "myFeatureModel" is the content model type
        $activator = new ContentfulActivator($this->client, 'myFeatureModel', [
            'name' => 'featureName', // Field name for feature key
            'state' => 'isActive' // Field name for feature state
        ]);
        
        $manager = new FeatureManager($activator);
        if ($manager->isActive('your_feature_name')) {
            // do something
        }
    }
}

You can skip the field mapping (like the first example) if you use the default field names in contentful:

  • 'state' for the feature state field
  • 'name' for the feature key field