我试图找到一种重构此代码的方法,因此我不会在很多地方重复相同的代码。我正在寻找DRY原理。
这是createDaemon()
方法的示例。
function createDaemon($server, $command, $user)
{
try {
DB::beginTransaction();
$model = $server->daemons()->create([
'command' => $command,
'user' => $user,
]);
$shell = $this->getCommand('add-daemon', [
'daemonId' => $daemon->id,
'command' => $command,
'user' => $user,
]);
$this->pushToQueue($model, $shell);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
return $model;
}
这是另一个类中createRule()
的另一个示例,因为您可以看到代码几乎相同。如何将其重构为DRY原理-您会创建一个新的方法或类来执行相同的逻辑吗?
public function createRule($server, $name, $port, $ipAddress = null)
{
try {
DB::beginTransaction();
$model = $server->rule()->create([
'name' => $name,
'port' => $port,
]);
$shell = $this->getCommand('rule', [
'port' => $port,
'ipAddress' => $ipAddress
]);
$this->pushToQueue($model, $shell);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
return $model;
}
答案 0 :(得分:1)
我想我会创建一个像这样的通用方法:
public function createGeneralRule(Closure $closure)
{
try {
DB::beginTransaction();
[$model, $shell] = $closure();
$this->pushToQueue($model, $shell);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
return $model;
}
现在您可以像这样使用它:
function createDaemon($server, $command, $user)
{
return $this->createGeneralRule(function() use ($server, $command, $user) {
$model = $server->daemons()->create([
'command' => $command,
'user' => $user,
]);
$shell = $this->getCommand('add-daemon', [
'daemonId' => $daemon->id,
'command' => $command,
'user' => $user,
]);
return [$model, $shell];
}
}
和
public function createRule($server, $name, $port, $ipAddress = null)
{
return $this->createGeneralRule(function() use ($server, $name, $port, $ipAddress) {
$model = $server->rule()->create([
'name' => $name,
'port' => $port,
]);
$shell = $this->getCommand('rule', [
'port' => $port,
'ipAddress' => $ipAddress
]);
return [$model, $shell];
}
}
当然,您也可以使用类,但这实际上取决于您将重用此代码多少次以及真正需要什么灵活性。
使用类可能是这样的:
abstract class Rule
{
public function process()
{
try {
DB::beginTransaction();
$model = $this->model();
$shell = $this->shell();
$this->pushToQueue($model, $shell);
DB::commit();
} catch (\Exception $e) {
DB::rollback();
throw $e;
}
return $model;
}
protected function getCommand($name, $data)
{
// here you put implementation you had before of getCommand
}
abstract protected function model();
abstract protected function shell();
}
class Deamon extends Rule
{
protected $server;
protected $command;
protected $user;
public function __construct($server, $command, $user)
{
$this->server = $server;
$this->command = $command;
$this->user = $user;
}
protected function model()
{
return $this->server->daemons()->create([
'command' => $this->command,
'user' => $this->user,
]);
}
protected function shell()
{
return $this->getCommand('add-daemon', [
'daemonId' => $daemon->id, // this is unknown, should be passed in constructor?
'command' => $this->command,
'user' => $this->user,
]);
}
}
,在您的控制器中,您将像这样使用它:
(新的Deamon($ server,$ command,$ user))-> process();
以防万一-请记住,您有$deamon
变量未定义(在控制器中也未定义)
答案 1 :(得分:1)
扩展
您可以使它们扩展相同的基类:
class foo{
public function myMethod(){}
}
class bar extends foo{ }
class biz extends foo{ }
现在两个子类都具有方法myMethod
特质
您可以将特征用于共享功能
trait foo{
public function myMethod(){}
}
class bar{
use foo;
}
class biz{
use foo;
}
关于实际功能,我将其分为3种方法: 我打算在上面写一些东西,但是我看到@MarcinNabiałek在这方面有很好的答案。我只想介绍如何构造类以重新使用通用方法。
干杯。