onConnection('rabbitmq'); $this->customer = $customer; } /** * Execute the job. * * @return void */ public function handle() { $hashchode = $this->customer['hashcode']; $email = $this->customer['email']; try { DB::transaction(function () use ($hashchode, $email) { // For baskets and old walls. $contact = Contact::query()->where('contact_ref', $hashchode)->first(); if ($contact) { $contact->deleteOrFail(); $this->writeCsvLog('Contact deleted'); } // For walls. $customer = Customers::query()->where('hashcode', $hashchode)->first(); if ($customer) { $customer->deleteOrFail(); $this->writeCsvLog('Customer deleted'); } }); } catch (\Exception $e) { Log::info( sprintf( "DeleteCrmCustomerJob failed - Code : %s - %s", $e->getCode(), $e->getMessage(), ) ); \report($e); } //end try } /** * Write logs in /logs/crm.csv. * * @param string $message * * @return void */ private function writeCsvLog(string $message) { $csvPath = \storage_path('/logs/crm.csv'); if (!\file_exists($csvPath)) { $logFile = fopen($csvPath, 'w'); \fputcsv($logFile, ['date', 'hashcode', 'email', 'action'], ';'); \fclose($logFile); } $logFile = fopen($csvPath, 'a'); \fputcsv( $logFile, [ \now()->toAtomString(), $this->customer['hashcode'], $this->customer['email'], $message ], ';' ); \fclose($logFile); } }