/** * @param DatabaseBackupFile $file * @return ResultObject[] */ public function process(DatabaseBackupFile $file) { $d = $file->getBackupDate(); $results = []; foreach ($this->uploadsCredentials as $credentials) { $result = new ResultObject(); // empty ResultObject means all is OK $backupPath = $credentials['path'] . '/' . $d->format('Y') . '/' . $d->format('F'); $entireFilePath = $backupPath . '/' . $file->getFileName(); try { $ftp = new \Ftp(); $ftp->connect($credentials['host']); $ftp->login($credentials['username'], $credentials['password']); if (!$ftp->fileExists($backupPath)) { $ftp->mkDirRecursive($backupPath); } $ftp->put($entireFilePath, $file->getFilePath(), FTP_BINARY); $ftp->close(); } catch (\FtpException $e) { $this->logger->addCritical(sprintf('Uploading backup file\'s failed. %s', $e)); $result->addError('Zálohu se nepodařilo nahrát na: ' . $credentials['host'], 'error'); } $results[] = $result; } return $results; }
/** * @param array $receivers * @param string $subject * @param string $messageText * @param string $attachedFile * @return ResultObject */ private function sendMail(array $receivers, $subject, $messageText, $attachedFile = null) { $result = new ResultObject(); $message = new Message(); $message->setFrom('Výčetkový systém <' . $this->systemEmail . '>')->setSubject($subject)->setBody($messageText); foreach ($receivers as $receiverEmail) { $message->addTo($receiverEmail); } if ($attachedFile !== null and file_exists($attachedFile)) { $message->addAttachment($attachedFile); } try { $this->mailer->send($message); } catch (SendException $s) { $this->logger->addError(sprintf('Backup file sending\'s failed. %s', $s)); $result->addError('Zálohu se nepodařilo odeslat', 'error'); } return $result; }