mshamaseen /
laravel-repositories
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | /** @noinspection PhpMultipleClassDeclarationsInspection */ |
||||||
| 4 | |||||||
| 5 | namespace Shamaseen\Repository\Utility; |
||||||
| 6 | |||||||
| 7 | use Exception; |
||||||
| 8 | use Illuminate\Auth\Access\AuthorizationException; |
||||||
| 9 | use Illuminate\Contracts\View\View; |
||||||
| 10 | use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
||||||
| 11 | use Illuminate\Foundation\Bus\DispatchesJobs; |
||||||
| 12 | use Illuminate\Foundation\Validation\ValidatesRequests; |
||||||
| 13 | use Illuminate\Http\JsonResponse; |
||||||
| 14 | use Illuminate\Http\RedirectResponse; |
||||||
| 15 | use Illuminate\Http\Resources\Json\JsonResource; |
||||||
| 16 | use Illuminate\Routing\Controller as LaravelController; |
||||||
| 17 | use Illuminate\Support\Collection; |
||||||
| 18 | use Illuminate\Support\Facades\App; |
||||||
| 19 | use Illuminate\Support\Facades\Gate; |
||||||
| 20 | use Illuminate\Support\Facades\Route; |
||||||
| 21 | use Symfony\Component\HttpFoundation\Response; |
||||||
| 22 | |||||||
| 23 | /** |
||||||
| 24 | * Class BaseController. |
||||||
| 25 | */ |
||||||
| 26 | class Controller extends LaravelController |
||||||
| 27 | { |
||||||
| 28 | use AuthorizesRequests; |
||||||
| 29 | use DispatchesJobs; |
||||||
| 30 | use ValidatesRequests; |
||||||
| 31 | |||||||
| 32 | public AbstractRepository $repository; |
||||||
| 33 | public Request $request; |
||||||
| 34 | |||||||
| 35 | public int $limit = 10; |
||||||
| 36 | public int $maxLimit = 100; |
||||||
| 37 | |||||||
| 38 | public string $pageTitle = ''; |
||||||
| 39 | public Collection $breadcrumbs; |
||||||
| 40 | |||||||
| 41 | /** |
||||||
| 42 | * Can be either a route name or a URL. |
||||||
| 43 | */ |
||||||
| 44 | public string $routeIndex = ''; |
||||||
| 45 | public string $createRoute = ''; |
||||||
| 46 | |||||||
| 47 | public string $viewIndex = ''; |
||||||
| 48 | public string $viewCreate = ''; |
||||||
| 49 | public string $viewEdit = ''; |
||||||
| 50 | public string $viewShow = ''; |
||||||
| 51 | |||||||
| 52 | public bool $isAPI = false; |
||||||
| 53 | |||||||
| 54 | public ?string $paginateType = null; |
||||||
| 55 | |||||||
| 56 | /** |
||||||
| 57 | * Allow returning trash if the frontend user request it. |
||||||
| 58 | */ |
||||||
| 59 | public bool $allowTrashRequest = false; |
||||||
| 60 | |||||||
| 61 | public array $params = []; |
||||||
| 62 | |||||||
| 63 | public ResponseDispatcher $responseDispatcher; |
||||||
| 64 | public string $requestClass = Request::class; |
||||||
| 65 | public ?string $policyClass = null; |
||||||
| 66 | public ?string $resourceClass = Resource::class; |
||||||
| 67 | public ?string $collectionClass = ResourceCollection::class; |
||||||
| 68 | |||||||
| 69 | /** |
||||||
| 70 | * BaseController constructor. |
||||||
| 71 | */ |
||||||
| 72 | public function __construct(AbstractRepository $repository) |
||||||
| 73 | { |
||||||
| 74 | $this->repository = $repository; |
||||||
| 75 | $this->breadcrumbs = new Collection(); |
||||||
| 76 | $this->paginateType = $this->paginateType ?? config('repository.default_pagination'); |
||||||
| 77 | } |
||||||
| 78 | |||||||
| 79 | /** |
||||||
| 80 | * @throws AuthorizationException |
||||||
| 81 | */ |
||||||
| 82 | public function authorizeAction(string $action): void |
||||||
| 83 | { |
||||||
| 84 | if ($this->policyClass && method_exists($this->policyClass, $action)) { |
||||||
| 85 | Gate::policy($this->repository->getModelClass(), $this->policyClass) |
||||||
| 86 | ->authorize($action, $this->repository->getModelClass()); |
||||||
| 87 | } |
||||||
| 88 | } |
||||||
| 89 | |||||||
| 90 | /** |
||||||
| 91 | * Any data that depend on the request instance should be inside this callback, |
||||||
| 92 | * Request instance should be initialized after other middlewares. |
||||||
| 93 | * |
||||||
| 94 | * @return Response |
||||||
| 95 | * |
||||||
| 96 | * @throws AuthorizationException |
||||||
| 97 | */ |
||||||
| 98 | public function callAction($method, $parameters) |
||||||
| 99 | { |
||||||
| 100 | $this->request = App::make($this->requestClass, ['repository' => $this->repository]); |
||||||
| 101 | |||||||
| 102 | $this->authorizeAction($method); |
||||||
| 103 | |||||||
| 104 | $this->isAPI = $this->request->expectsJson(); |
||||||
| 105 | $this->limit = min($this->request->get('limit', $this->limit), $this->maxLimit); |
||||||
| 106 | |||||||
| 107 | if ($this->allowTrashRequest) { |
||||||
| 108 | if ($this->request->get('with-trash', false)) { |
||||||
| 109 | $this->repository->withTrash(); |
||||||
|
0 ignored issues
–
show
|
|||||||
| 110 | } |
||||||
| 111 | |||||||
| 112 | if ($this->request->get('only-trash', false)) { |
||||||
| 113 | $this->repository->onlyTrash(); |
||||||
|
0 ignored issues
–
show
The method
onlyTrash() does not exist on Shamaseen\Repository\Utility\AbstractRepository.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 114 | } |
||||||
| 115 | } |
||||||
| 116 | |||||||
| 117 | $this->request->offsetUnset('only-trash'); |
||||||
| 118 | $this->request->offsetUnset('with-trash'); |
||||||
| 119 | $this->request->offsetUnset('limit'); |
||||||
| 120 | $this->responseDispatcher = new ResponseDispatcher($this); |
||||||
| 121 | |||||||
| 122 | return parent::callAction($method, $parameters); |
||||||
| 123 | } |
||||||
| 124 | |||||||
| 125 | /** |
||||||
| 126 | * Display a listing of the model. |
||||||
| 127 | */ |
||||||
| 128 | public function index(): mixed |
||||||
| 129 | { |
||||||
| 130 | $this->breadcrumbs->put('index', [ |
||||||
|
0 ignored issues
–
show
'index' of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
array('link' => $this->r...t' => $this->pageTitle) of type array<string,string> is incompatible with the type Illuminate\Support\TValue expected by parameter $value of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 131 | 'link' => $this->resolveRoute($this->routeIndex), |
||||||
| 132 | 'text' => $this->pageTitle, |
||||||
| 133 | ]); |
||||||
| 134 | |||||||
| 135 | if ('simple' === $this->paginateType) { |
||||||
| 136 | $paginate = $this->repository->simplePaginate($this->limit, $this->request->all()); |
||||||
| 137 | } else { |
||||||
| 138 | $paginate = $this->repository->paginate($this->limit, $this->request->all()); |
||||||
| 139 | } |
||||||
| 140 | |||||||
| 141 | return $this->responseDispatcher->index($paginate); |
||||||
| 142 | } |
||||||
| 143 | |||||||
| 144 | /** |
||||||
| 145 | * Display the specified resource. |
||||||
| 146 | */ |
||||||
| 147 | public function show(int|string $entityId): mixed |
||||||
| 148 | { |
||||||
| 149 | $this->breadcrumbs->put('view', [ |
||||||
|
0 ignored issues
–
show
'view' of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
array('link' => '', 'tex... __('repository.show')) of type array<string,string> is incompatible with the type Illuminate\Support\TValue expected by parameter $value of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 150 | 'link' => '', |
||||||
| 151 | 'text' => __('repository.show'), |
||||||
| 152 | ]); |
||||||
| 153 | |||||||
| 154 | $entity = $this->repository->findOrFail($entityId); |
||||||
| 155 | |||||||
| 156 | return $this->responseDispatcher->show($entity); |
||||||
| 157 | } |
||||||
| 158 | |||||||
| 159 | /** |
||||||
| 160 | * Show the form to create a new resource, only for web responses. |
||||||
| 161 | */ |
||||||
| 162 | public function create(): mixed |
||||||
| 163 | { |
||||||
| 164 | $this->breadcrumbs->put('create', [ |
||||||
|
0 ignored issues
–
show
array('link' => $this->r...s('repository.create')) of type array<string,string> is incompatible with the type Illuminate\Support\TValue expected by parameter $value of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
'create' of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 165 | 'link' => $this->resolveRoute($this->createRoute), |
||||||
| 166 | 'text' => trans('repository.create'), |
||||||
| 167 | ]); |
||||||
| 168 | |||||||
| 169 | return $this->responseDispatcher->create(); |
||||||
| 170 | } |
||||||
| 171 | |||||||
| 172 | /** |
||||||
| 173 | * Store a newly created resource in storage. |
||||||
| 174 | */ |
||||||
| 175 | public function store(): mixed |
||||||
| 176 | { |
||||||
| 177 | $entity = $this->repository->create($this->request->except(['_token', '_method'])); |
||||||
| 178 | |||||||
| 179 | return $this->responseDispatcher->store($entity); |
||||||
| 180 | } |
||||||
| 181 | |||||||
| 182 | /** |
||||||
| 183 | * Show the form for editing the specified resource. |
||||||
| 184 | */ |
||||||
| 185 | public function edit(int|string $entityId): mixed |
||||||
| 186 | { |
||||||
| 187 | $this->breadcrumbs->put('edit', [ |
||||||
|
0 ignored issues
–
show
'edit' of type string is incompatible with the type Illuminate\Support\TKey expected by parameter $key of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
array('link' => '', 'tex... __('repository.edit')) of type array<string,string> is incompatible with the type Illuminate\Support\TValue expected by parameter $value of Illuminate\Support\Collection::put().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 188 | 'link' => '', |
||||||
| 189 | 'text' => __('repository.edit'), |
||||||
| 190 | ]); |
||||||
| 191 | |||||||
| 192 | $entity = $this->repository->findOrFail($entityId); |
||||||
| 193 | |||||||
| 194 | return $this->responseDispatcher->edit($entity); |
||||||
| 195 | } |
||||||
| 196 | |||||||
| 197 | /** |
||||||
| 198 | * Update the specified resource in storage. |
||||||
| 199 | */ |
||||||
| 200 | public function update(int|string $entityId): mixed |
||||||
| 201 | { |
||||||
| 202 | $updatedCount = $this->repository->update($entityId, $this->request->except(['_token', '_method'])); |
||||||
| 203 | |||||||
| 204 | return $this->responseDispatcher->update($updatedCount); |
||||||
| 205 | } |
||||||
| 206 | |||||||
| 207 | /** |
||||||
| 208 | * Remove the specified resource from storage. |
||||||
| 209 | * |
||||||
| 210 | * @throws Exception |
||||||
| 211 | */ |
||||||
| 212 | public function destroy(int|string $entityId): mixed |
||||||
| 213 | { |
||||||
| 214 | $deletedCount = $this->repository->delete($entityId); |
||||||
| 215 | |||||||
| 216 | return $this->responseDispatcher->destroy($deletedCount); |
||||||
| 217 | } |
||||||
| 218 | |||||||
| 219 | /** |
||||||
| 220 | * Return a URI from a route or URL. |
||||||
| 221 | */ |
||||||
| 222 | public function resolveRoute(string $route): string |
||||||
| 223 | { |
||||||
| 224 | return Route::has($route) ? route($route) : $route; |
||||||
| 225 | } |
||||||
| 226 | } |
||||||
| 227 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.