Reputation: 10828
In many methods in the service classes I am repeating the code like this:
$model->addEvent($provider->id, 'installing', 'username', 'add', 'Description of event');
$this->worker->uploadFile($provider->id, 'root', $file);
Many different $model
will have addEvent()
which is done via trait.
How can I refactor those two lines into a method with readable/memorable parameters?
I have tried the following:
public function deploy($model, $providerId, $status, $user, $action, $file, $description = null)
{
$model->addEvent($providerId, $status, $user, $action, $description);
$this->serverWorker->uploadFile($providerId, $user, $file);
}
What I don't like about this method that there is too many params.
Usage:
Eg 1. deploy($site, 1, 'In Queue', 'root', 'create', $file, 'Installing Site domain.com')
Eg 2. deploy($rule, 1, 'In Queue', 'root', 'update', $file, 'Updating Rule')
Eg 2. deploy($something, 1, 'In Queue', 'root', 'delete', $file)
Upvotes: 1
Views: 61
Reputation: 587
You could try wrapping the common configurations into small reusable classes like so:
public function deploy($model, FileDeployTarget $target, $description = null)
{
$model->addEvent($target->providerId, $target->status, $target->user, $target->action, $description);
$this->serverWorker->uploadFile($target->providerId, $target->user, $target->file);
}
And somewhere else:
class UpdateInQueue extends FileDeployTarget {
public $status = 'In Queue';
public $action = 'update';
}
FileDeployTarget and descendants would handle all extra parameters in their constructors.
Upvotes: 1