Skip to content

Commit

Permalink
Merge pull request #9 from justbetter/feature/first-or-create
Browse files Browse the repository at this point in the history
Added the firstOrCreate method
  • Loading branch information
VincentBean authored Jan 27, 2023
2 parents 6f9fa3c + 645b839 commit e36feef
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
9 changes: 6 additions & 3 deletions src/OData/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use JustBetter\DynamicsClient\Concerns\HasCasts;
use JustBetter\DynamicsClient\Concerns\HasData;
use JustBetter\DynamicsClient\Concerns\HasKeys;
use JustBetter\DynamicsClient\Exceptions\DynamicsException;
use JustBetter\DynamicsClient\Query\QueryBuilder;
use SaintSystems\OData\Entity;
use SaintSystems\OData\ODataClient;
Expand All @@ -28,7 +29,8 @@ abstract class BaseResource implements ArrayAccess, Arrayable
final public function __construct(?string $connection = null, ?string $endpoint = null)
{
$this->connection ??= $connection ?? config('dynamics.connection');
$this->endpoint ??= $endpoint ?? config('dynamics.resources.'.static::class, Str::afterLast(static::class, '\\'));
$this->endpoint ??= $endpoint ?? config('dynamics.resources.'.static::class,
Str::afterLast(static::class, '\\'));
}

public static function new(?string $connection = null, ?string $endpoint = null): static
Expand Down Expand Up @@ -69,15 +71,16 @@ public function fromPage(BaseResource $page): static
return $this;
}

public function create(array $data): ?static
public function create(array $data): static
{
/** @var array<int, Entity> $entities */
$entities = $this->client()->post($this->endpoint, $data);

if (empty($entities)) {
return null;
throw new DynamicsException('No data returned after creation');
}

/** @var Entity $entity */
$entity = reset($entities);

return static::new($this->connection, $this->endpoint)->fromEntity($entity);
Expand Down
29 changes: 22 additions & 7 deletions src/Query/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ public function __call(string $name, array $arguments): mixed
return $this;
}

public function mapToClass(Entity $entity): BaseResource
public function newResourceInstance(): BaseResource
{
/** @var class-string<BaseResource> $class */
$class = $this->class;

return $class::new($this->connection, $this->endpoint)->fromEntity($entity);
return $class::new($this->connection, $this->endpoint);
}

public function mapToClass(Entity $entity): BaseResource
{
return $this->newResourceInstance()->fromEntity($entity);
}

public function get(): Enumerable
Expand Down Expand Up @@ -73,11 +78,7 @@ public function firstOrFail(): BaseResource

public function find(mixed ...$values): ?BaseResource
{
/** @var class-string<BaseResource> $class */
$class = $this->class;

/** @var BaseResource $baseResource */
$baseResource = app($class);
$baseResource = $this->newResourceInstance();

$combined = array_combine($baseResource->primaryKey, $values);

Expand Down Expand Up @@ -108,6 +109,20 @@ public function findOrFail(mixed ...$values): BaseResource
return $resource;
}

public function firstOrCreate(array $attributes = [], array $values = []): BaseResource
{
/** @var ?BaseResource $resource */
$resource = $this->where($attributes)->first();

if ($resource !== null) {
return $resource;
}

$data = array_merge($attributes, $values);

return $this->newResourceInstance()->create($data);
}

public function lazy(): LazyCollection
{
return LazyCollection::make(function (): Generator {
Expand Down

0 comments on commit e36feef

Please sign in to comment.