FeatureFlagsController.php
3 years ago
FeatureFlagsRepository.php
3 years ago
FeaturesController.php
1 month ago
index.php
3 years ago
FeatureFlagsController.php
57 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Features; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoet\Entities\FeatureFlagEntity; |
| 9 | |
| 10 | class FeatureFlagsController { |
| 11 | |
| 12 | /** @var FeaturesController */ |
| 13 | private $featuresController; |
| 14 | |
| 15 | /** @var FeatureFlagsRepository */ |
| 16 | private $featureFlagsRepository; |
| 17 | |
| 18 | public function __construct( |
| 19 | FeaturesController $featuresController, |
| 20 | FeatureFlagsRepository $featureFlagsRepository |
| 21 | ) { |
| 22 | $this->featuresController = $featuresController; |
| 23 | $this->featureFlagsRepository = $featureFlagsRepository; |
| 24 | } |
| 25 | |
| 26 | public function set($name, $value) { |
| 27 | if (!$this->featuresController->exists($name)) { |
| 28 | throw new \RuntimeException("Feature '$name' does not exist'"); |
| 29 | } |
| 30 | |
| 31 | $this->featureFlagsRepository->createOrUpdate(['name' => $name, 'value' => $value]); |
| 32 | } |
| 33 | |
| 34 | public function getAll() { |
| 35 | $flags = $this->featureFlagsRepository->findAll(); |
| 36 | $flagsMap = array_combine( |
| 37 | array_map( |
| 38 | function (FeatureFlagEntity $flag) { |
| 39 | return $flag->getName(); |
| 40 | }, |
| 41 | $flags |
| 42 | ), |
| 43 | $flags |
| 44 | ); |
| 45 | |
| 46 | $output = []; |
| 47 | foreach ($this->featuresController->getDefaults() as $name => $default) { |
| 48 | $output[] = [ |
| 49 | 'name' => $name, |
| 50 | 'value' => isset($flagsMap[$name]) ? (bool)$flagsMap[$name]->getValue() : $default, |
| 51 | 'default' => $default, |
| 52 | ]; |
| 53 | } |
| 54 | return $output; |
| 55 | } |
| 56 | } |
| 57 |