Commit af51a4cb authored by Emiliano Balbuena's avatar Emiliano Balbuena

(feat): Pro settings

1 merge request!281WIP: (feat): Minds Pro
Pipeline #72975277 passed with stages
in 9 minutes and 7 seconds
......@@ -328,4 +328,13 @@ class Response implements \Iterator, \ArrayAccess, \Countable, \JsonSerializable
{
return array_reduce($this->data, $callback, $initialValue);
}
/**
* Returns the first element of the Response, or null if empty
* @return mixed|null
*/
public function first()
{
return $this->data[0] ?? null;
}
}
......@@ -86,6 +86,19 @@ class channel implements Interfaces\Api
$block = Core\Security\ACL\Block::_();
$response['channel']['blocked'] = $block->isBlocked($user);
if ($user->isPro()) {
/** @var Core\Pro\Manager $manager */
$manager = Core\Di\Di::_()->get('Pro\Manager');
$manager
->setUser($user);
$proSettings = $manager->get();
if ($proSettings) {
$response['channel']['pro_settings'] = $proSettings;
}
}
return Factory::response($response);
}
......
......@@ -30,7 +30,6 @@ class pro implements Interfaces\Api
return Factory::response([
'isActive' => $manager->isActive(),
/* TODO: Send values */
]);
}
......@@ -46,26 +45,20 @@ class pro implements Interfaces\Api
$manager
->setUser(Session::getLoggedinUser());
switch ($pages[0]) {
case 'enable':
// TODO: Send and process payment data
$success = $manager->enable(time() + (365 * 86400));
// TODO: Send and process payment data
$success = $manager->enable(time() + (365 * 86400));
if (!$success) {
return Factory::response([
'status' => 'error',
'message' => 'Error activating Pro',
]);
}
return Factory::response([/* TODO: Send values */]);
default:
return Factory::response([
'status' => 'error',
'message' => 'Unknown method'
]);
if (!$success) {
return Factory::response([
'status' => 'error',
'message' => 'Error activating Pro',
]);
}
return Factory::response([
'isActive' => $manager->isActive(),
'settings' => $manager->get(),
]);
}
/**
......
<?php
/**
* settings
* @author edgebal
*/
namespace Minds\Controllers\api\v2\pro;
use Exception;
use Minds\Core\Di\Di;
use Minds\Core\Pro\Manager;
use Minds\Core\Session;
use Minds\Interfaces;
use Minds\Api\Factory;
class settings implements Interfaces\Api
{
/**
* Equivalent to HTTP GET method
* @param array $pages
* @return mixed|null
*/
public function get($pages)
{
/** @var Manager $manager */
$manager = Di::_()->get('Pro\Manager');
$manager
->setUser(Session::getLoggedinUser());
return Factory::response([
'isActive' => $manager->isActive(),
'settings' => $manager->get(),
]);
}
/**
* Equivalent to HTTP POST method
* @param array $pages
* @return mixed|null
*/
public function post($pages)
{
/** @var Manager $manager */
$manager = Di::_()->get('Pro\Manager');
$manager
->setUser(Session::getLoggedinUser());
if (!$manager->isActive()) {
return Factory::response([
'status' => 'error',
'message' => 'You are not Pro',
]);
}
try {
$success = $manager->set($_POST);
if (!$success) {
throw new Exception('Cannot save Pro settings');
}
} catch (\Exception $e) {
return Factory::response([
'status' => 'error',
'message' => $e->getMessage(),
]);
}
return Factory::response([]);
}
/**
* Equivalent to HTTP PUT method
* @param array $pages
* @return mixed|null
*/
public function put($pages)
{
return Factory::response([]);
}
/**
* Equivalent to HTTP DELETE method
* @param array $pages
* @return mixed|null
*/
public function delete($pages)
{
return Factory::response([]);
}
}
<?php
/**
* InitializeValuesDelegate
* InitializeSettingsDelegate
* @author edgebal
*/
......@@ -8,16 +8,16 @@ namespace Minds\Core\Pro\Delegates;
use Exception;
use Minds\Core\Pro\Repository;
use Minds\Core\Pro\Values;
use Minds\Core\Pro\Settings;
use Minds\Entities\User;
class InitializeValuesDelegate
class InitializeSettingsDelegate
{
/** @var Repository */
protected $repository;
/**
* InitializeValuesDelegate constructor.
* InitializeSettingsDelegate constructor.
* @param Repository $repository
*/
public function __construct(
......@@ -33,21 +33,26 @@ class InitializeValuesDelegate
*/
public function onEnable(User $user)
{
$values = $this->repository
/** @var Settings|null $settings */
$settings = $this->repository
->getList(['user_guid' => $user->guid])
->toArray()[0] ?? null;
->first();
if (!$values) {
$values = new Values();
$values
if (!$settings) {
$settings = new Settings();
$settings
->setUserGuid($user->guid);
}
if (!$values->getDomain()) {
$values->setDomain("pro-{$user->guid}.minds.com");
if (!$settings->getDomain()) {
$settings->setDomain("pro-{$user->guid}.minds.com");
}
if (!$settings->getTitle()) {
$settings->setTitle($user->name ?: $user->username);
}
$this->repository
->add($values);
->add($settings);
}
}
......@@ -12,27 +12,33 @@ use Minds\Entities\User;
class Manager
{
/** @var Repository */
protected $repository;
/** @var Save */
protected $saveAction;
/** @var Delegates\InitializeValuesDelegate */
protected $initializeValuesDelegate;
/** @var Delegates\InitializeSettingsDelegate */
protected $initializeSettingsDelegate;
/** @var User */
protected $user;
/**
* Manager constructor.
* @param Repository $repository
* @param Save $saveAction
* @param Delegates\InitializeValuesDelegate $initializeValuesDelegate
* @param Delegates\InitializeSettingsDelegate $initializeSettingsDelegate
*/
public function __construct(
$repository = null,
$saveAction = null,
$initializeValuesDelegate = null
$initializeSettingsDelegate = null
)
{
$this->repository = $repository ?: new Repository();
$this->saveAction = $saveAction ?: new Save();
$this->initializeValuesDelegate = $initializeValuesDelegate ?: new Delegates\InitializeValuesDelegate();
$this->initializeSettingsDelegate = $initializeSettingsDelegate ?: new Delegates\InitializeSettingsDelegate();
}
/**
......@@ -76,7 +82,7 @@ class Manager
->setEntity($this->user)
->save();
$this->initializeValuesDelegate
$this->initializeSettingsDelegate
->onEnable($this->user);
return (bool) $saved;
......@@ -103,4 +109,80 @@ class Manager
return (bool) $saved;
}
/**
* @return Settings|null
* @throws Exception
*/
public function get()
{
if (!$this->user) {
throw new Exception('Invalid user');
}
return $this->repository->getList([
'user_guid' => $this->user->guid
])->first();
}
/**
* @param array $settings
* @return bool
* @throws Exception
*/
public function set(array $settings = [])
{
if (!$this->user) {
throw new Exception('Invalid user');
}
$settings = $this->get() ?: new Settings();
$settings
->setUserGuid($this->user->guid);
if (isset($settings['domain'])) {
// TODO: Validate!
$settings
->setDomain($settings['domain']);
}
if (isset($settings['title'])) {
// TODO: Validate!
$settings
->setTitle($settings['title']);
}
if (isset($settings['headline'])) {
// TODO: Validate!
$settings
->setHeadline($settings['headline']);
}
if (isset($settings['text_color'])) {
// TODO: Validate!
$settings
->setTextColor($settings['text_color']);
}
if (isset($settings['primary_color'])) {
// TODO: Validate!
$settings
->setPrimaryColor($settings['primary_color']);
}
if (isset($settings['plain_background_color'])) {
// TODO: Validate!
$settings
->setPlainBackgroundColor($settings['plain_background_color']);
}
return $this->repository->update($settings);
}
}
......@@ -81,15 +81,20 @@ class Repository
if ($rows) {
foreach ($rows as $row) {
$valuesEntity = new Values();
$valuesEntity
$settings = new Settings();
$settings
->setUserGuid($row['user_guid']->toInt())
->setDomain($row['domain']);
$valuesEntityJsonData = json_decode($row['json_data'] ?: '{}', true);
// TODO: Set entity data
$data = json_decode($row['json_data'] ?: '{}', true);
$settings
->setTitle($data['title'] ?? '')
->setHeadline($data['headline'] ?? '')
->setTextColor($data['text_color'] ?? '')
->setPrimaryColor($data['primary_color'] ?? '')
->setPlainBackgroundColor($data['plain_background_color'] ?? '');
$response[] = $valuesEntity;
$response[] = $settings;
}
$response
......@@ -105,61 +110,65 @@ class Repository
}
/**
* @param Values $values
* @param Settings $settings
* @return bool
* @throws Exception
*/
public function add(Values $values)
public function add(Settings $settings)
{
if (!$values->getUserGuid()) {
if (!$settings->getUserGuid()) {
throw new Exception('Invalid user GUID');
}
$cql = "INSERT INTO pro (user_guid, domain, json_data) VALUES (?, ?, ?)";
$values = [
new Bigint($values->getUserGuid()),
$values->getDomain(),
$settings = [
new Bigint($settings->getUserGuid()),
$settings->getDomain(),
json_encode([
'user_guid' => (string) $values->getUserGuid(),
'domain' => $values->getDomain(),
// TODO: Set entity data
'user_guid' => (string) $settings->getUserGuid(),
'domain' => $settings->getDomain(),
'title' => $settings->getTitle(),
'headline' => $settings->getHeadline(),
'text_color' => $settings->getTextColor(),
'primary_color' => $settings->getPrimaryColor(),
'plain_background_color' => $settings->getPlainBackgroundColor(),
]),
];
$prepared = new Custom();
$prepared->query($cql, $values);
$prepared->query($cql, $settings);
return (bool) $this->db->request($prepared, true);
}
/**
* @param Values $values
* @param Settings $settings
* @return bool
* @throws Exception
*/
public function update(Values $values)
public function update(Settings $settings)
{
return $this->add($values);
return $this->add($settings);
}
/**
* @param Values $values
* @param Settings $settingsRef
* @return bool
* @throws Exception
*/
public function delete(Values $values)
public function delete(Settings $settingsRef)
{
if (!$values->getUserGuid()) {
if (!$settingsRef->getUserGuid()) {
throw new Exception('Invalid user GUID');
}
$cql = "DELETE FROM pro WHERE user_guid = ?";
$values = [
new Bigint($values->getUserGuid()),
$settingsRef = [
new Bigint($settingsRef->getUserGuid()),
];
$prepared = new Custom();
$prepared->query($cql, $values);
$prepared->query($cql, $settingsRef);
return (bool) $this->db->request($prepared, true);
}
......
<?php
/**
* Values
* Settings
* @author edgebal
*/
......@@ -10,14 +10,24 @@ use JsonSerializable;
use Minds\Traits\MagicAttributes;
/**
* Class Values
* Class Settings
* @package Minds\Core\Pro
* @method int|string getUserGuid()
* @method Values setUserGuid(int|string $userGuid)
* @method Settings setUserGuid(int|string $userGuid)
* @method string getDomain()
* @method Values setDomain(string $domain)
* @method Settings setDomain(string $domain)
* @method string getTitle()
* @method Settings setTitle(string $title)
* @method string getHeadline()
* @method Settings setHeadline(string $headline)
* @method string getTextColor()
* @method Settings setTextColor(string $textColor)
* @method string getPrimaryColor()
* @method Settings setPrimaryColor(string $primaryColor)
* @method string getPlainBackgroundColor()
* @method Settings setPlainBackgroundColor(string $plainBackgroundColor)
*/
class Values implements JsonSerializable
class Settings implements JsonSerializable
{
use MagicAttributes;
......@@ -27,14 +37,34 @@ class Values implements JsonSerializable
/** @var string */
protected $domain;
/** @var string */
protected $title;
/** @var string */
protected $headline;
/** @var string */
protected $textColor;
/** @var string */
protected $primaryColor;
/** @var string */
protected $plainBackgroundColor;
/**
* @return array
*/
public function export()
{
return [
'user_guid' => $this->userGuid,
'user_guid' => (string) $this->userGuid,
'domain' => $this->domain,
'title' => $this->title,
'headline' => $this->headline,
'text_color' => $this->textColor,
'primary_color' => $this->primaryColor,
'plain_background_color' => $this->plainBackgroundColor,
];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment