Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Blair2004 committed Jan 7, 2024
1 parent afbddd4 commit fd10003
Showing 1 changed file with 16 additions and 60 deletions.
76 changes: 16 additions & 60 deletions app/Services/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public function setDefault( $options = [] ): void
{
Option::truncate();

$types = app()->make( OrdersService::class )->getTypeLabels();

$defaultOptions = [
'ns_registration_enabled' => 'no',
'ns_store_name' => 'NexoPOS',
Expand Down Expand Up @@ -81,41 +79,14 @@ public function build()
if ( Helper::installed() && empty( $this->rawOptions ) ) {
$this->rawOptions = $this->option()
->get()
->mapWithKeys( function ( $option ) {
$option = $this->parseOption( $option );

->mapWithKeys( function( $option ) {
return [
$option->key => $option,
];
});
}
}

public function parseOption( $option )
{
/**
* We should'nt run this everytime we
* try to pull an option from the database or from the array
*/
if ( ! empty( $option->value ) ) {
if ( is_string( $option->value ) ) {
$json = json_decode( $option->value, true );

if ( json_last_error() == JSON_ERROR_NONE ) {
$option->value = $json;
}
} elseif ( ! is_array( $option->value ) ) {
$option->value = match ( $option->value ) {
preg_match( '/[0-9]{1,}/', $option->value ) => (int) $option->value,
preg_match( '/[0-9]{1,}\.[0-9]{1,}/', $option->value ) => (float) $option->value,
default => $option->value,
};
}
}

return $option;
}

/**
* Set Option
*
Expand All @@ -126,12 +97,6 @@ public function parseOption( $option )
**/
public function set( $key, $value, $expiration = null )
{
/**
* We rather like to remove unecessary spaces. That might
* cause unwanted behaviors.
*/
$key = trim( strtolower( $key ) );

/**
* if an option has been found,
* it will save the new value and update
Expand Down Expand Up @@ -171,33 +136,24 @@ public function set( $key, $value, $expiration = null )
$option->array = false;

$this->encodeOptionValue( $option, $value );
}

$option->key = $key;
$option->array = false;
$option->expire_on = $expiration;

if ( is_array( $value ) ) {
$option->value = json_encode( $value );
} elseif ( empty( $value ) && ! (bool) preg_match( '/[0-9]{1,}/', $value ) ) {
$option->value = '';
/**
* this should be overridable
* from a user option or any
* extending this class
*/
$option = $this->beforeSave( $option );
$option->save();
} else {
$option->value = $value;
$option = $foundOption->first();
}

$option->expire_on = $expiration;

/**
* this should be overridable
* from a user option or any
* extending this class
*/
$option = $this->beforeSave( $option );
$option->save();

/**
* Let's save the new option
*/
$this->rawOptions[ $key ] = $this->parseOption( $option );
$this->rawOptions[ $key ] = $option;

return $option;
}
Expand Down Expand Up @@ -240,7 +196,7 @@ public function get( string $key = null, mixed $default = null )
return $this->rawOptions;
}

$filtredOptions = collect( $this->rawOptions )->filter( function ( $option ) use ( $key ) {
$filtredOptions = collect( $this->rawOptions )->filter( function( $option ) use ( $key ) {
return is_array( $key ) ? in_array( $option->key, $key ) : $option->key === $key;
});

Expand All @@ -252,8 +208,8 @@ public function get( string $key = null, mixed $default = null )

return match ( $options->count() ) {
0 => $default,
1 => $filtredOptions->first()->value,
default => $filtredOptions->map( fn( $option ) => $option->value )->toArray()
1 => $options->first()->value,
default => $options->map( fn( $option ) => $option->value )->toArray()
};
}

Expand Down Expand Up @@ -289,7 +245,7 @@ public function decodeOptionValue( $option )
**/
public function delete( string $key ): void
{
$this->rawOptions = collect( $this->rawOptions )->filter( function ( Option $option ) use ( $key ) {
$this->rawOptions = collect( $this->rawOptions )->filter( function( Option $option ) use ( $key ) {
if ( $option->key === $key ) {
$option->delete();

Expand All @@ -299,4 +255,4 @@ public function delete( string $key ): void
return true;
});
}
}
}

0 comments on commit fd10003

Please sign in to comment.