Rewrite UserFlag entity to Doctrine

[MAILPOET-2014]
This commit is contained in:
Jan Jakeš
2019-07-04 11:24:06 +02:00
committed by M. Shull
parent 7105c8ad5b
commit c71bf4833c
9 changed files with 212 additions and 141 deletions

View File

@@ -1,26 +1,26 @@
<?php
namespace MailPoet\Settings;
use MailPoet\Models\UserFlag;
use MailPoet\Doctrine\Entities\UserFlag;
use MailPoet\WP\Functions as WPFunctions;
class UserFlagsController {
/**
* @var array|null
*/
/** @var array|null */
private $data = null;
/**
* @var array
*/
/** @var array */
private $defaults;
function __construct() {
/** @var UserFlagsRepository */
private $user_flags_repository;
function __construct(UserFlagsRepository $user_flags_repository) {
$this->defaults = [
'last_announcement_seen' => false,
'editor_tutorial_seen' => false,
];
$this->user_flags_repository = $user_flags_repository;
}
function get($name) {
@@ -38,11 +38,20 @@ class UserFlagsController {
function set($name, $value) {
$current_user_id = WPFunctions::get()->getCurrentUserId();
UserFlag::createOrUpdate([
$flag = $this->user_flags_repository->findOneBy([
'user_id' => $current_user_id,
'name' => $name,
'value' => $value,
]);
if (!$flag) {
$flag = new UserFlag();
$flag->setUserId($current_user_id);
$flag->setName($name);
$this->user_flags_repository->persist($flag);
}
$flag->setValue($value);
$this->user_flags_repository->flush();
if ($this->isLoaded()) {
$this->data[$name] = $value;
}
@@ -50,9 +59,18 @@ class UserFlagsController {
function delete($name) {
$current_user_id = WPFunctions::get()->getCurrentUserId();
UserFlag::where('user_id', $current_user_id)
->where('name', $name)
->deleteMany();
$flag = $this->user_flags_repository->findOneBy([
'user_id' => $current_user_id,
'name' => $name,
]);
if (!$flag) {
return;
}
$this->user_flags_repository->remove($flag);
$this->user_flags_repository->flush();
if ($this->isLoaded()) {
unset($this->data[$name]);
}
@@ -60,10 +78,10 @@ class UserFlagsController {
private function load() {
$current_user_id = WPFunctions::get()->getCurrentUserId();
$flags = UserFlag::where('user_id', $current_user_id)->findMany();
$flags = $this->user_flags_repository->findBy(['user_id' => $current_user_id]);
$this->data = [];
foreach ($flags as $flag) {
$this->data[$flag->name] = $flag->value;
$this->data[$flag->getName()] = $flag->getValue();
}
}
@@ -76,4 +94,4 @@ class UserFlagsController {
$this->load();
}
}
}
}