Tag syncing is now done within the Component commands

This commit is contained in:
James Brooks
2019-07-12 10:29:23 +01:00
parent 06af61b8c5
commit 6810af48f7
10 changed files with 197 additions and 64 deletions

View File

@@ -74,6 +74,13 @@ final class CreateComponentCommand
*/
public $meta;
/**
* Tags string.
*
* @var string
*/
public $tags;
/**
* The validation rules.
*
@@ -88,23 +95,25 @@ final class CreateComponentCommand
'group_id' => 'nullable|int',
'enabled' => 'nullable|bool',
'meta' => 'nullable|array',
'tags' => 'nullable|string',
];
/**
* Create a new add component command instance.
*
* @param string $name
* @param string $description
* @param int $status
* @param string $link
* @param int $order
* @param int $group_id
* @param bool $enabled
* @param array|null $meta
* @param string $name
* @param string $description
* @param int $status
* @param string $link
* @param int $order
* @param int $group_id
* @param bool $enabled
* @param array|null $meta
* @param string|null $tags
*
* @return void
*/
public function __construct($name, $description, $status, $link, $order, $group_id, $enabled, $meta)
public function __construct($name, $description, $status, $link, $order, $group_id, $enabled, $meta, $tags = null)
{
$this->name = $name;
$this->description = $description;
@@ -114,5 +123,6 @@ final class CreateComponentCommand
$this->group_id = $group_id;
$this->enabled = $enabled;
$this->meta = $meta;
$this->tags = $tags;
}
}

View File

@@ -78,6 +78,13 @@ final class UpdateComponentCommand
*/
public $meta;
/**
* The tags.
*
* @var string|null
*/
public $tags;
/**
* If this is true, we won't notify subscribers of the change.
*
@@ -114,11 +121,12 @@ final class UpdateComponentCommand
* @param int|null $group_id
* @param bool|null $enabled
* @param array|null $meta
* @param string|null $tags
* @param bool $silent
*
* @return void
*/
public function __construct(Component $component, $name = null, $description = null, $status = null, $link = null, $order = null, $group_id = null, $enabled = null, $meta = null, $silent = null)
public function __construct(Component $component, $name = null, $description = null, $status = null, $link = null, $order = null, $group_id = null, $enabled = null, $meta = null, $tags = null, $silent = null)
{
$this->component = $component;
$this->name = $name;
@@ -129,6 +137,8 @@ final class UpdateComponentCommand
$this->group_id = $group_id;
$this->enabled = $enabled;
$this->meta = $meta;
$this->tags = $tags;
$this->silent = $silent;
$this->tags = $tags;
}
}

View File

@@ -53,6 +53,15 @@ class CreateComponentCommandHandler
{
$component = Component::create($this->filter($command));
// Sync the tags into the component.
if ($command->tags) {
collect(preg_split('/ ?, ?/', $command->tags))->filter()->map(function ($tag) {
return trim($tag);
})->pipe(function ($tags) use ($component) {
$component->attachTags($tags);
});
}
event(new ComponentWasCreatedEvent($this->auth->user(), $component));
return $component;

View File

@@ -56,6 +56,15 @@ class UpdateComponentCommandHandler
$component->update($this->filter($command));
// Sync the tags into the component.
if ($command->tags) {
collect(preg_split('/ ?, ?/', $command->tags))->filter()->map(function ($tag) {
return trim($tag);
})->pipe(function ($tags) use ($component) {
$component->syncTags($tags);
});
}
event(new ComponentWasUpdatedEvent($this->auth->user(), $component));
return $component;

View File

@@ -84,25 +84,13 @@ class ComponentController extends AbstractApiController
Binput::get('order'),
Binput::get('group_id'),
(bool) Binput::get('enabled', true),
Binput::get('meta', null)
Binput::get('meta'),
Binput::get('tags'),
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
if (Binput::has('tags')) {
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
}
return $this->item($component);
}
@@ -125,26 +113,14 @@ class ComponentController extends AbstractApiController
Binput::get('order'),
Binput::get('group_id'),
Binput::get('enabled', $component->enabled),
Binput::get('meta', null),
Binput::get('meta'),
Binput::get('tags'),
(bool) Binput::get('silent', false)
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
if (Binput::has('tags')) {
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
}
return $this->item($component);
}

View File

@@ -113,7 +113,6 @@ class ComponentController extends Controller
public function updateComponentAction(Component $component)
{
$componentData = Binput::get('component');
$tags = Arr::pull($componentData, 'tags');
try {
$component = execute(new UpdateComponentCommand(
@@ -126,6 +125,7 @@ class ComponentController extends Controller
$componentData['group_id'],
$componentData['enabled'],
null, // Meta data cannot be supplied through the dashboard yet.
$componentData['tags'], // Meta data cannot be supplied through the dashboard yet.
true // Silent since we're not really making changes to the component (this should be optional)
));
} catch (ValidationException $e) {
@@ -135,17 +135,6 @@ class ComponentController extends Controller
->withErrors($e->getMessageBag());
}
$component->tags()->delete();
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
return cachet_redirect('dashboard.components.edit', [$component->id])
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.edit.success')));
}
@@ -170,7 +159,6 @@ class ComponentController extends Controller
public function createComponentAction()
{
$componentData = Binput::get('component');
$tags = Arr::pull($componentData, 'tags');
try {
$component = execute(new CreateComponentCommand(
@@ -181,7 +169,8 @@ class ComponentController extends Controller
$componentData['order'],
$componentData['group_id'],
$componentData['enabled'],
null // Meta data cannot be supplied through the dashboard yet.
null, // Meta data cannot be supplied through the dashboard yet.
$componentData['tags']
));
} catch (ValidationException $e) {
return cachet_redirect('dashboard.components.create')
@@ -190,15 +179,6 @@ class ComponentController extends Controller
->withErrors($e->getMessageBag());
}
// The component was added successfully, so now let's deal with the tags.
Collection::make(preg_split('/ ?, ?/', $tags))->map(function ($tag) {
return trim($tag);
})->map(function ($tag) {
return execute(new CreateTagCommand($tag));
})->each(function ($tag) use ($component) {
execute(new ApplyTagCommand($component, $tag));
});
return cachet_redirect('dashboard.components')
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.components.add.success')));
}

