Exemplo n.º 1
0
 public function active($featureName = null, $attributes = [])
 {
     if (!$featureName) {
         throw new QandidateException("Feature name must not be empty");
     }
     if (!is_array($attributes)) {
         throw new QandidateException("Attribute must be an instance of array");
     }
     $this->getToggleConditions($featureName);
     $conditionCollection = [];
     foreach ($this->conditions as $condition) {
         $class = $this->convertStringToClass($condition->operator);
         if ('Qandidate\\Toggle\\Operator\\InSet' === $class) {
             $value = [$condition->value];
         } else {
             $value = $condition->value;
         }
         $operator = new $class($value);
         $conditionCollection[] = new OperatorCondition($condition->key, $operator);
     }
     $toggle = new Toggle('toggling', $conditionCollection);
     $constName = $this->convertStringToConstant($this->toggle->status);
     if ('INACTIVE' === $constName) {
         $toggle->deactivate();
     } else {
         $toggle->activate(constant('Qandidate\\Toggle\\Toggle::' . $constName));
     }
     $this->manager->add($toggle);
     $context = new Context();
     foreach ($attributes as $key => $value) {
         $context->set($key, $value);
     }
     return $this->manager->active('toggling', $context);
 }
 private function createToggle($name, $active)
 {
     $toggle = new Toggle($name, array());
     if ($active) {
         $toggle->activate(Toggle::ALWAYS_ACTIVE);
     } else {
         $toggle->deactivate();
     }
     return $toggle;
 }
 private function deserializeStatus(Toggle $toggle, $status)
 {
     switch ($status) {
         case 'always-active':
             $toggle->activate(Toggle::ALWAYS_ACTIVE);
             return;
         case 'inactive':
             $toggle->deactivate();
             return;
         case 'conditionally-active':
             $toggle->activate(Toggle::CONDITIONALLY_ACTIVE);
             return;
     }
     throw new RuntimeException(sprintf('Unknown toggle status "%s".', $status));
 }
 private function createInactiveToggle()
 {
     $toggle = new Toggle('some-feature', array());
     $toggle->deactivate();
     return $toggle;
 }
 /**
  * @test
  * @dataProvider contextProvider
  */
 public function it_is_not_active_for_every_context_if_inactivated($context)
 {
     $conditions = array(new OperatorCondition('age', new GreaterThan(42)));
     $toggle = new Toggle('some-feature', $conditions);
     $toggle->deactivate();
     $this->assertFalse($toggle->activeFor($context));
 }
 /**
  * @param Toggle $toggle
  *
  * @return string
  */
 private function serializeStrategy(Toggle $toggle)
 {
     switch ($toggle->getStrategy()) {
         case Toggle::STRATEGY_AFFIRMATIVE:
             return 'affirmative';
         case Toggle::STRATEGY_MAJORITY:
             return 'majority';
         case Toggle::STRATEGY_UNANIMOUS:
             return 'unanimous';
     }
 }
 private function createToggle($name, $status, array $conditions = array())
 {
     $toggle = new Toggle($name, $conditions);
     $toggle->activate($status);
     return $toggle;
 }
// true
// Create and check a new context for a user with id 41, age 23 and height 5.6
$context = new Context();
$context->set('user_id', 41);
$context->set('age', 23);
$context->set('height', 5.6);
var_dump($manager->active('toggling', $context));
// false
// Unanimous strategy
// A toggle that will be active if the user id is less than 42 and if the age is greater than 24
$conditions = [];
$operator = new LessThan(42);
$conditions[] = new OperatorCondition('user_id', $operator);
$operator = new GreaterThan(24);
$conditions[] = new OperatorCondition('age', $operator);
$toggle = new Toggle('toggling', $conditions, Toggle::STRATEGY_UNANIMOUS);
$toggle->activate(Toggle::CONDITIONALLY_ACTIVE);
// Add the toggle to the manager
$manager->add($toggle);
// Create and check a new context for a user with id 41 and age 25
$context = new Context();
$context->set('user_id', 41);
$context->set('age', 25);
var_dump($manager->active('toggling', $context));
// true
// Create and check a new context for a user with id 41 and age 23
$context = new Context();
$context->set('user_id', 41);
$context->set('age', 23);
var_dump($manager->active('toggling', $context));
// false