morgan_chester 4 years ago
parent
commit
f3022f4fab

+ 1 - 1
composer.json

@@ -10,7 +10,7 @@
   ],
   "homepage": "https://repo.underclub.ru/subbclub/autobackup/",
   "license": "MIT",
-  "version": "1.0.4.1",
+  "version": "1.0.4.2",
   "authors": [
     {
       "name": "Gregory Krassilnikov",

+ 2 - 2
packages.json

@@ -1,7 +1,7 @@
 {
   "packages": {
     "subbclub/autobackup": {
-      "1.0.4.1": {
+      "1.0.4.2": {
         "name": "subbclub/autobackup",
         "type": "library",
         "description": "Script to backup to the usual Yandex Disk",
@@ -12,7 +12,7 @@
         ],
         "homepage": "https://subbclub.ru",
         "license": "MIT",
-        "version": "1.0.4.1",
+        "version": "1.0.4.2",
         "authors": [
           {
             "name": "Gregory Krassilnikov",

+ 58 - 1
src/AutoBackup/BaseConnect.php

@@ -4,7 +4,64 @@
 namespace AutoBackup;
 
 
-class BaseConnect
+use AutoBackup\Exception\ProviderException;
+
+abstract class BaseConnect
 {
+    protected string $remoteDirectoryPath;
+    protected string $proxyServer;
+    protected int $proxyPort;
+    protected bool $consoleOutput;
+    protected string $fileWebPath;
+    protected array $logOutput;
+    protected bool $useProxy;
+    protected string $proxyUser;
+    protected string $proxyPass;
+    protected string $backupVersionDirName;
+
+    public function __construct(ProviderOptions $options)
+    {
+
+        $this->remoteDirectoryPath = $options->remoteDirectoryPath;
+        $this->fileWebPath = $options->fileWebPath;
+        $this->consoleOutput = $options->consoleOutput;
+
+        $this->useProxy = $options->useProxy;
+
+        if ($this->useProxy) {
+            $this->proxyServer = $options->proxyServer;
+            $this->proxyPort = $options->proxyPort;
+
+            if (!empty($options->proxyUser)) {
+                $this->proxyUser = $options->proxyUser;
+                $this->proxyPass = $options->proxyPass;
+            }
+        }
+
+        $this->backupVersionDirName = isset($options->backupVersionDirName) ? $options->backupVersionDirName : date('Y-m-d');
+    }
+
+    protected function stdOutput($stage, $message)
+    {
+        switch ($this->consoleOutput) {
+            case true:
+                echo date("d.m.Y H:i:s") . " " . $stage . ": " . $message . "\n";
+                break;
+            case false:
+                $this->logOutput[] = [
+                    "date" => date("c"),
+                    "stage" => $stage,
+                    "message" => $message
+                ];
+
+                break;
+        }
+    }
+
+    /**
+     * @param array $files
+     * @throws ProviderException
+     */
+    abstract function proceedBackup(array $files);
 
 }

+ 42 - 0
src/AutoBackup/ProviderOptions.php

@@ -10,6 +10,48 @@ abstract class ProviderOptions
     private array $options;
 
     /**
+     * @var string
+     */
+    public string $remoteDirectoryPath;
+    /**
+     * @var string
+     */
+    public string $fileWebPath;
+    /**
+     * @var bool
+     */
+    public bool $consoleOutput = false;
+    /**
+     * @var string
+     */
+    public string $proxyServer;
+
+    /**
+     * @var int
+     */
+    public int $proxyPort;
+
+    /**
+     * @var bool
+     */
+    public bool $useProxy = false;
+
+    /**
+     * @var string
+     */
+    public string $proxyUser;
+
+    /**
+     * @var string
+     */
+    public string $proxyPass;
+
+    /**
+     * @var string
+     */
+    public string $backupVersionDirName;
+
+    /**
      * @param string $name
      * @param mixed $value
      */

+ 0 - 41
src/AutoBackup/ProviderOptions/Yandex.php

@@ -12,46 +12,5 @@ class Yandex extends ProviderOptions
      * @var string
      */
     public string $token;
-    /**
-     * @var string
-     */
-    public string $remoteDirectoryPath;
-    /**
-     * @var string
-     */
-    public string $fileWebPath;
-    /**
-     * @var bool
-     */
-    public bool $consoleOutput = false;
-    /**
-     * @var string
-     */
-    public string $proxyServer;
-
-    /**
-     * @var int
-     */
-    public int $proxyPort;
-
-    /**
-     * @var bool
-     */
-    public bool $useProxy = false;
-
-    /**
-     * @var string
-     */
-    public string $proxyUser;
-
-    /**
-     * @var string
-     */
-    public string $proxyPass;
-
-    /**
-     * @var string
-     */
-    public string $backupVersionDirName;
 
 }

+ 8 - 50
src/AutoBackup/Providers/Yandex.php

@@ -13,55 +13,13 @@ use Httpful\Request;
 class Yandex extends BaseConnect
 {
     private string $token;
-    private string $remoteDirectoryPath;
-    private string $proxyServer;
-    private int $proxyPort;
-    private bool $consoleOutput;
-    private string $fileWebPath;
-    private array $logOutput;
-    private bool $useProxy;
-    private string $proxyUser;
-    private string $proxyPass;
-    private string $backupVersionDirName;
 
     function __construct(ProviderOptions\Yandex $options)
     {
         $this->token = $options->token;
-        $this->remoteDirectoryPath = $options->remoteDirectoryPath;
-        $this->fileWebPath = $options->fileWebPath;
-        $this->consoleOutput = $options->consoleOutput;
-
-        $this->useProxy = $options->useProxy;
-
-        if ($this->useProxy) {
-            $this->proxyServer = $options->proxyServer;
-            $this->proxyPort = $options->proxyPort;
-
-            if (!empty($options->proxyUser)) {
-                $this->proxyUser = $options->proxyUser;
-                $this->proxyPass = $options->proxyPass;
-            }
-        }
-
-        $this->backupVersionDirName = isset($options->backupVersionDirName) ? $options->backupVersionDirName : date('Y-m-d');
+        parent::__construct($options);
     }
 
-    private function stdOutput($stage, $message)
-    {
-        switch ($this->consoleOutput) {
-            case true:
-                echo date("d.m.Y H:i:s") . " " . $stage . ": " . $message . "\n";
-                break;
-            case false:
-                $this->logOutput[] = [
-                    "date" => date("c"),
-                    "stage" => $stage,
-                    "message" => $message
-                ];
-
-                break;
-        }
-    }
 
     /**
      * @param Request $request
@@ -84,16 +42,16 @@ class Yandex extends BaseConnect
 
 
     /**
-     * @param array $resultFiles
+     * @param array $files
      * @throws ProviderException
      */
-    public function proceedBackup(array $resultFiles)
+    public function proceedBackup(array $files)
     {
         $baseRemoteDirectoryPath = $this->remoteDirectoryPath;
         $fileWebPath = rtrim($this->fileWebPath, '/');
         $backupVersionDirName = $this->backupVersionDirName;
 
-        if (!empty($resultFiles)) {
+        if (!empty($files)) {
 
             $dirName = $baseRemoteDirectoryPath . $backupVersionDirName;
 
@@ -110,7 +68,7 @@ class Yandex extends BaseConnect
                 throw new ProviderException("Unable to proceed createRemoteDir request");
             }
 
-            foreach ($resultFiles as &$resultFile) {
+            foreach ($files as &$resultFile) {
                 $resultFileNameExploded = explode("/", $resultFile);
                 $resultFileName = end($resultFileNameExploded);
 
@@ -135,7 +93,7 @@ class Yandex extends BaseConnect
                     }
 
                 } catch (ConnectionErrorException $e) {
-                    $resultFiles[] = $resultFile;
+                    $files[] = $resultFile;
                     $this->stdOutput("file upload", "file $resultFile will be proceeded again later");
                     $this->stdOutput("file upload", $e->getMessage());
                 }
@@ -166,7 +124,7 @@ class Yandex extends BaseConnect
                                         }
                                         if (in_array($status, ["failed"])) {
                                             $this->stdOutput("file upload", $responseStatus->code);
-                                            $resultFiles[] = $resultFile;
+                                            $files[] = $resultFile;
                                             $this->stdOutput("file upload", "file $resultFile will be proceeded again later");
 
                                             break;
@@ -183,7 +141,7 @@ class Yandex extends BaseConnect
                         }
                     }
                 } catch (Exception $e) {
-                    $resultFiles[] = $resultFile;
+                    $files[] = $resultFile;
                     $this->stdOutput("file upload", "file $resultFile will be proceeded again later");
                     $this->stdOutput("file upload", $e->getMessage());
                 }