Remove old Tag commands, refactor migration etc

This commit is contained in:
James Brooks 2019-07-12 11:08:25 +01:00
parent 32a3adb43c
commit 3ac7492858
17 changed files with 5 additions and 680 deletions

View File

@ -1,51 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Database\Eloquent\Model;
/**
* This is the apply tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class ApplyTagCommand
{
/**
* The model to apply the tag to.
*
* @var \Illuminate\Database\Eloquent\Model
*/
public $model;
/**
* The tag to apply.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* Create a new apply tag command instance.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \CachetHQ\Cachet\Models\Tag $tag
*
* @return void
*/
public function __construct(Model $model, Tag $tag)
{
$this->model = $model;
$this->tag = $tag;
}
}

View File

@ -1,48 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
/**
* This is the create tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class CreateTagCommand
{
/**
* The tag name.
*
* @var string
*/
public $name;
/**
* The tag slug.
*
* @var string|null
*/
public $slug;
/**
* Create a new create tag command instance.
*
* @param string $name
* @param string|null $slug
*
* @return void
*/
public function __construct($name, $slug = null)
{
$this->name = $name;
$this->slug = $slug;
}
}

View File

@ -1,41 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
/**
* This is the delete tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class DeleteTagCommand
{
/**
* The tag.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* Create a new delete tag command instance.
*
* @param \CachetHQ\Cachet\Models\Tag $tag
*
* @return void
*/
public function __construct(Tag $tag)
{
$this->tag = $tag;
}
}

View File

@ -1,59 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Commands\Tag;
use CachetHQ\Cachet\Models\Tag;
/**
* This is the update tag coommand class.
*
* @author James Brooks <james@alt-three.com>
*/
final class UpdateTagCommand
{
/**
* The tag.
*
* @var \CachetHQ\Cachet\Models\Tag
*/
public $tag;
/**
* The new tag name.
*
* @var string|null
*/
public $name;
/**
* The new tag slug.
*
* @var string|null
*/
public $slug;
/**
* Create a new update tag command instance.
*
* @param \CachetHQ\Cachet\Models\Tag $tag
* @param string|null $name
* @param string|null $slug
*
* @return void
*/
public function __construct(Tag $tag, $name, $slug)
{
$this->tag = $tag;
$this->name = $name;
$this->slug = $slug;
}
}

View File

@ -1,39 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Models\Taggable;
/**
* This is the apply tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class ApplyTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand $command
*
* @return void
*/
public function handle(ApplyTagCommand $command)
{
Taggable::firstOrCreate([
'tag_id' => $command->tag->id,
'taggable_id' => $command->model->id,
'taggable_type' => $command->model->getTable(),
]);
}
}

View File

@ -1,39 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Tag;
use Illuminate\Support\Str;
/**
* This is the create tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class CreateTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand $command
*
* @return \CachetHQ\Cachet\Models\Tag
*/
public function handle(CreateTagCommand $command)
{
return Tag::firstOrCreate([
'name' => $command->name,
'slug' => $command->slug ? $command->slug : Str::slug($command->name),
]);
}
}

View File

@ -1,34 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand;
/**
* This is the delete tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class DeleteTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand $command
*
* @return \CachetHQ\Cachet\Models\Tag
*/
public function handle(DeleteTagCommand $command)
{
$command->tag->delete();
}
}

View File

@ -1,38 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Bus\Handlers\Commands\Tag;
use CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand;
use Illuminate\Support\Str;
/**
* This is the create tag command handler class.
*
* @author James Brooks <james@alt-three.com>
*/
class UpdateTagCommandHandler
{
/**
* Handle the command.
*
* @param \CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand $command
*
* @return void
*/
public function handle(UpdateTagCommand $command)
{
return $command->tag->update([
'name' => $command->name,
'slug' => $command->slug ? $command->slug : Str::slug($command->name),
]);
}
}

View File

@ -14,8 +14,6 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Component;
use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;

View File