View File

@@ -55,4 +55,31 @@ class Tag extends Model
{
return $this->belongsToMany(Component::class);
}
/**
* @param array|\ArrayAccess $values
*
* @return \CachetHQ\Cachet\Models\Tag|static
*/
public static function findOrCreate($values)
{
$tags = collect($values)->map(function ($value) {
if ($value instanceof Tag) {
return $value;
}
$tag = static::where('name', '=', $value)->first();
if (!$tag instanceof Tag) {
$tag = static::create([
'name' => $value,
'slug' => Str::slug($value),
]);
}
return $tag;
});
return is_string($values) ? $tags->first() : $tags;
}
}

View File

@@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Models\Traits;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
/**
* This is the has tags trait.
@@ -21,6 +22,33 @@ use Illuminate\Database\Eloquent\Builder;
*/
trait HasTags
{
/**
* @var array
*/
protected $queuedTags = [];
/**
* Boot the trait.
*
* @return void
*/
public static function bootHasTags()
{
static::created(function (Model $taggableModel) {
if (count($taggableModel->queuedTags) > 0) {
$taggableModel->attachTags($taggableModel->queuedTags);
$taggableModel->queuedTags = [];
}
});
static::deleted(function (Model $deletedModel) {
$tags = $deletedModel->tags()->get();
$deletedModel->detachTags($tags);
});
}
/**
* Get the tags relation.
*
@@ -31,6 +59,20 @@ trait HasTags
return $this->morphToMany(Tag::class, 'taggable');
}
/**
* @param string|array|\ArrayAccess|\CachetHQ\Cachet\Models\Tag $tags
*/
public function setTagsAttribute($tags)
{
if (! $this->exists) {
$this->queuedTags = $tags;
return;
}
$this->attachTags($tags);
}
/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array|\ArrayAccess $tags
@@ -61,12 +103,78 @@ trait HasTags
$tags = static::convertToTags($tags);
return $query->whereHas('tags', function (Builder $query) use ($tags) {
$tagIds = $tags->pluck('id')->toArray();
$tagIds = collect($tags)->pluck('id');
$query->whereIn('taggables.tag_id', $tagIds);
$query->whereIn('tags.id', $tagIds);
});
}
/**
* @param array|\ArrayAccess|\CachetHQ\Cachet\Models\Tag $tags
*
* @return $this
*/
public function attachTags($tags)
{
$tags = collect(Tag::findOrCreate($tags));
$this->tags()->syncWithoutDetaching($tags->pluck('id')->toArray());
return $this;
}
/**
* @param string|\CachetHQ\Cachet\Models\Tag $tag
*
* @return $this
*/
public function attachTag($tag)
{
return $this->attachTags([$tag]);
}
/**
* @param array|\ArrayAccess $tags
*
* @return $this
*/
public function detachTags($tags)
{
$tags = static::convertToTags($tags);
collect($tags)
->filter()
->each(function (Tag $tag) {
$this->tags()->detach($tag);
});
return $this;
}
/**
* @param string|\CachetHQ\Cachet\Models\Tag $tag
*
* @return $this
*/
public function detachTag($tag)
{
return $this->detachTags([$tag]);
}
/**
* @param array|\ArrayAccess $tags
*
* @return $this
*/
public function syncTags($tags)
{
$tags = collect(Tag::findOrCreate($tags));
$this->tags()->sync($tags->pluck('id')->toArray());
return $this;
}
/**
* Convert a list of tags into a collection of \CachetHQ\Cachet\Models\Tag.
*

View File

@@ -37,6 +37,7 @@ class CreateComponentCommandTest extends AbstractTestCase
'group_id' => 0,
'enabled' => true,
'meta' => null,
'tags' => 'Foo, Bar',
];
$object = new CreateComponentCommand(
$params['name'],
@@ -46,7 +47,8 @@ class CreateComponentCommandTest extends AbstractTestCase
$params['order'],
$params['group_id'],
$params['enabled'],
$params['meta']
$params['meta'],
$params['tags']
);
return compact('params', 'object');

View File

@@ -39,6 +39,7 @@ class UpdateComponentCommandTest extends AbstractTestCase
'group_id' => 0,
'enabled' => true,
'meta' => null,
'tags' => null,
'silent' => false,
];
@@ -52,6 +53,7 @@ class UpdateComponentCommandTest extends AbstractTestCase
$params['group_id'],
$params['enabled'],
$params['meta'],
$params['tags'],
$params['silent']
);