Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Replace DTO's with Fluent objects. #80

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions src/Contracts/Accountable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ public function getId();
* @return string
*/
public function getName();

/**
* Get the accountable service config.
*
* @return \Payavel\Orchestration\Fluent\ServiceConfig
*/
public function getServiceConfig();
}
7 changes: 0 additions & 7 deletions src/Contracts/Providable.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,4 @@ public function getId();
* @return string
*/
public function getName();

/**
* Get the providable service config.
*
* @return \Payavel\Orchestration\Fluent\ServiceConfig
*/
public function getServiceConfig();
}
83 changes: 0 additions & 83 deletions src/DataTransferObjects/Account.php

This file was deleted.

56 changes: 0 additions & 56 deletions src/DataTransferObjects/Provider.php

This file was deleted.

18 changes: 9 additions & 9 deletions src/Drivers/ConfigDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Illuminate\Support\Str;
use Payavel\Orchestration\Contracts\Accountable;
use Payavel\Orchestration\Contracts\Providable;
use Payavel\Orchestration\DataTransferObjects\Account;
use Payavel\Orchestration\DataTransferObjects\Provider;
use Payavel\Orchestration\Fluent\Account;
use Payavel\Orchestration\Fluent\Provider;
use Payavel\Orchestration\Fluent\ServiceConfig;
use Payavel\Orchestration\ServiceDriver;
use Payavel\Orchestration\Traits\GeneratesFiles;
Expand Down Expand Up @@ -43,8 +43,8 @@ public function __construct(ServiceConfig $serviceConfig)
{
parent::__construct($serviceConfig);

$this->providers = collect($this->serviceConfig->get('providers'));
$this->accounts = collect($this->serviceConfig->get('accounts'));
$this->providers = Collection::make($this->serviceConfig->get('providers'));
$this->accounts = Collection::make($this->serviceConfig->get('accounts'));
}

/**
Expand All @@ -59,13 +59,13 @@ public function resolveProvider($provider)
return $provider;
}

if (is_null($attributes = $this->providers->get($provider))) {
if (!$this->providers->has($provider)) {
return null;
}

return new Provider(
$this->serviceConfig,
array_merge(['id' => $provider], $attributes)
$provider
);
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public function resolveAccount($account)

return new Account(
$this->serviceConfig,
array_merge(['id' => $account], $attributes)
$account
);
}

Expand All @@ -129,9 +129,9 @@ public function getDefaultAccount(Providable $provider = null)
*
* @throws Exception
*/
protected function check(Providable $provider, Accountable $account)
protected function check(Provider $provider, Account $account)
{
if ($account->providers->contains('id', $provider->id)) {
if (Collection::make($account->get('providers'))->has($provider->id)) {
return;
}

Expand Down
73 changes: 73 additions & 0 deletions src/Fluent/Account.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Payavel\Orchestration\Fluent;

use Illuminate\Support\Collection;
use Illuminate\Support\Fluent;
use Payavel\Orchestration\Contracts\Accountable;

class Account extends Fluent implements Accountable
{
/**
* The service config.
*
* @var \Payavel\Orchestration\Fluent\ServiceConfig
*/
private ServiceConfig $serviceConfig;

public function __construct(ServiceConfig $serviceConfig, $id)
{
$this->serviceConfig = $serviceConfig;

parent::__construct([
'id' => $id,
...$serviceConfig->get("accounts.{$id}", []),
]);
}

/**
* Get an attribute from the fluent instance using "dot" notation.
*
* @param string $key
* @param string|int|mixed|null $default
* @return string|int|mixed|null
*/
public function get($key, $default = null)
{
return data_get($this->attributes, $key, value($default));
}

/**
* Set an attribute to the fluent instance using "dot" notation.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function set($key, $value)
{
data_set($this->attributes, $key, $value);

$this->serviceConfig->set("accounts.{$this->attributes['id']}.{$key}", $value);
}

/**
* Get the accountable id.
*
* @return int
*/
public function getId()
{
return $this->attributes['id'];
}

/**
* Get the accountable name.
*
* @return string
*/
public function getName()
{
return $this->attributes['name'] ?? $this->attributes['id'];
}
}
72 changes: 72 additions & 0 deletions src/Fluent/Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Payavel\Orchestration\Fluent;

use Illuminate\Support\Fluent;
use Payavel\Orchestration\Contracts\Providable;

class Provider extends Fluent implements Providable
{
/**
* The service config.
*
* @var \Payavel\Orchestration\Fluent\ServiceConfig
*/
public ServiceConfig $serviceConfig;

public function __construct(ServiceConfig $serviceConfig, $id)
{
$this->serviceConfig = $serviceConfig;

parent::__construct([
'id' => $id,
...$serviceConfig->get("providers.{$id}"),
]);
}

/**
* Get an attribute from the fluent instance using "dot" notation.
*
* @param string $key
* @param string|int|mixed|null $default
* @return string|int|mixed|null
*/
public function get($key, $default = null)
{
return data_get($this->attributes, $key, value($default));
}

/**
* Set an attribute to the fluent instance using "dot" notation.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function set($key, $value)
{
data_set($this->attributes, $key, $value);

$this->serviceConfig->set("providers.{$this->attributes['id']}.{$key}", $value);
}

/**
* Get the providable id.
*
* @return int
*/
public function getId()
{
return $this->attributes['id'];
}

/**
* Get the providable name.
*
* @return string
*/
public function getName()
{
return $this->attributes['name'] ?? $this->attributes['id'];
}
}
Loading
Loading