Skip to content

Commit

Permalink
Code Refactor: data resiliency improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharthesavior committed Sep 30, 2023
1 parent 98f9fe3 commit 7a07e1f
Show file tree
Hide file tree
Showing 30 changed files with 458 additions and 368 deletions.
5 changes: 3 additions & 2 deletions src/Actions/ActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace Conveyor\Actions;

use Conveyor\Actions\Interfaces\ActionInterface;
use Conveyor\Models\Interfaces\GenericPersistenceInterface;
use Conveyor\SocketHandlers\SocketMessageRouter;
use Conveyor\Persistence\Interfaces\GenericPersistenceInterface;
use Exception;
use League\Pipeline\PipelineBuilder;
use League\Pipeline\PipelineInterface;
Expand Down Expand Up @@ -40,6 +39,8 @@ public static function make(array $actions = [], bool $fresh = false): static
}

/**
* This method adds default actions to the manager.
*
* @param bool $fresh
*
* @return static
Expand Down
8 changes: 4 additions & 4 deletions src/Actions/Traits/HasPersistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace Conveyor\Actions\Traits;

use Conveyor\Models\Interfaces\ChannelPersistenceInterface;
use Conveyor\Models\Interfaces\GenericPersistenceInterface;
use Conveyor\Models\Interfaces\ListenerPersistenceInterface;
use Conveyor\Models\Interfaces\UserAssocPersistenceInterface;
use Conveyor\Persistence\Interfaces\ChannelPersistenceInterface;
use Conveyor\Persistence\Interfaces\GenericPersistenceInterface;
use Conveyor\Persistence\Interfaces\ListenerPersistenceInterface;
use Conveyor\Persistence\Interfaces\UserAssocPersistenceInterface;

trait HasPersistence
{
Expand Down
14 changes: 0 additions & 14 deletions src/Models/Abstractions/GenericPersistence.php

This file was deleted.

100 changes: 0 additions & 100 deletions src/Models/Sqlite/DatabaseBootstrap.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Conveyor\Models\Sqlite;
namespace Conveyor\Models;

use Illuminate\Database\Eloquent\Model;

Expand All @@ -9,6 +9,7 @@ class WsAssociation extends Model
const TABLE_NAME = 'wsassociations';

protected $table = self::TABLE_NAME;
protected $connection = 'socket-conveyor';

protected $fillable = [
'fd',
Expand Down
3 changes: 2 additions & 1 deletion src/Models/Sqlite/WsChannel.php → src/Models/WsChannel.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Conveyor\Models\Sqlite;
namespace Conveyor\Models;

use Illuminate\Database\Eloquent\Model;

Expand All @@ -10,6 +10,7 @@ class WsChannel extends Model

/** @var string */
protected $table = self::TABLE_NAME;
protected $connection = 'socket-conveyor';

protected $fillable = [
'fd',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Conveyor\Models\Sqlite;
namespace Conveyor\Models;

use Illuminate\Database\Eloquent\Model;

Expand All @@ -10,6 +10,7 @@ class WsListener extends Model


protected $table = self::TABLE_NAME;
protected $connection = 'socket-conveyor';

protected $fillable = [
'fd',
Expand Down
32 changes: 32 additions & 0 deletions src/Persistence/Abstracts/GenericPersistence.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Conveyor\Persistence\Abstracts;

use Conveyor\Persistence\DTO\DatabaseConnectionDTO;
use Conveyor\Persistence\Interfaces\GenericPersistenceInterface;

abstract class GenericPersistence implements GenericPersistenceInterface
{
/**
* @param @param DatabaseConnectionDTO|array{driver:string,database:string,username:string,password:string,charset:string,collation:string,prefix:string} $databaseOptions
*/
public function __construct(
protected DatabaseConnectionDTO|array $databaseOptions = [
'driver' => 'sqlite',
'database' => __DIR__ . '/../../../../../database/database.sqlite',
'username' => null,
'password' => null,
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
) {
if (is_array($this->databaseOptions)) {
$this->databaseOptions = DatabaseConnectionDTO::fromArray($this->databaseOptions);
}

$this->refresh(true);
}

abstract public function refresh(bool $fresh = false): static;
}
64 changes: 64 additions & 0 deletions src/Persistence/DTO/DatabaseConnectionDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Conveyor\Persistence\DTO;

use ArrayAccess;

class DatabaseConnectionDTO implements ArrayAccess
{
public function __construct(
public string $driver,
public string $database,
public ?string $username = null,
public ?string $password = null,
public string $charset = 'utf8',
public string $collation = 'utf8_unicode_ci',
public string $prefix = '',
) {}

public static function fromArray(array $data): self
{
return new self(
$data['driver'],
$data['database'],
$data['username'] ?? null,
$data['password'] ?? null,
$data['charset'] ?? 'utf8',
$data['collation'] ?? 'utf8_unicode_ci',
$data['prefix'] ?? ''
);
}

public function toArray(): array
{
return [
'driver' => $this->driver,
'database' => $this->database,
'username' => $this->username,
'password' => $this->password,
'charset' => $this->charset,
'collation' => $this->collation,
'prefix' => $this->prefix,
];
}

public function offsetExists($offset): bool
{
return property_exists($this, $offset);
}

public function offsetGet($offset): mixed
{
return $this->{$offset};
}

public function offsetSet($offset, $value): void
{
$this->{$offset} = $value;
}

public function offsetUnset($offset): void
{
$this->{$offset} = null;
}
}
Loading

0 comments on commit 7a07e1f

Please sign in to comment.