Skip to content

Commit

Permalink
Backend Refactor & Expose Events (#87)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
DavideIadeluca authored Oct 17, 2024
1 parent 972eda3 commit dffb0f0
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 87 deletions.
1 change: 1 addition & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ jobs:
with:
enable_backend_testing: false
enable_phpstan: true
php_versions: '["8.1", "8.2", "8.3"]'

backend_directory: .
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "*"
Expand Down
29 changes: 10 additions & 19 deletions src/Api/Controllers/DeleteFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
25 changes: 10 additions & 15 deletions src/Api/Controllers/StoreFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
23 changes: 10 additions & 13 deletions src/Api/Controllers/UpdateFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
1 change: 0 additions & 1 deletion src/Api/Controllers/UserConfigureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions src/Events/AbstractFieldEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of fof/terms.
*
* Copyright (c) FriendsOfFlarum.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace FoF\Masquerade\Events;

use Flarum\User\User;
use FoF\Masquerade\Field;

abstract class AbstractFieldEvent
{
public function __construct (
public Field $field,
public User $actor,
public array $data
)
{
}
}
7 changes: 7 additions & 0 deletions src/Events/FieldCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

namespace FoF\Masquerade\Events;

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

namespace FoF\Masquerade\Events;

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

namespace FoF\Masquerade\Events;

class FieldUpdated extends AbstractFieldEvent
{
}
76 changes: 38 additions & 38 deletions src/Repositories/FieldRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@

use FoF\Masquerade\Answer;
use FoF\Masquerade\Field;
use FoF\Masquerade\Events\FieldCreated;
use FoF\Masquerade\Events\FieldUpdated;
use FoF\Masquerade\Events\FieldDeleted;
use FoF\Masquerade\FieldType\TypeFactory;
use FoF\Masquerade\Validators\FieldValidator;
use Flarum\User\User;
use Illuminate\Cache\Repository;
use Illuminate\Contracts\Events\Dispatcher;

class FieldRepository
{
const CACHE_KEY_ALL_FIELDS = 'fof.masquerade.fields.all';
const CACHE_KEY_UNCOMPLETED = 'fof.masquerade.uncompleted.u.%d';

/**
* @var Field
*/
protected $field;
/**
* @var Repository
*/
protected $cache;

/**
* FieldRepository constructor.
* @param Field $field
* @param Repository $cache
*/
public function __construct(Field $field, Repository $cache)
public function __construct (
protected Field $field,
protected FieldValidator $validator,
protected Repository $cache,
protected Dispatcher $events
)
{
$this->field = $field;
$this->cache = $cache;
}

/**
Expand All @@ -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;
}
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit dffb0f0

Please sign in to comment.