我目前正在阅读Adam Wathan写的《重构集合》一书,并试图通过对正在研究的项目实施高阶函数技术来进行重构。
请求流程:
//this is storing new property feature
protected $property;
public function handle(Request $request)
{
try {
$databaseConnection->transaction(function() use ($request) {
$this->property = $this->run(StorePropertyJob::class, [
'property' => $this->property,
'request' => $request
]);
//this will call to an application API
$this->run(PushAPICallJob::class, [
'propertyId' => $this->propertyId,
'name' => $request->input('name'),
'location' => $request->input('location')
]);
//this will call to separate application API
$this->run(AnotherPushAPICallJob::class, [
'propertyId' => $this->propertyId,
//request will be different
]);
flash('Successfully created '.$request->input('name'));
return redirect(route('property.index'));
});
} catch (ApiCallException $e) {
//logging api error?
flash('Unable to push to do API call');
} catch (AnotherApiCallException $e) {
//logging api error?
flash('Unable to push to do another API call');
} catch (Exception $e) {
//log the error
flash('Something went wrong!');
}
return back()->withInput();
}
//PushApiCalJob class extending a parent class that has all guzzle set
protected $propertyId;
protected $name;
protected $location;
protected $endPoint = '/property';
public function __construct($propertyId, $name, $location)
{
$this->requestType = 'POST';
$this->path = $this->path.$this->endPoint;
$this->successfulStatusCode = Response::HTTP_CREATED;
$this->body = [
'body' => [
'propertyId' => $propertyId,
'name' => $name,
'location' => $location
]
];
}
public function handle()
{
$request = $this->client->request($this->requestType, $this->path, $this->body);
$statusCode = $request->getStatusCode();
if($statusCode == $this->successfulStatusCode) {
//this is just basically getting request body content and returning it
} elseif($statusCode == Request::HTTP_CONFLICT) {
//this is if it is already existing in the other system
//need to do logging here - this is a problem
//this is the return (check point 1 comment below)
} else {
//logging for any error - this is also a problem
//should I throw an exception here?
}
throw new ApiCallException();
}
现在我的问题是: