Skip to content

Commit

Permalink
feat: category model published to userland
Browse files Browse the repository at this point in the history
  • Loading branch information
frknasir committed May 27, 2022
1 parent ac2d366 commit 97d4dc3
Show file tree
Hide file tree
Showing 24 changed files with 238 additions and 113 deletions.
16 changes: 8 additions & 8 deletions src/Actions/CreateCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Support\Facades\Validator;
use StarfolkSoftware\Pigeonhole\Contracts\CreatesCategories;
use StarfolkSoftware\Pigeonhole\Events\CategoryCreated;
use StarfolkSoftware\Pigeonhole\Events\CreatingCategory;
use StarfolkSoftware\Pigeonhole\Pigeonhole;

class CreateCategory implements CreatesCategories
Expand All @@ -18,13 +20,7 @@ class CreateCategory implements CreatesCategories
*/
public function __invoke($user, array $data, $teamId = null)
{
if (is_callable(Pigeonhole::$validateCategoryCreation)) {
call_user_func(
Pigeonhole::$validateCategoryCreation,
$user,
$data
);
}
event(new CreatingCategory(user: $user, data: $data));

Validator::make($data, [
'name' => 'required|string|max:255',
Expand All @@ -36,8 +32,12 @@ public function __invoke($user, array $data, $teamId = null)
'type',
])->toArray();

return Pigeonhole::$supportsTeams ?
$category = Pigeonhole::$supportsTeams ?
Pigeonhole::findTeamByIdOrFail($teamId)->categories()->create($fields) :
Pigeonhole::newCategoryModel()->create($fields);

event(new CategoryCreated(category: $category));

return $category;
}
}
18 changes: 7 additions & 11 deletions src/Actions/DeleteCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

namespace StarfolkSoftware\Pigeonhole\Actions;

use StarfolkSoftware\Pigeonhole\Category;
use StarfolkSoftware\Pigeonhole\Contracts\DeletesCategories;
use StarfolkSoftware\Pigeonhole\Pigeonhole;
use StarfolkSoftware\Pigeonhole\Events\CategoryDeleted;
use StarfolkSoftware\Pigeonhole\Events\DeletingCategory;

class DeleteCategory implements DeletesCategories
{
/**
* Delete a category.
*
* @param mixed $user
* @param \StarfolkSoftware\Pigeonhole\Category $category
* @param mixed $category
* @return void
*/
public function __invoke($user, Category $category)
public function __invoke($user, $category)
{
if (is_callable(Pigeonhole::$validateCategoryDeletion)) {
call_user_func(
Pigeonhole::$validateCategoryUpdate,
$user,
$category
);
}
event(new DeletingCategory(user: $user, category: $category));

$category->delete();

event(new CategoryDeleted(category: $category));
}
}
23 changes: 11 additions & 12 deletions src/Actions/UpdateCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Facades\Validator;
use StarfolkSoftware\Pigeonhole\Category;
use StarfolkSoftware\Pigeonhole\Contracts\UpdatesCategories;
use StarfolkSoftware\Pigeonhole\Events\CategoryUpdated;
use StarfolkSoftware\Pigeonhole\Events\UpdatingCategory;
use StarfolkSoftware\Pigeonhole\Pigeonhole;

class UpdateCategory implements UpdatesCategories
Expand All @@ -13,20 +15,13 @@ class UpdateCategory implements UpdatesCategories
* Update a category.
*
* @param mixed $user
* @param \StarfolkSoftware\Pigeonhole\Category $category
* @param mixed $category
* @param array $data
* @return \StarfolkSoftware\Pigeonhole\Category
* @return mixed
*/
public function __invoke($user, Category $category, array $data)
public function __invoke($user, $category, array $data)
{
if (is_callable(Pigeonhole::$validateCategoryCreation)) {
call_user_func(
Pigeonhole::$validateCategoryUpdate,
$user,
$category,
$data
);
}
event(new UpdatingCategory(user: $user, category: $category, data: $data));

Validator::make($data, [
'name' => 'required|string|max:255',
Expand All @@ -38,6 +33,10 @@ public function __invoke($user, Category $category, array $data)
'type',
])->toArray());

return $category->refresh();
$category->refresh();

event(new CategoryUpdated(category: $category));

return $category;
}
}
2 changes: 1 addition & 1 deletion src/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

class Category extends Model
abstract class Category extends Model
{
use HasFactory;

Expand Down
3 changes: 3 additions & 0 deletions src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public function handle(): int
$this->callSilent('vendor:publish', ['--tag' => 'pigeonhole-config', '--force' => true]);
$this->callSilent('vendor:publish', ['--tag' => 'pigeonhole-migrations', '--force' => true]);

// Models...
copy(__DIR__.'/../../stubs/app/Models/Category.php', app_path('Models/Category.php'));

// Service Providers...
copy(__DIR__.'/../../stubs/app/Providers/PigeonholeServiceProvider.php', app_path('Providers/PigeonholeServiceProvider.php'));

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

namespace StarfolkSoftware\Pigeonhole\Events;

use StarfolkSoftware\Pigeonhole\Events\CategoryEvent;

class CategoryCreated extends CategoryEvent
{
}
9 changes: 9 additions & 0 deletions src/Events/CategoryDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace StarfolkSoftware\Pigeonhole\Events;

use StarfolkSoftware\Pigeonhole\Events\CategoryEvent;

class CategoryDeleted extends CategoryEvent
{
}
29 changes: 29 additions & 0 deletions src/Events/CategoryEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace StarfolkSoftware\Pigeonhole\Events;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

abstract class CategoryEvent
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*
* @param mixed $user
* @param mixed $category
* @param array $data
* @return void
*/
public function __construct(
public $user = null,
public $category = null,
public $data = []
) {
}
}
9 changes: 9 additions & 0 deletions src/Events/CategoryUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace StarfolkSoftware\Pigeonhole\Events;