@ -15,8 +15,6 @@ use AltThree\Validator\ValidationException;
use CachetHQ\Cachet\Bus\Commands\Component\CreateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\RemoveComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Component\UpdateComponentCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use GrahamCampbell\Binput\Facades\Binput;

View File

@ -1,79 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Models;
use AltThree\Validator\ValidatingTrait;
use Illuminate\Database\Eloquent\Model;
/**
* This is the taggable model class.
*
* @author James Brooks <james@alt-three.com>
*/
class Taggable extends Model
{
use ValidatingTrait;
/**
* The attributes that should be casted to native types.
*
* @var string[]
*/
protected $casts = [
'id' => 'int',
'tag_id' => 'int',
'taggable_id' => 'int',
'taggable_type' => 'string',
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'tag_id',
'taggable_id',
'taggable_type',
];
/**
* The validation rules.
*
* @var string[]
*/
public $rules = [
'tag_id' => 'required|int',
'taggable_id' => 'required|int',
'taggable_type' => 'required|string',
];
/**
* Get the tag relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function tag()
{
return $this->belongsTo(Tag::class);
}
/**
* Get the taggable relation.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function taggable()
{
return $this->morphTo();
}
}

View File

@ -9,7 +9,6 @@
* file that was distributed with this source code.
*/
use CachetHQ\Cachet\Models\Taggable;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
@ -24,15 +23,16 @@ class MigrateComponentTagTable extends Migration
*/
public function up()
{
// Start by migrating the data into the new taggables field.
DB::table('component_tag')->get()->each(function ($tag) {
Taggable::create([
$tags = DB::table('component_tag')->get()->map(function ($tag) {
return [
'tag_id' => $tag->tag_id,
'taggable_type' => 'components',
'taggable_id' => $tag->component_id,
]);
];
});
DB::table('taggables')->insert($tags);
Schema::dropIfExists('component_tag');
}

View File

@ -1,54 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\ApplyTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\ApplyTagCommandHandler;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the apply tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class ApplyTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'model' => new Component(),
'tag' => new Tag(),
];
$object = new ApplyTagCommand(
$params['model'],
$params['tag']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return ApplyTagCommandHandler::class;
}
}

View File

@ -1,52 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\CreateTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\CreateTagCommandHandler;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the create tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class CreateTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'name' => 'Test',
'slug' => 'test',
];
$object = new CreateTagCommand(
$params['name'],
$params['slug']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return CreateTagCommandHandler::class;
}
}

View File

@ -1,51 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\DeleteTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\DeleteTagCommandHandler;
use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the delete tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class DeleteTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'tag' => new Tag(),
];
$object = new DeleteTagCommand(
$params['tag']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return DeleteTagCommandHandler::class;
}
}

View File

@ -1,55 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Bus\Commands\Tag;
use AltThree\TestBench\CommandTrait;
use CachetHQ\Cachet\Bus\Commands\Tag\UpdateTagCommand;
use CachetHQ\Cachet\Bus\Handlers\Commands\Tag\UpdateTagCommandHandler;
use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the update tag command test class.
*
* @author James Brooks <james@alt-three.com>
*/
class UpdateTagCommandTest extends AbstractTestCase
{
use CommandTrait;
protected function getObjectAndParams()
{
$params = [
'tag' => new Tag(),
'name' => 'Test',
'slug' => 'test',
];
$object = new UpdateTagCommand(
$params['tag'],
$params['name'],
$params['slug']
);
return compact('params', 'object');
}
protected function objectHasRules()
{
return false;
}
protected function getHandlerClass()
{
return UpdateTagCommandHandler::class;
}
}

View File

@ -1,31 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Tests\Cachet\Models;
use AltThree\TestBench\ValidationTrait;
use CachetHQ\Cachet\Models\Taggable;
use CachetHQ\Tests\Cachet\AbstractTestCase;
/**
* This is the taggable model test class.
*
* @author James Brooks <james@alt-three.com>
*/
class TaggableTest extends AbstractTestCase
{
use ValidationTrait;
public function testValidation()
{
$this->checkRules(new Taggable());
}
}