cherrypulp /
laravel-mock
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Blok\Mock\Http\Controllers; |
||
| 4 | |||
| 5 | use Blok\Mock\Mock; |
||
| 6 | use Illuminate\Foundation\Bus\DispatchesJobs; |
||
| 7 | use Illuminate\Pagination\LengthAwarePaginator; |
||
| 8 | use Illuminate\Routing\Controller as BaseController; |
||
| 9 | use Illuminate\Foundation\Validation\ValidatesRequests; |
||
| 10 | use Illuminate\Foundation\Auth\Access\AuthorizesRequests; |
||
| 11 | use Illuminate\Http\Request; |
||
| 12 | use Illuminate\Support\Collection; |
||
| 13 | use Symfony\Component\HttpFoundation\HeaderBag; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Data mock controller into json (for testing an mocking) |
||
| 17 | */ |
||
| 18 | class MockController extends BaseController |
||
| 19 | { |
||
| 20 | use AuthorizesRequests, DispatchesJobs, ValidatesRequests; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @param $table |
||
| 24 | * @param $method |
||
| 25 | * @param $request Request |
||
| 26 | * @throws \Illuminate\Validation\ValidationException |
||
| 27 | */ |
||
| 28 | public function _handleValidation($table, $method, $request){ |
||
| 29 | |||
| 30 | $validation = config("mock.entrypoints.".$table.".".$method."_validation"); |
||
| 31 | |||
| 32 | if (config("mock.force_json") || config("mock.entrypoints.".$table.".".$method."_force_json")) { |
||
| 33 | $request->server->set('HTTP_ACCEPT', 'application/json'); |
||
| 34 | $request->headers = new HeaderBag($request->server->getHeaders()); |
||
| 35 | } |
||
| 36 | |||
| 37 | if ($validation) { |
||
| 38 | if (is_array($validation)) { |
||
| 39 | $this->validate($request, $validation); |
||
| 40 | } else { |
||
| 41 | |||
| 42 | $formRequest = new $validation($request->query->all(), $request->request->all(), $request->attributes->all(), $request->cookies->all(), $request->files->all(), $request->server->all(), $request->getContent()); |
||
| 43 | |||
| 44 | $this->validate($formRequest, $formRequest->rules()); |
||
| 45 | } |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param $table |
||
| 51 | * @param Request $request |
||
| 52 | * @return array|\Illuminate\Http\JsonResponse|LengthAwarePaginator|Collection|mixed |
||
| 53 | * @throws \Illuminate\Validation\ValidationException |
||
| 54 | */ |
||
| 55 | public function index($table, Request $request) |
||
| 56 | { |
||
| 57 | $this->_handleValidation($table, "index", $request); |
||
| 58 | |||
| 59 | $entity = config("mock.entrypoints.".$table); |
||
| 60 | |||
| 61 | if ($entity) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var $data Collection |
||
| 65 | */ |
||
| 66 | $data = factory($entity["class"], $entity["number"])->states($entity["states"])->make($entity["override"]); |
||
| 67 | |||
| 68 | $data = $data->map(function ($item, $key) { |
||
| 69 | |||
| 70 | if (!$item["id"]) { |
||
| 71 | $item['id'] = $key + 1; |
||
| 72 | } |
||
| 73 | |||
| 74 | return $item; |
||
| 75 | }); |
||
| 76 | |||
| 77 | } else { |
||
| 78 | |||
| 79 | $folder = public_path(Mock::getPath() . '/' . $table); |
||
| 80 | |||
| 81 | if (is_file(Mock::getPath() . '/' . $table . '.json')) { |
||
| 82 | $data = json_decode(file_get_contents(Mock::getPath() . '/' . $table . '.json'), true); |
||
| 83 | return response()->json($data); |
||
| 84 | } |
||
| 85 | |||
| 86 | if (!is_dir($folder)) { |
||
| 87 | \File::makeDirectory($folder, 0775, true); |
||
| 88 | } |
||
| 89 | |||
| 90 | $files = \File::allFiles(public_path(Mock::getPath() . '/' . $table)); |
||
| 91 | |||
| 92 | $data = []; |
||
| 93 | |||
| 94 | foreach ($files as $file) { |
||
| 95 | $item = json_decode(file_get_contents($file->getRealPath()), true); |
||
| 96 | |||
| 97 | if (!isset($item["id"])) { |
||
| 98 | $item["id"] = str_replace(".json", "", $file->getBasename()); |
||
| 99 | } |
||
| 100 | |||
| 101 | $data[] = $item; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | if (config("mock.paginate")) { |
||
| 106 | // Get current page form url e.x. &page=1 |
||
| 107 | $currentPage = LengthAwarePaginator::resolveCurrentPage(); |
||
| 108 | |||
| 109 | // Create a new Laravel collection from the array data |
||
| 110 | $itemCollection = collect($data); |
||
| 111 | |||
| 112 | // Define how many items we want to be visible in each page |
||
| 113 | $perPage = config("mock.per_page"); |
||
| 114 | |||
| 115 | // Slice the collection to get the items to display in current page |
||
| 116 | $currentPageItems = $itemCollection->slice(($currentPage * $perPage) - $perPage, $perPage)->values()->all(); |
||
| 117 | |||
| 118 | // Create our paginator and pass it to the view |
||
| 119 | $paginatedItems= new LengthAwarePaginator($currentPageItems , count($itemCollection), $perPage); |
||
| 120 | |||
| 121 | // set url path for generted links |
||
| 122 | $paginatedItems->setPath($request->url()); |
||
| 123 | |||
| 124 | return $paginatedItems; |
||
| 125 | } |
||
| 126 | |||
| 127 | return $data; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param $table |
||
| 132 | * @param Request $request |
||
| 133 | * @return array |
||
| 134 | * @throws \Illuminate\Validation\ValidationException |
||
| 135 | */ |
||
| 136 | public function store($table, Request $request) |
||
| 137 | { |
||
| 138 | $this->_handleValidation($table, "store", $request); |
||
| 139 | |||
| 140 | $folder = public_path(Mock::getPath() . '/' . $table); |
||
| 141 | |||
| 142 | if (!is_dir($folder)) { |
||
| 143 | \File::makeDirectory($folder, 493, true); |
||
| 144 | } |
||
| 145 | |||
| 146 | $data = $request->all(); |
||
| 147 | $data['id'] = uniqid(); |
||
| 148 | |||
| 149 | \File::put($folder . '/' . $data['id'] . '.json', json_encode($data)); |
||
| 150 | |||
| 151 | return $data; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * @param $table |
||
| 156 | * @param $id |
||
| 157 | * @param Request $request |
||
| 158 | * @return array |
||
| 159 | * @throws \Illuminate\Validation\ValidationException |
||
| 160 | */ |
||
| 161 | public function update($table, $id, Request $request) |
||
| 162 | { |
||
| 163 | $this->_handleValidation($table, "update", $request); |
||
| 164 | |||
| 165 | $folder = public_path(Mock::getPath() ."/". $table); |
||
| 166 | |||
| 167 | if (!is_dir($folder)) { |
||
| 168 | \File::makeDirectory($folder, 493, true); |
||
| 169 | } |
||
| 170 | |||
| 171 | $data = $request->all(); |
||
| 172 | |||
| 173 | if (!isset($data["id"])) { |
||
| 174 | $data["id"] = $id; |
||
| 175 | } |
||
| 176 | |||
| 177 | \File::put($folder . '/' . $data['id'] . '.json', json_encode($data)); |
||
| 178 | |||
| 179 | return $data; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param $table |
||
| 184 | * @param $id |
||
| 185 | * @param Request $request |
||
| 186 | * @return Collection|mixed |
||
| 187 | * @throws \Illuminate\Validation\ValidationException |
||
| 188 | */ |
||
| 189 | public function view($table, $id, Request $request) |
||
| 190 | { |
||
| 191 | $this->_handleValidation($table, "view", $request); |
||
| 192 | |||
| 193 | $entity = config("mock.entrypoints.".$table); |
||
| 194 | |||
| 195 | if ($entity) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @var $data Collection |
||
| 199 | */ |
||
| 200 | $data = factory($entity["class"])->states($entity["states"])->make($entity["override"]); |
||
| 201 | |||
| 202 | if (!$data->id) { |
||
| 203 | $data->id = $id; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 204 | } |
||
| 205 | |||
| 206 | } else { |
||
| 207 | |||
| 208 | $folder = public_path(Mock::getPath() . '/' . $table); |
||
| 209 | |||
| 210 | $data = json_decode(file_get_contents($folder . '/' . $id . '.json'), true); |
||
| 211 | } |
||
| 212 | |||
| 213 | return $data; |
||
| 214 | } |
||
| 215 | } |
||
| 216 |