public function apiObjectQuery($table, $request) { $params = config('harvester-transaction.valid_params'); // check that all params are valid. foreach ($request->all() as $key => $value) { if (!in_array($key, $params)) { return ['error' => $key . ' is not a valid request parameter.']; } } $take = $request->has('take') ? $request->input('take') : config('harvester-transaction.defaults.take'); $action = $request->has('action') ? $request->input('action') : null; $actions = config('harvester-transaction.valid_actions'); if (!in_array($action, $actions)) { return ['error' => 'action=' . $action . ' is not a valid request parameter.']; } if ($action != 'deleted') { $query = Object::select(); if (in_array($action, ['updated', 'created', 'modified'])) { $query->whereHas('transactions', function ($q) use($table, $request, $action) { $hours = $request->has('since') ? $request->input('since') : config('harvester-transaction.defaults.since'); $since = Carbon::now()->subhours($hours); $q->where('created_at', '>=', $since); // ?action=modified will find both created and update objects if ($action == 'modified') { $q->whereIn('action', ['created', 'updated']); } // ?action=[created,updated] if ($action != 'modified') { $q->where('action', '=', $action); } $q->where('table', '=', $table); })->with(['transactions' => function ($q) use($table) { $q->where('table', '=', $table); }]); } $query->with(['actors', 'assets', 'assets.type', 'assets.source', 'terms', 'terms.type', 'texts', 'texts.type', 'locations', 'locations.type', 'dates', 'dates.type']); } if ($action == 'deleted') { $query = Transaction::where('table', '=', $table)->where('action', '=', $action)->distinct(); } $request = $query->paginate($take); return $request; }
/** * Execute the console command. * * @return mixed */ public function handle() { if ($this->option('modified')) { $action = 'modified'; } if ($this->option('deleted')) { $action = 'deleted'; } // make a request to the api for created and updated objects $response = $this->client->request('GET', config('app.url') . 'api/transactions/objects?action=' . $action); // get the response from the api $api = json_decode($response->getBody()); // get number of pages from api response $last_page = $api->meta->last_page; // set defaults $page = 1; $ids = []; // make requests to Drupal for paginated results while ($page <= $last_page) { $res = $this->client->request('GET', config('harvester-transaction.export_url'), ['query' => ['token' => $this->token, 'action' => $action, 'take' => $this->take, 'page' => $page]]); // if response status is 200 if ($res->getStatusCode() == 200) { // build array of ids to delete from transactions table $res_ids = json_decode($res->getBody()); $ids = array_merge($ids, (array) $res_ids); $page++; } } // empty the transaction table based on returned ids from Drupal foreach ($ids as $id) { if ($this->option('modified')) { $transactions = ['updated', 'created']; } if ($this->option('deleted')) { $transactions = ['deleted']; } Transaction::where('table_id', '=', $id)->where('table', '=', 'objects')->whereIn('action', $transactions)->delete(); } }