Skip to content

Commit

Permalink
feat(System): configuration option firebase to store data
Browse files Browse the repository at this point in the history
- composer require kreait/laravel-firebase

- php artisan route:cache

see #34
  • Loading branch information
tuannt committed Nov 12, 2020
1 parent 0ae1874 commit 0f1404e
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 38 deletions.
41 changes: 33 additions & 8 deletions src/Helpers/Config.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use GGPHP\Config\Models\GGConfig;
use GGPHP\Config\Services\FirebaseService;

/**
* This function use to get config info by code
Expand All @@ -24,17 +25,21 @@ function getConfigByCode($code)
* @param code field $code
* @return object
*/
if (! function_exists('getDefaultValue')) {
function getDefaultValue($code)
if (! function_exists('getDefaultValueByCode')) {
function getDefaultValueByCode($code)
{
$fields = config('config.system');
$configs = config('config.system');
$default = null;

foreach ($fields as $field) {
if ($field['code'] === $code && isset($field['default'])) {
$default = $field['default'];
foreach ($configs as $config) {
if ($config['key'] == 'configuration.system.fields') {
foreach ($config['fields'] as $field) {
if ($field['code'] === $code && isset($field['default'])) {
$default = $field['default'];

break;
break;
}
}
}
}

Expand All @@ -56,4 +61,24 @@ function getThrottle($routeName)
return ! empty($throttle) ? json_decode($throttle->value, true) : [];
}
}
?>

/**
* This function use to get key data of firebase info by code
* @param code field $code
* @return string
*/
if (! function_exists('getKeyByCode')) {
function getKeyByCode($code)
{
$firebaseService = new FirebaseService;
$reference = $firebaseService->getRetrievingData(GGConfig::REFERENCE);
$data = $reference->getValue() ?? [];

foreach($data as $key => $value) {
if ($value['code'] == $code)
return $key;
}

return false;
}
}
49 changes: 22 additions & 27 deletions src/Http/Controllers/ConfigController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
use GGPHP\Config\Models\GGConfig;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use GGPHP\Config\Services\FirebaseService;

class ConfigController extends Controller
{
/**
* Show the view for the configuration fields
*
* @return \Illuminate\View\View
*/
public function edit()
Expand All @@ -21,14 +21,12 @@ public function edit()

/**
* Update the configuration fields.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\View\View
*/
public function update(Request $request)
{
$data = $request->except(['_method', '_token']);

$configs = config('config.system');
$rules = $booleans = $types = [];

Expand All @@ -52,26 +50,7 @@ public function update(Request $request)
if ($validator->fails())
return view('ggphp-config::config')->with('errors', $validator->errors());

// Default is false if boolean field empty
foreach ($booleans as $value)
if (! in_array($value, array_keys($data)))
$data[$value] = false;

foreach ($data as $code => $value) {
$configInfo = getConfigByCode($code);

if ($configInfo) {
$configInfo->value = $value ?? '';
$configInfo->save();
} else {
GGConfig::create([
'code' => $code,
'value' => $value ?? '',
'type' => $types[$code] ?? '',
'default' => $value ?? '',
]);
}
}
GGConfig::updateConfigs($data, $booleans, $types);

$request->session()->flash('message', 'The update was successful!');

Expand All @@ -80,15 +59,31 @@ public function update(Request $request)

/**
* Reset the fields to default
*
* @return \Illuminate\Http\Response
*/
public function reset()
{
$configs = GGConfig::get(['code', 'type', 'default']);
$configs = [];

if (env('STORE_DB', 'database') == 'database') {
$configs = GGConfig::get(['code', 'type', 'default']);

if (empty($configs))
return response()->json(['data' => null], 400);
if (empty($configs))
return response()->json(['data' => null], 400);

} elseif (env('STORE_DB') == 'firebase') {
$firebaseService = new FirebaseService;
$reference = $firebaseService->getRetrievingData(GGConfig::REFERENCE);
$data = $reference->getValue() ?? [];

foreach($data as $value) {
$configs[] = [
'code' => $value['code'],
'type' => $value['type'],
'default' => $value['default']
];
}
}

return response()->json(['data' => $configs], 200);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
Route::prefix('configuration')->group(function () {
// Field Routes
Route::prefix('field')->group(function () {
Route::get('edit', 'ConfigController@edit')->name('config.field.edit');
Route::patch('updates', 'ConfigController@update')->name('config.field.update');
Route::get('edit', 'ConfigController@edit')->name('config.field.edit');
Route::get('reset', 'ConfigController@reset')->name('config.field.reset');
});

Expand Down
52 changes: 52 additions & 0 deletions src/Models/GGConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GGPHP\Config\Models;

use Illuminate\Database\Eloquent\Model;
use GGPHP\Config\Services\FirebaseService;

class GGConfig extends Model
{
Expand All @@ -12,4 +13,55 @@ class GGConfig extends Model
// Define API throttle
public const MAX_ATTEMPTS_DEFAULT = 60;
public const DECAY_MINUTES_DEFAULT = 1;

// Define reference
public const REFERENCE = 'configuration/system/fields';

/**
* This function use to update data configs
*
* @param array $data
* @param array $booleans field with type booleans
* @param array $types input. Ex: text, select,...
* @return object
*/
public static function updateConfigs($data, $booleans, $types)
{
// Default is false if boolean field empty
foreach ($booleans as $value)
if (! in_array($value, array_keys($data)))
$data[$value] = false;

foreach ($data as $code => $value) {
$attributes = [
'code' => $code,
'value' => $value ?? '',
'type' => $types[$code] ?? '',
'default' => getDefaultValueByCode($code) ?? ''
];

if (env('STORE_DB', 'database') == 'database') {
$configInfo = getConfigByCode($code);

if ($configInfo) {
$configInfo->value = $value ?? '';
$configInfo->save();
} else {
GGConfig::create($attributes);
}
} elseif (env('STORE_DB') == 'firebase') {
$firebaseService = new FirebaseService;
$configInfo = $firebaseService->getDataByCode($code);

if (empty($configInfo)) {
$firebaseService->addData(GGConfig::REFERENCE, $attributes);
} else {
if (getKeyByCode($code))
$firebaseService->setData(GGConfig::REFERENCE . '/' . getKeyByCode($code), $attributes);
}
}
}

return true;
}
}
6 changes: 4 additions & 2 deletions src/Resources/views/config.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@

@foreach ($data['fields'] as $field)
@php
$config = getConfigByCode($field['code']);
$value = $config ? $config->value : null;
$config = env('STORE_DB', 'database') == 'database'
? getConfigByCode($field['code'])
: app('GGPHP\Config\Services\FirebaseService')->getDataByCode($field['code']);
$value = $config ? $config['value'] : null;
@endphp

<div class="row">
Expand Down
70 changes: 70 additions & 0 deletions src/Services/FirebaseService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace GGPHP\Config\Services;

use GGPHP\Config\Models\GGConfig;

class FirebaseService {

protected $database;

public function __construct()
{
$this->database = app('firebase.database');
}

/**
* This function use to get reference
* @param string $reference
* @return object
*/
public function getRetrievingData($reference = null)
{
return $reference ? $this->database->getReference($reference) : $this->database->getReference();
}

/**
* This function use to add data in firebase
* @param string $reference
* @param object $data
* @return object
*/
public function addData($reference = null, $data)
{
$firebase = $this->getRetrievingData($reference);

return $firebase->push($data);
}

/**
* This function use to set data in firebase
* @param string $reference
* @param object $data
* @return object
*/
public function setData($reference = null, $data)
{
$firebase = $this->getRetrievingData($reference);

return $firebase->set($data);
}

/**
* This function use to get data config info by code
* @param code field $code
* @return object
*/
public function getDataByCode($code)
{
$reference = $this->getRetrievingData(GGConfig::REFERENCE);
$data = $reference->getValue() ?? [];
$configs = [];

foreach($data as $value) {
if (isset($value['code']) && $value['code'] == $code)
$configs = $value;
}

return $configs;
}
}

0 comments on commit 0f1404e

Please sign in to comment.