Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 27 |
DeployFilesToTracker | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 27 |
directoryToTrackers | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 6 |
|||
fileToTrackers | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 6 |
|||
putToTrackers | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 7 |
|||
createTrackerDisk | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 8 |
1 | <?php |
2 | namespace Qmp\Laravel\Tracker\Services\DeployFiles; |
3 | |
4 | use Qmp\Laravel\ToolsLaravel\FilesManager\FilesManager; |
5 | use Qmp\Laravel\Tracker\Models\Config; |
6 | use \Illuminate\Filesystem\FilesystemManager; |
7 | use \Illuminate\Contracts\Filesystem\Filesystem; |
8 | |
9 | /** |
10 | * @example (new DeployFilesToTracker()) |
11 | * ->from(Storage::disk('disk_from')) |
12 | * ->directoryToTrackers('from_path_folder', 'to_path_folder', true); |
13 | */ |
14 | class DeployFilesToTracker extends FilesManager |
15 | { |
16 | /** |
17 | * @param string $fromPath |
18 | * @param string $toPath |
19 | * @param bool $cleanToBefore = false |
20 | * @param bool $cleanFromAfter = false |
21 | * |
22 | * @return DeployFilesToTracker |
23 | */ |
24 | public function directoryToTrackers(string $fromPath, string $toPath, bool $cleanToBefore = false, bool $cleanFromAfter = false) : DeployFilesToTracker |
25 | { |
26 | $fsMg = new FilesystemManager(app()); |
27 | Config::where('active', 1)->get()->each(function($trackerConfig) use($fsMg, $fromPath, $toPath, $cleanToBefore, $cleanFromAfter) { |
28 | $tracker = $fsMg->createSftpDriver($trackerConfig->ssh); |
29 | $this->to($tracker)->directory($fromPath, $toPath, $cleanToBefore, $cleanFromAfter); |
30 | }); |
31 | |
32 | return $this; |
33 | } |
34 | |
35 | /** |
36 | * @param string $fromPath |
37 | * @param string $toPath |
38 | * @param bool $cleanToBefore = false |
39 | * @param bool $cleanFromAfter = false |
40 | * |
41 | * @return DeployFilesToTracker |
42 | */ |
43 | public function fileToTrackers(string $fromPath, string $toPath, bool $cleanToBefore = false, bool $cleanFromAfter = false) : DeployFilesToTracker |
44 | { |
45 | $fsMg = new FilesystemManager(app()); |
46 | Config::where('active', 1)->get()->each(function($trackerConfig) use($fsMg, $fromPath, $toPath, $cleanToBefore, $cleanFromAfter) { |
47 | $tracker = $fsMg->createSftpDriver($trackerConfig->ssh); |
48 | $this->to($tracker)->file($fromPath, $toPath, $cleanToBefore, $cleanFromAfter); |
49 | }); |
50 | |
51 | return $this; |
52 | } |
53 | |
54 | /** |
55 | * @param string $toPath |
56 | * @param \SplFileInfo $file |
57 | * @param string $filename = null |
58 | * @param bool $cleanToBefore = false |
59 | * |
60 | * @return DeployFilesToTracker |
61 | */ |
62 | public function putToTrackers(string $toPath, \SplFileInfo $file, string $filename = null, bool $cleanToBefore = false) : DeployFilesToTracker |
63 | { |
64 | $fsMg = new FilesystemManager(app()); |
65 | Config::where('active', 1)->get()->each(function($trackerConfig) use($fsMg, $toPath, $file, $filename, $cleanToBefore) { |
66 | if (is_array($trackerConfig->ssh) && $trackerConfig->ssh['active']) { |
67 | $tracker = $this->createTrackerDisk($fsMg, $trackerConfig); |
68 | $this->to($tracker)->put($toPath, $file, $filename, $cleanToBefore); |
69 | } |
70 | }); |
71 | |
72 | return $this; |
73 | } |
74 | |
75 | /** |
76 | * @param FilesystemManager $fsMg |
77 | * @param Config $trackerConfig |
78 | * |
79 | * @return Filesystem |
80 | */ |
81 | protected function createTrackerDisk(FilesystemManager $fsMg, Config $trackerConfig): Filesystem |
82 | { |
83 | $sshConfig = [ |
84 | 'driver' => 'sftp', |
85 | 'host' => $trackerConfig->ip, |
86 | 'port' => $trackerConfig->ssh['port'], |
87 | 'username' => $trackerConfig->ssh['user'], |
88 | 'password' => $trackerConfig->ssh['password'], |
89 | 'root' => $trackerConfig->ssh['root'], |
90 | 'timeout' => 30, |
91 | ]; |
92 | |
93 | return $fsMg->createSftpDriver($sshConfig); |
94 | } |
95 | } |