Skip to content

Commit

Permalink
Use trait instead of overwriting the entry repository
Browse files Browse the repository at this point in the history
  • Loading branch information
royduin committed Feb 7, 2023
1 parent 10a0cd6 commit bf44710
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
5 changes: 4 additions & 1 deletion src/Actions/Categories/CreateCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Str;
use Rapidez\Statamic\Actions\Traits\UpdateOrCreateEntry;
use Statamic\Facades\Entry;

class CreateCategories
{
use UpdateOrCreateEntry;

public function create(Collection $categories, string $storeCode): \Illuminate\Support\Collection
{
if ($categories->isEmpty()) {
Expand All @@ -20,6 +23,6 @@ public function create(Collection $categories, string $storeCode): \Illuminate\S
'title' => $category->name,
'slug' => Str::replace('/', '-', $category->url_path),
'url_path' => $category->url_path ? '/' . $category->url_path : '',
])->each(fn ($category) => Entry::updateOrCreate($category, 'categories', 'slug', $storeCode));
])->each(fn ($category) => self::updateOrCreate($category, 'categories', 'slug', $storeCode));
}
}
5 changes: 4 additions & 1 deletion src/Actions/Products/CreateProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
namespace Rapidez\Statamic\Actions\Products;

use Illuminate\Support\Enumerable;
use Rapidez\Statamic\Actions\Traits\UpdateOrCreateEntry;
use Statamic\Facades\Entry;

class CreateProducts
{
use UpdateOrCreateEntry;

public function create(Enumerable $products, string $storeCode): Enumerable
{
return $products->map(fn ($product): array => [
'sku' => $product->sku,
'title' => $product->name,
'visibility' => $product->visibility
])->each(fn (array $product) => Entry::updateOrCreate($product, 'products', 'sku', $storeCode));
])->each(fn (array $product) => self::updateOrCreate($product, 'products', 'sku', $storeCode));
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
<?php

namespace Rapidez\Statamic\Repositories;
namespace Rapidez\Statamic\Actions\Traits;

use Statamic\Facades\Entry;
use Statamic\Entries\Entry as StatamicEntry;
use Statamic\Stache\Repositories\EntryRepository as StatamicEntryRepository;
use Statamic\Statamic;

class EntryRepository extends StatamicEntryRepository
trait UpdateOrCreateEntry
{
public function updateOrCreate(array $data, string $collection, string $identifier, string $storeCode) : StatamicEntry
{
$entry = Entry::query()->where('collection', $collection)->where($identifier, $data[$identifier])->where('locale', $storeCode)->first();
$entry = Entry::query()
->where('collection', $collection)
->where($identifier, $data[$identifier])
->where('locale', $storeCode)
->first();

if ($entry) {
$entry->merge($data)
Expand All @@ -30,7 +32,7 @@ public function updateOrCreate(array $data, string $collection, string $identifi
if ($identifier == 'slug' && isset($data[$identifier])) {
$entry->slug($data[$identifier]);
}

$entry->save();

return $entry;
Expand Down
11 changes: 0 additions & 11 deletions src/RapidezStatamicServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public function boot()

$this->bootCommands()
->bootConfig()
->bootRepositories()
->bootPublishables()
->bootComposers()
->bootListeners()
Expand Down Expand Up @@ -98,16 +97,6 @@ public function bootListeners() : self
return $this;
}

public function bootRepositories() : self
{
Statamic::repository(
StatamicEntryRepository::class,
EntryRepository::class
);

return $this;
}


public function bootPublishables() : self
{
Expand Down

0 comments on commit bf44710

Please sign in to comment.