• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tito10047 / migration-backup / 22960007943

11 Mar 2026 03:19PM UTC coverage: 66.667%. First build
22960007943

push

github

web-flow
Merge pull request #1 from tito10047/v2

Create brand new version

108 of 165 new or added lines in 14 files covered. (65.45%)

120 of 180 relevant lines covered (66.67%)

28.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.58
/src/BackupManager.php
1
<?php
2

3
namespace Tito10047\MigrationBackup;
4

5
use Tito10047\MigrationBackup\Registry\BackupDriverRegistryInterface;
6
use Tito10047\MigrationBackup\Resolver\ConnectionResolverInterface;
7
use Tito10047\MigrationBackup\Storage\StorageProviderInterface;
8
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
9
use Tito10047\MigrationBackup\Event\BackupStartedEvent;
10
use Tito10047\MigrationBackup\Event\BackupFinishedEvent;
11
use Tito10047\MigrationBackup\Event\BackupFailedEvent;
12
use Symfony\Component\Filesystem\Filesystem;
13
use Throwable;
14

15
class BackupManager {
16
        public function __construct(
17
                private readonly ConnectionResolverInterface   $connectionResolver,
18
                private readonly BackupDriverRegistryInterface $driverRegistry,
19
                private readonly StorageProviderInterface      $storageProvider,
20
                private readonly EventDispatcherInterface      $eventDispatcher,
21
                private readonly Filesystem                    $fs,
22
                private readonly int                           $keepLastN = 0,
23
                private readonly bool                          $compress = false,
24
        ) {}
105✔
25

26
        public function backup(string $connectionName): string {
27
                $this->eventDispatcher->dispatch(new BackupStartedEvent($connectionName));
105✔
28

29
                $tempPath = null;
105✔
30
                try {
31
                        $params = $this->connectionResolver->resolve($connectionName);
105✔
32
                        $driver = $this->driverRegistry->getDriver($params->driver);
105✔
33

34
                        $extension = $this->compress ? '.sql.gz' : '.sql';
84✔
35
                        $filename  = $connectionName . '-' . date('Y-m-d-H-i-s') . $extension;
84✔
36
                        $tempPath  = tempnam(sys_get_temp_dir(), 'mb_');
84✔
37

38
                        $driver->dump($params, $tempPath);
84✔
39

40
                        if ($this->compress) {
63✔
NEW
41
                                $this->compressFile($tempPath);
×
42
                        }
43

44
                        $storedPath = $this->storageProvider->store($tempPath, $filename);
63✔
45

46
                        $this->storageProvider->cleanup($connectionName, $this->keepLastN);
63✔
47

48
                        $this->eventDispatcher->dispatch(new BackupFinishedEvent($connectionName, $storedPath));
63✔
49

50
                        return $storedPath;
63✔
51
                } catch (Throwable $e) {
42✔
52
                        $this->eventDispatcher->dispatch(new BackupFailedEvent($connectionName, $e));
42✔
53
                        throw $e;
42✔
54
                } finally {
55
                        if ($tempPath && $this->fs->exists($tempPath)) {
105✔
56
                                $this->fs->remove($tempPath);
42✔
57
                        }
58
                }
59
        }
60

61
        private function compressFile(string $path): void {
NEW
62
                $gzPath = $path . '.gz';
×
NEW
63
                $fp     = gzopen($gzPath, 'w9');
×
NEW
64
                if (!$fp) {
×
NEW
65
                        throw new \RuntimeException('Could not open file for compression: ' . $gzPath);
×
66
                }
67

NEW
68
                $handle = fopen($path, 'rb');
×
NEW
69
                if (!$handle) {
×
NEW
70
                        gzclose($fp);
×
NEW
71
                        throw new \RuntimeException('Could not open file for reading: ' . $path);
×
72
                }
73

NEW
74
                while (!feof($handle)) {
×
NEW
75
                        gzwrite($fp, fread($handle, 1024 * 512));
×
76
                }
77

NEW
78
                fclose($handle);
×
NEW
79
                gzclose($fp);
×
80

NEW
81
                $this->fs->rename($gzPath, $path, true);
×
82
        }
83
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc