Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
Sairahcaz authored and github-actions[bot] committed Apr 2, 2023
1 parent 6f970db commit 9ca06f1
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 29 deletions.
2 changes: 0 additions & 2 deletions src/DynamicModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace LaracraftTech\LaravelDynamicModel;

use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;

class DynamicModel extends Model implements DynamicModelInterface
{
Expand Down
9 changes: 5 additions & 4 deletions src/DynamicModelBinding.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace LaracraftTech\LaravelDynamicModel;

use Exception;
use Illuminate\Support\Facades\Schema;

trait DynamicModelBinding
{
public function bindDynamically(string $tableName, string $dbConnection = null): void
{
if (!Schema::hasTable($tableName)) {
if (! Schema::hasTable($tableName)) {
throw DynamicModelException::tableDoesNotExist($tableName);
}

// change connection, if desired
if ($dbConnection) $this->setConnection($dbConnection);
if ($dbConnection) {
$this->setConnection($dbConnection);
}

// set the table for the dynamic model
$this->setTable($tableName);
Expand All @@ -24,7 +25,7 @@ public function bindDynamically(string $tableName, string $dbConnection = null):

$table = $connection->getDoctrineSchemaManager()->listTableDetails($tableName);

if (!$primaryKey = $table->getPrimaryKey()) {
if (! $primaryKey = $table->getPrimaryKey()) {
throw DynamicModelException::primaryKeyDoesNotExist();
}

Expand Down
2 changes: 1 addition & 1 deletion src/DynamicModelException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static function tableDoesNotExist(string $tableName): static

public static function primaryKeyDoesNotExist(): static
{
return new static("The table you provided to the dynamic model has no primary key set! Please create it first!");
return new static('The table you provided to the dynamic model has no primary key set! Please create it first!');
}

public static function bindFuncDoesNotExist(string $model): static
Expand Down
10 changes: 2 additions & 8 deletions src/DynamicModelFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@

namespace LaracraftTech\LaravelDynamicModel;

use Exception;
use Illuminate\Database\Eloquent\Model;

class DynamicModelFactory
{
/**
* Create the Dynamic Model Instance and make sure the one provided/created,
* is based on Eloquent and implements the DynamicModelInterface
*
* @param string $concreteClassName
* @param string $tableName
* @param string|null $dbConnection
* @return Model & DynamicModelInterface
*/
public function create(string $concreteClassName, string $tableName, string $dbConnection = null): Model & DynamicModelInterface
public function create(string $concreteClassName, string $tableName, string $dbConnection = null): Model&DynamicModelInterface
{
/** @var $dynamicModel Model & DynamicModelInterface */
$dynamicModel = new $concreteClassName();

if (!method_exists($dynamicModel, 'bindDynamically')) {
if (! method_exists($dynamicModel, 'bindDynamically')) {
throw DynamicModelException::bindFuncDoesNotExist($concreteClassName);
}

Expand Down
3 changes: 0 additions & 3 deletions src/DynamicModelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ interface DynamicModelInterface
{
/**
* Make sure the DynamicModel has a bindDynamic function.
*
* @param string $tableName
* @param string|null $dbConnection
*/
public function bindDynamically(string $tableName, string $dbConnection = null): void;
}
3 changes: 1 addition & 2 deletions src/LaravelDynamicModelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ public function register()
parent::register();

$this->app->bind(DynamicModel::class, function ($app, $parameters = []) {
if (!isset($parameters['table_name'])) {
if (! isset($parameters['table_name'])) {
throw new \Exception('please provide table_name parameter');
}

return app(DynamicModelFactory::class)
->create(DynamicModel::class, $parameters['table_name']);
});
}

}
12 changes: 6 additions & 6 deletions tests/DynamicModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

$foo->create([
'col1' => 'asdf',
'col2' => 123
'col2' => 123,
]);

expect($foo->find(1))
Expand All @@ -41,7 +41,7 @@

$bar->create([
'col1' => 'fdsa',
'col2' => 321
'col2' => 321,
]);

expect($bar->find(1))
Expand All @@ -54,25 +54,25 @@

$myDynamicModel->create([
'col1' => 'asdf',
'col2' => 123
'col2' => 123,
]);

expect($myDynamicModel->find(1))
->col1->toBe('asdf')
->col2->toBe(123)
->doSomething()->toBe("foo");
->doSomething()->toBe('foo');
});

it('can use traited dynamic model', function () {
$myDynamicModel = App::make(DynamicModelFactory::class)->create(MyTraitedDynamicModel::class, 'foo');

$myDynamicModel->create([
'col1' => 'asdf',
'col2' => 123
'col2' => 123,
]);

expect($myDynamicModel->find(1))
->col1->toBe('asdf')
->col2->toBe(123)
->doSomethingBase()->toBe("bar");
->doSomethingBase()->toBe('bar');
});
2 changes: 1 addition & 1 deletion tests/MyBaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class MyBaseModel extends Model
{
public function doSomethingBase()
{
return "bar";
return 'bar';
}
}
2 changes: 1 addition & 1 deletion tests/MyExtendedDynamicModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class MyExtendedDynamicModel extends DynamicModel
{
public function doSomething()
{
return "foo";
return 'foo';
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace LaracraftTech\LaravelDynamicModel\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Orchestra\Testbench\TestCase as Orchestra;
use LaracraftTech\LaravelDynamicModel\LaravelDynamicModelServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

class TestCase extends Orchestra
{
Expand Down

0 comments on commit 9ca06f1

Please sign in to comment.