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 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)); }
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)); }