use StarfolkSoftware\Pigeonhole\Events\CategoryEvent;

class CategoryUpdated extends CategoryEvent
{
}
9 changes: 9 additions & 0 deletions src/Events/CreatingCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace StarfolkSoftware\Pigeonhole\Events;

use StarfolkSoftware\Pigeonhole\Events\CategoryEvent;

class CreatingCategory extends CategoryEvent
{
}
9 changes: 9 additions & 0 deletions src/Events/DeletingCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace StarfolkSoftware\Pigeonhole\Events;

use StarfolkSoftware\Pigeonhole\Events\CategoryEvent;

class DeletingCategory extends CategoryEvent
{
}
9 changes: 9 additions & 0 deletions src/Events/UpdatingCategory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace StarfolkSoftware\Pigeonhole\Events;

use StarfolkSoftware\Pigeonhole\Events\CategoryEvent;

class UpdatingCategory extends CategoryEvent
{
}
12 changes: 8 additions & 4 deletions src/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ public function store(CreatesCategories $createsCategories)
/**
* Update the specified resource in storage.
*
* @param \StarfolkSoftware\Pigeonhole\Category $category
* @param mixed $category
* @param \StarfolkSoftware\Pigeonhole\Contracts\UpdatesCategories $updatesCategories
* @return \Illuminate\Http\RedirectResponse
*/
public function update(Category $category, UpdatesCategories $updatesCategories)
public function update($category, UpdatesCategories $updatesCategories)
{
$category = Pigeonhole::newCategoryModel()->findOrFail($category);

$category = $updatesCategories(
request()->user(),
$category,
Expand All @@ -51,12 +53,14 @@ public function update(Category $category, UpdatesCategories $updatesCategories)
/**
* Remove the specified resource from storage.
*
* @param \StarfolkSoftware\Pigeonhole\Category $category
* @param mixed $category
* @param \StarfolkSoftware\Pigeonhole\Contracts\DeletesCategories $deletesCategories
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(Category $category, DeletesCategories $deletesCategories)
public function destroy($category, DeletesCategories $deletesCategories)
{
$category = Pigeonhole::newCategoryModel()->findOrFail($category);

$deletesCategories(
request()->user(),
$category
Expand Down
56 changes: 1 addition & 55 deletions src/Pigeonhole.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class Pigeonhole
*
* @var string
*/
public static $categoryModel = 'StarfolkSoftware\\Pigeonhole\\Category';
public static $categoryModel = 'App\\Models\\Category';

/**
* Indicates if Pigeonhole should support teams.
Expand All @@ -43,27 +43,6 @@ final class Pigeonhole
*/
public static $teamModel;

/**
* The callback to perform additional validation when creating new category.
*
* @var callable
*/
public static $validateCategoryCreation;

/**
* The callback to perform additional validation when updating a category.
*
* @var callable
*/
public static $validateCategoryUpdate;

/**
* The callback to perform additional validation when deleting a category.
*
* @var callable
*/
public static $validateCategoryDeletion;

/**
* Get the name of the category model used by the application.
*
Expand Down Expand Up @@ -156,17 +135,6 @@ public static function createCategoriesUsing(string $class)
app()->singleton(CreatesCategories::class, $class);
}

/**
* Register a class / callback that should be used to validate category creation.
*
* @param callable $callback
* @return void
*/
public static function validateCategoryCreationUsing(callable $callback)
{
static::$validateCategoryCreation = $callback;
}

/**
* Register a class / callback that should be used to update Categories.
*
Expand All @@ -178,17 +146,6 @@ public static function updateCategoriesUsing(string $class)
app()->singleton(UpdatesCategories::class, $class);
}

/**
* Register a class / callback that should be used to validate category update.
*
* @param callable $callback
* @return void
*/
public static function validateCategoryUpdateUsing(callable $callback)
{
static::$validateCategoryUpdate = $callback;
}

/**
* Register a class / callback that should be used to delete Categories.
*
Expand All @@ -200,17 +157,6 @@ public static function deleteCategoriesUsing(string $class)
app()->singleton(DeletesCategories::class, $class);
}

/**
* Register a class / callback that should be used to validate category deletion.
*
* @param callable $callback
* @return void
*/
public static function validateCategoryDeletionUsing(callable $callback)
{
static::$validateCategoryDeletion = $callback;
}

/**
* Configure Pigeonhole to not register its routes.
*
Expand Down
22 changes: 22 additions & 0 deletions stubs/app/Models/Category.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Models;

use StarfolkSoftware\Pigeonhole\Category as PigeonholeCategory;
use StarfolkSoftware\Pigeonhole\Events\CategoryCreated;
use StarfolkSoftware\Pigeonhole\Events\CategoryDeleted;
use StarfolkSoftware\Pigeonhole\Events\CategoryUpdated;

class Category extends PigeonholeCategory
{
/**
* The event map for the model.
*
* @var array
*/
protected $dispatchesEvents = [
'created' => CategoryCreated::class,
'updated' => CategoryUpdated::class,
'deleted' => CategoryDeleted::class,
];
}
Loading

0 comments on commit 97d4dc3

Please sign in to comment.