-
-
Save mdhafiz/ae47383e57894506f3df74017ab2c751 to your computer and use it in GitHub Desktop.
Controller architecture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Space\Repositories\Eloquent; | |
abstract class AbstractQueries | |
{ | |
protected $model; | |
/** | |
* AbstractQueries constructor. | |
* | |
* @param $model | |
*/ | |
public function __construct($model) | |
{ | |
$this->model = "App\\$model"; | |
} | |
/** | |
* Find a single resource by ID. | |
* | |
* @param $id | |
* @return mixed | null | |
*/ | |
public function findById($id) | |
{ | |
return $this->model::find($id) ?: null; | |
} | |
/** | |
* Find a single url by key. | |
* | |
* @param $key | |
* @return mixed | null | |
*/ | |
public function findByKey($key) | |
{ | |
return $this->model::byKey($key) ?: null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers\Admin; | |
use App\Exceptions\ModelException; | |
use App\Http\Controllers\DataTableController; | |
use App\Http\Requests\Admin\RemoteDomainStoreRequest; | |
use App\Http\Requests\Admin\RemoteDomainUpdateRequest; | |
use App\Space\Repositories\Interfaces\Admin\RemoteDomainRepoInterface; | |
use Illuminate\Http\JsonResponse; | |
use Illuminate\View\View; | |
use Tests\Feature\Http\Admin\RemoteDomainControllerTest; | |
/** | |
* Class RemoteDomainController | |
* | |
* Remote domain operations. | |
* | |
* @see RemoteDomainControllerTest | |
* @package App\Http\Controllers\Admin | |
*/ | |
class RemoteDomainController extends DataTableController | |
{ | |
protected $remoteDomainRepo; | |
/** | |
* RemoteDomainController constructor. | |
* | |
* @param RemoteDomainRepoInterface $remoteDomainRepo | |
*/ | |
public function __construct(RemoteDomainRepoInterface $remoteDomainRepo) | |
{ | |
$this->remoteDomainRepo = $remoteDomainRepo; | |
} | |
/** | |
* Delete a remote domain. | |
* | |
* @see RemoteDomainControllerTest::unauthenticated_user_cannot_delete_a_remote_domain() | |
* @see RemoteDomainControllerTest::will_fail_when_trying_to_delete_a_remote_domain_that_does_not_exist() | |
* @see RemoteDomainControllerTest::can_delete_a_remote_domain() | |
* | |
* @param $id | |
* @return JsonResponse | |
* @throws ModelException | |
*/ | |
public function destroy($id): JsonResponse | |
{ | |
if (($user = $this->remoteDomainRepo->findById($id)) == null) | |
throw new ModelException(); | |
try { | |
$this->remoteDomainRepo->delete($user); | |
return $this->dataTableSuccessResponse(); | |
} catch (\Exception $e) { | |
return $this->dataTableErrorResponse($e); | |
} | |
} | |
/** | |
* Master view for remote domains. | |
* | |
* @see RemoteDomainControllerTest::unauthenticated_user_cannot_view_remote_domains() | |
* @see RemoteDomainControllerTest::can_view_remote_domains() | |
* | |
* @return View | |
*/ | |
public function index(): View | |
{ | |
return view('admin.remotedomains.index'); | |
} | |
/** | |
* Create a new remote domain. | |
* | |
* @see RemoteDomainControllerTest::unauthenticated_user_cannot_create_remote_domain() | |
* @see RemoteDomainControllerTest::can_create_remote_domain() | |
* | |
* @param RemoteDomainStoreRequest $request | |
* @return JsonResponse | |
*/ | |
public function store(RemoteDomainStoreRequest $request): JsonResponse | |
{ | |
try { | |
$this->remoteDomainRepo->create($request); | |
return $this->dataTableSuccessResponse(); | |
} catch (\Exception $e) { | |
return $this->dataTableErrorResponse($e); | |
} | |
} | |
/** | |
* Update a remote domain. | |
* | |
* @see RemoteDomainControllerTest::unauthenticated_user_cannot_update_remote_domain() | |
* @see RemoteDomainControllerTest::will_fail_when_trying_to_update_a_remote_domain_that_does_not_exist() | |
* @see RemoteDomainControllerTest::can_update_all_fields_for_a_remote_domain() | |
* | |
* @param RemoteDomainUpdateRequest $request | |
* @param $id | |
* @return JsonResponse | |
*/ | |
public function update(RemoteDomainUpdateRequest $request, $id): JsonResponse | |
{ | |
try { | |
$this->remoteDomainRepo->update($request, $request->remoteDomain); | |
return $this->dataTableSuccessResponse(); | |
} catch (\Exception $e) { | |
return $this->dataTableErrorResponse($e); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Space\Repositories\Eloquent\Admin; | |
use App\Http\Requests\Admin\RemoteDomainStoreRequest; | |
use App\Http\Requests\Admin\RemoteDomainUpdateRequest; | |
use App\RemoteDomain; | |
use App\Space\Repositories\Eloquent\AbstractQueries; | |
use App\Space\Repositories\Interfaces\Admin\RemoteDomainRepoInterface; | |
/** | |
* Class RemoteDomainRepo | |
* | |
* Remote domain operations. | |
* | |
* @package App\Space\Repositories\Eloquent\Admin | |
*/ | |
class RemoteDomainRepo extends AbstractQueries implements RemoteDomainRepoInterface | |
{ | |
public function __construct() | |
{ | |
parent::__construct('Remotedomain'); | |
} | |
/** | |
* Create a new remote domain. | |
* | |
* @param RemoteDomainStoreRequest $request | |
*/ | |
public function create(RemoteDomainStoreRequest $request): void | |
{ | |
RemoteDomain::create([ | |
'ip' => $request->ip, | |
'domain' => $request->domain, | |
'description' => $request->description, | |
'status' => $request->status, | |
'key' => $request->key, | |
'secret' => $request->secret, | |
'user_id' => authUser()->id | |
]); | |
} | |
/** | |
* Delete a remote domain. | |
* | |
* @param RemoteDomain $remoteDomain | |
* @throws \Exception | |
*/ | |
public function delete(RemoteDomain $remoteDomain): void | |
{ | |
$remoteDomain->delete(); | |
} | |
/** | |
* Update a remote domain. | |
* | |
* @param RemoteDomainUpdateRequest $request | |
* @param RemoteDomain $remoteDomain | |
*/ | |
public function update(RemoteDomainUpdateRequest $request, RemoteDomain $remoteDomain): void | |
{ | |
$remoteDomain->update([ | |
'ip' => $request->ip, | |
'domain' => $request->domain, | |
'description' => $request->description, | |
'status' => $request->status, | |
'key' => $request->key, | |
'secret' => $request->secret, | |
'user_id' => authUser()->id | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Space\Repositories\Interfaces\Admin; | |
use App\Http\Requests\Admin\RemoteDomainStoreRequest; | |
use App\Http\Requests\Admin\RemoteDomainUpdateRequest; | |
use App\RemoteDomain; | |
/** | |
* Interface RemoteDomainRepoInterface | |
* | |
* Remote domain operations. | |
* | |
* @package App\Space\Repositories\Interfaces\Admin | |
*/ | |
interface RemoteDomainRepoInterface | |
{ | |
/** | |
* Create a new remote domain. | |
* | |
* @param RemoteDomainStoreRequest $request | |
*/ | |
public function create(RemoteDomainStoreRequest $request): void; | |
/** | |
* Delete a remote domain. | |
* | |
* @param RemoteDomain $remoteDomain | |
*/ | |
public function delete(RemoteDomain $remoteDomain): void; | |
/** | |
* Update a remote domain. | |
* | |
* @param RemoteDomainUpdateRequest $request | |
* @param RemoteDomain $remoteDomain | |
*/ | |
public function update(RemoteDomainUpdateRequest $request, RemoteDomain $remoteDomain): void; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment