From dffb0f04d5a6fc8ba3d66bc4c50a2572da1f8d35 Mon Sep 17 00:00:00 2001 From: Davide Iadeluca <146922689+DavideIadeluca@users.noreply.github.com> Date: Thu, 17 Oct 2024 13:31:42 +0200 Subject: [PATCH] Backend Refactor & Expose Events (#87) * refactor: change deletion logic & implement event * refactor: change creation logic & implement event * chore: fix * chore: fix * efactor: change update logic & implement event * fix: add back types * chore: remove unused imports * chore: fix phpstan * chore: raise PHP version to ^8.1 * chore: raise flarum/core constraint to ^1.8.5 * feat: add in boilerplate events --- .github/workflows/backend.yml | 1 + composer.json | 3 +- src/Api/Controllers/DeleteFieldController.php | 29 +++---- src/Api/Controllers/StoreFieldController.php | 25 +++--- src/Api/Controllers/UpdateFieldController.php | 23 +++--- .../Controllers/UserConfigureController.php | 1 - src/Events/AbstractFieldEvent.php | 26 +++++++ src/Events/FieldCreated.php | 7 ++ src/Events/FieldDeleted.php | 7 ++ src/Events/FieldUpdated.php | 7 ++ src/Repositories/FieldRepository.php | 76 +++++++++---------- 11 files changed, 118 insertions(+), 87 deletions(-) create mode 100644 src/Events/AbstractFieldEvent.php create mode 100644 src/Events/FieldCreated.php create mode 100644 src/Events/FieldDeleted.php create mode 100644 src/Events/FieldUpdated.php diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml index eba98e5..583ac00 100644 --- a/.github/workflows/backend.yml +++ b/.github/workflows/backend.yml @@ -8,5 +8,6 @@ jobs: with: enable_backend_testing: false enable_phpstan: true + php_versions: '["8.1", "8.2", "8.3"]' backend_directory: . diff --git a/composer.json b/composer.json index dc26680..8b24ac8 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ "forum": "https://discuss.flarum.org/d/5791" }, "require": { - "flarum/core": "^1.2" + "php": "^8.1", + "flarum/core": "^1.8.5" }, "replace": { "flagrow/masquerade": "*" diff --git a/src/Api/Controllers/DeleteFieldController.php b/src/Api/Controllers/DeleteFieldController.php index 6f037bc..e245518 100644 --- a/src/Api/Controllers/DeleteFieldController.php +++ b/src/Api/Controllers/DeleteFieldController.php @@ -2,38 +2,29 @@ namespace FoF\Masquerade\Api\Controllers; +use Flarum\Api\Controller\AbstractDeleteController; use Flarum\Http\RequestUtil; -use FoF\Masquerade\Api\Serializers\FieldSerializer; use FoF\Masquerade\Repositories\FieldRepository; -use FoF\Masquerade\Validators\FieldValidator; -use Flarum\Api\Controller\AbstractShowController; use Illuminate\Support\Arr; use Psr\Http\Message\ServerRequestInterface; -use Tobscure\JsonApi\Document; -class DeleteFieldController extends AbstractShowController +class DeleteFieldController extends AbstractDeleteController { - public $serializer = FieldSerializer::class; - - protected $validator; - protected $fields; - - public function __construct(FieldValidator $validator, FieldRepository $fields) + public function __construct( + protected FieldRepository $fields + ) { - $this->validator = $validator; - $this->fields = $fields; } - protected function data(ServerRequestInterface $request, Document $document) + protected function delete(ServerRequestInterface $request) { - RequestUtil::getActor($request)->assertAdmin(); + $actor = RequestUtil::getActor($request); + $actor->assertAdmin(); $id = Arr::get($request->getQueryParams(), 'id'); - if ($deleted = $this->fields->delete($id)) { - return $deleted; - } + $field = $this->fields->findOrFail($id); - throw new \RuntimeException('Could not delete Field'); + $this->fields->delete($actor, $field); } } diff --git a/src/Api/Controllers/StoreFieldController.php b/src/Api/Controllers/StoreFieldController.php index a573edf..8ffa9ef 100644 --- a/src/Api/Controllers/StoreFieldController.php +++ b/src/Api/Controllers/StoreFieldController.php @@ -2,36 +2,31 @@ namespace FoF\Masquerade\Api\Controllers; +use Flarum\Api\Controller\AbstractCreateController; use Flarum\Http\RequestUtil; -use FoF\Masquerade\Api\Serializers\FieldSerializer; use FoF\Masquerade\Repositories\FieldRepository; -use FoF\Masquerade\Validators\FieldValidator; -use Flarum\Api\Controller\AbstractShowController; +use FoF\Masquerade\Api\Serializers\FieldSerializer; use Illuminate\Support\Arr; use Psr\Http\Message\ServerRequestInterface; use Tobscure\JsonApi\Document; -class StoreFieldController extends AbstractShowController +class StoreFieldController extends AbstractCreateController { public $serializer = FieldSerializer::class; - protected $validator; - protected $fields; - - public function __construct(FieldValidator $validator, FieldRepository $fields) + public function __construct( + protected FieldRepository $fields + ) { - $this->validator = $validator; - $this->fields = $fields; } protected function data(ServerRequestInterface $request, Document $document) { - RequestUtil::getActor($request)->assertAdmin(); + $actor = RequestUtil::getActor($request); + $actor->assertAdmin(); $attributes = Arr::get($request->getParsedBody(), 'data.attributes', []); - $this->validator->assertValid($attributes); - - return $this->fields->store($attributes); + return $this->fields->store($actor, $attributes); } -} +} \ No newline at end of file diff --git a/src/Api/Controllers/UpdateFieldController.php b/src/Api/Controllers/UpdateFieldController.php index d5d889d..c620108 100644 --- a/src/Api/Controllers/UpdateFieldController.php +++ b/src/Api/Controllers/UpdateFieldController.php @@ -2,38 +2,35 @@ namespace FoF\Masquerade\Api\Controllers; +use Flarum\Api\Controller\AbstractCreateController; use Flarum\Http\RequestUtil; use FoF\Masquerade\Api\Serializers\FieldSerializer; use FoF\Masquerade\Repositories\FieldRepository; -use FoF\Masquerade\Validators\FieldValidator; -use Flarum\Api\Controller\AbstractShowController; use Illuminate\Support\Arr; use Psr\Http\Message\ServerRequestInterface; use Tobscure\JsonApi\Document; -class UpdateFieldController extends AbstractShowController +class UpdateFieldController extends AbstractCreateController { public $serializer = FieldSerializer::class; - protected $validator; - protected $fields; - - public function __construct(FieldValidator $validator, FieldRepository $fields) + public function __construct ( + protected FieldRepository $field + ) { - $this->validator = $validator; - $this->fields = $fields; } protected function data(ServerRequestInterface $request, Document $document) { - RequestUtil::getActor($request)->assertAdmin(); + $actor = RequestUtil::getActor($request); + $actor->assertAdmin(); $id = Arr::get($request->getQueryParams(), 'id'); - $attributes = Arr::get($request->getParsedBody(), 'data.attributes', []); + $field = $this->field->findOrFail($id); - $this->validator->assertValid($attributes); + $attributes = Arr::get($request->getParsedBody(), 'data.attributes', []); - return $this->fields->update($id, $attributes); + return $this->field->update($actor, $field, $attributes); } } diff --git a/src/Api/Controllers/UserConfigureController.php b/src/Api/Controllers/UserConfigureController.php index 997a0b6..4bc5c48 100644 --- a/src/Api/Controllers/UserConfigureController.php +++ b/src/Api/Controllers/UserConfigureController.php @@ -3,7 +3,6 @@ namespace FoF\Masquerade\Api\Controllers; use Flarum\Http\RequestUtil; -use FoF\Masquerade\Api\Serializers\FieldSerializer; use FoF\Masquerade\Field; use FoF\Masquerade\Repositories\FieldRepository; use FoF\Masquerade\Validators\AnswerValidator; diff --git a/src/Events/AbstractFieldEvent.php b/src/Events/AbstractFieldEvent.php new file mode 100644 index 0000000..891c2f1 --- /dev/null +++ b/src/Events/AbstractFieldEvent.php @@ -0,0 +1,26 @@ +field = $field; - $this->cache = $cache; } /** @@ -43,39 +37,49 @@ public function all() }); } - public function store(array $attributes): Field + public function clearCacheAllFields() { - $field = $this->field->newInstance(); - $field->sort = $this->highestSort(); + $this->cache->forget(static::CACHE_KEY_ALL_FIELDS); + } - $type = TypeFactory::typeForField($attributes); + public function findOrFail(string $id) + { + return $this->field->newQuery()->findOrFail($id); + } + + public function store(User $actor, array $attributes): Field + { + $this->validator->assertValid($attributes); + $type = TypeFactory::typeForField($attributes); $attributes = array_merge($attributes, $type->overrideAttributes()); - $field->fill($attributes); + $field = new Field($attributes); $field->save(); - $this->cache->forget(static::CACHE_KEY_ALL_FIELDS); + $this->events->dispatch(new FieldCreated($field, $actor, $attributes)); + + $this->clearCacheAllFields(); return $field; } - public function update($id, array $attributes): Field + public function update(User $actor, Field $field, array $attributes): Field { - /** @var Field */ - $field = $this->field->findOrFail($id); + $this->validator->assertValid($attributes); $type = TypeFactory::typeForField($attributes); - $attributes = array_merge($attributes, $type->overrideAttributes()); $field->fill($attributes); if ($field->isDirty()) { $field->save(); - } - $this->cache->forget(static::CACHE_KEY_ALL_FIELDS); + $this->events->dispatch(new FieldUpdated($field, $actor, $attributes)); + + $this->clearCacheAllFields(); + } return $field; } @@ -92,19 +96,15 @@ public function sorting(array $sorting) $this->cache->forget(static::CACHE_KEY_ALL_FIELDS); } - /** - * @param $id - * @return bool|Field - */ - public function delete($id) + public function delete(User $actor, Field $field) { - $field = $this->field->findOrFail($id); + $response = $field->delete(); - $field->delete(); + $this->events->dispatch(new FieldDeleted($field, $actor, [])); - $this->cache->forget(static::CACHE_KEY_ALL_FIELDS); + $this->clearCacheAllFields(); - return $field; + return $response; } /**