Use Doctrine to get all User Flags

[MAILPOET-2219]
This commit is contained in:
Pavel Dohnal
2019-08-21 09:22:03 +02:00
committed by M. Shull
parent 513e4c8542
commit 8e65d4b6b2
3 changed files with 21 additions and 30 deletions

View File

@ -2,7 +2,7 @@
namespace MailPoet\Features;
use function MailPoet\Util\array_column;
use MailPoet\Entities\FeatureFlagEntity;
class FeatureFlagsController {
@ -26,14 +26,22 @@ class FeatureFlagsController {
}
function getAll() {
$flags = FeatureFlag::findArray();
$flagsMap = array_combine(array_column($flags, 'name'), $flags);
$flags = $this->feature_flags_repository->findAll();
$flagsMap = array_combine(
array_map(
function (FeatureFlagEntity $flag) {
return $flag->getName();
},
$flags
),
$flags
);
$output = [];
foreach ($this->features_controller->getDefaults() as $name => $default) {
$output[] = [
'name' => $name,
'value' => isset($flagsMap[$name]) ? (bool)$flagsMap[$name]['value'] : $default,
'value' => isset($flagsMap[$name]) ? (bool)$flagsMap[$name]->getValue() : $default,
'default' => $default,
];
}

View File

@ -2,8 +2,6 @@
namespace MailPoet\Features;
use MailPoet\Models\FeatureFlag;
class FeaturesController {
// Define features below in the following form:
@ -19,6 +17,13 @@ class FeaturesController {
/** @var array */
private $flags;
/** @var FeatureFlagsRepository */
private $feature_flags_repository;
public function __construct(FeatureFlagsRepository $feature_flags_repository) {
$this->feature_flags_repository = $feature_flags_repository;
}
/** @return bool */
function isSupported($feature) {
if (!$this->exists($feature)) {
@ -57,10 +62,10 @@ class FeaturesController {
}
private function getValueMap() {
$features = FeatureFlag::selectMany(['name', 'value'])->findMany();
$features = $this->feature_flags_repository->findAll();
$featuresMap = [];
foreach ($features as $feature) {
$featuresMap[$feature->name] = (bool)$feature->value;
$featuresMap[$feature->getName()] = (bool)$feature->getValue();
}
return $featuresMap;
}

View File

@ -1,22 +0,0 @@
<?php
namespace MailPoet\Models;
if (!defined('ABSPATH')) exit;
/**
* @property string $name
* @property bool $value
*/
class FeatureFlag extends Model {
public static $_table = MP_FEATURE_FLAGS_TABLE;
static function createOrUpdate($data = []) {
$keys = false;
if (isset($data['name'])) {
$keys = [
'name' => $data['name'],
];
}
return parent::_createOrUpdate($data, $keys);
}
}