Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/build-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
name: Deploy VitePress site to Pages

on:
# TODO: Change to tag?
push:
tags:
- "v*"
Expand Down
22 changes: 16 additions & 6 deletions .github/workflows/run-lint.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
name: Lint
on: push
jobs:
pint:
mago:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: "laravel-pint"
uses: aglipanci/[email protected]
- name: "checkout"
uses: "actions/checkout@v4"

- name: "installing PHP"
uses: "shivammathur/setup-php@v2"
with:
verboseMode: true
testMode: true
php-version: "8.3"

- name: "installing dependencies"
run: composer install --no-interaction --prefer-dist --optimize-autoloader

- name: "formatting"
run: php vendor/bin/mago fmt --dry-run

- name: "linting"
run: php vendor/bin/mago lint --reporting-format=github
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
13 changes: 9 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"pixelfear/composer-dist-plugin": "^0.1.6"
},
"require-dev": {
"laravel/pint": "^1.13",
"orchestra/testbench": "^9.0",
"pestphp/pest": "^3.0",
"pestphp/pest-plugin-laravel": "^3.0"
"pestphp/pest-plugin-laravel": "^3.0",
"carthage-software/mago": "^0.10.0"
},
"autoload": {
"psr-4": {
Expand Down Expand Up @@ -48,12 +48,16 @@
"@php vendor/bin/testbench serve"
],
"lint": [
"@php vendor/bin/pint"
"@php vendor/bin/mago lint"
],
"format": [
"@php vendor/bin/mago format"
],
"test": [
"XDEBUG_MODE=coverage vendor/bin/pest --coverage"
],
"qa": [
"@format",
"@lint",
"@test"
]
Expand All @@ -76,7 +80,8 @@
"config": {
"allow-plugins": {
"pixelfear/composer-dist-plugin": true,
"pestphp/pest-plugin": true
"pestphp/pest-plugin": true,
"carthage-software/mago": true
}
}
}
121 changes: 54 additions & 67 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions mago.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[source]
paths = ["src", "tests"]
includes = ["vendor"]

[format]
method_chain_break_threshold = 2
null_type_hint = "question"

[[linter.rules]]
name = "strictness/require-return-type"
ignore_arrow_function = true
3 changes: 0 additions & 3 deletions pint.json

This file was deleted.

10 changes: 7 additions & 3 deletions src/Abstracts/BackupPipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Closure;
use Itiden\Backup\Support\Zipper;

abstract class BackupPipe

Check notice on line 10 in src/Abstracts/BackupPipe.php

View workflow job for this annotation

GitHub Actions / mago

naming/class

Abstract class name `BackupPipe` should be prefixed with `Abstract`. The abstract class name `BackupPipe` does not follow PSR naming convention. Help: Consider renaming it to `AbstractBackupPipe` to adhere to the naming convention.
{
/**
* Get the key of the driver.
Expand All @@ -18,13 +18,17 @@
* Run the restore process.
*
* @param string $path The path to the root of the backup file.
* @param Closure(string $path):string $next The next pipe in the chain.
*/
abstract public function restore(string $path, Closure $next);
abstract public function restore(string $path, Closure $next): string;

/**
* Run the backup process.
*
* @param Zipper $zip The zipper instance.
* @param Closure(Zipper $zip):Zipper $next The next pipe in the chain.
*/
abstract public function backup(Zipper $zip, Closure $next);
abstract public function backup(Zipper $zip, Closure $next): Zipper;

/**
* Get the directory path for the current pipe.
Expand All @@ -37,7 +41,7 @@
/**
* Mark pipe as skipped.
*/
protected function skip(string $reason, Closure $next, Zipper $zip)
protected function skip(string $reason, Closure $next, Zipper $zip): Zipper
{
$zip->addMeta(static::class, ['skipped' => $reason]);
return $next($zip);
Expand Down
60 changes: 38 additions & 22 deletions src/Backuper.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Itiden\Backup;

use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Pipeline;
use Itiden\Backup\Contracts\Repositories\BackupRepository;
use Itiden\Backup\Support\Zipper;
Expand All @@ -15,7 +17,7 @@
final class Backuper
{
public function __construct(
protected BackupRepository $repository
protected BackupRepository $repository,
) {
}

Expand All @@ -36,7 +38,9 @@ public function backup(): BackupDto
->through(config('backup.pipeline'))
->thenReturn();

if ($password = config('backup.password')) {
$password = config('backup.password');

if ($password) {
$zipper->encrypt($password);
}

Expand All @@ -50,17 +54,23 @@ public function backup(): BackupDto

$metadata = $backup->getMetadata();

if ($user = auth()->user()) {
$user = auth()->user();

if ($user) {
$metadata->setCreatedBy($user);
}

$zipMeta->each(fn ($meta, $key) => match ($key) {
'skipped' => $meta->each(fn (string $reason, string $pipe) => $metadata->addSkippedPipe($pipe, $reason)),
});
$zipMeta->each(
static fn(Collection $meta, string $key): mixed => match ($key) {
'skipped' => $meta->each(function (string $reason, string $pipe) use ($metadata): void {
$metadata->addSkippedPipe(pipe: $pipe, reason: $reason);
}),
},
);

event(new BackupCreated($backup));

@unlink($temp_zip_path);
File::delete($temp_zip_path);

$this->enforceMaxBackups();

Expand All @@ -76,17 +86,22 @@ public function backup(): BackupDto
}
}

private function resolveMetaFromZip(Zipper $zip)
/**
* @return Collection<string, Collection<string|int, mixed>>
*/
private function resolveMetaFromZip(Zipper $zip): Collection
{
$metadata = collect([
'skipped' => collect(),
]);

$zip->getMeta()->each(function ($meta, $key) use ($metadata) {
if (isset($meta['skipped'])) {
$metadata->get('skipped')->put($key, $meta['skipped']);
}
});
$metadata = collect(['skipped' => collect()]);

$zip
->getMeta()
->each(static function (array|string $meta, string $key) use ($metadata): void {
if (is_array($meta) && isset($meta['skipped'])) {
$metadata
->get('skipped')
->put($key, $meta['skipped']);
}
});

return $metadata;
}
Expand All @@ -96,16 +111,17 @@ private function resolveMetaFromZip(Zipper $zip)
*/
private function enforceMaxBackups(): void
{
if (!$max_backups = config('backup.max_backups', false)) {
$maxBackups = config('backup.max_backups', false);
if (!$maxBackups) {
return;
}

$backups = $this->repository->all();

if ($backups->count() > $max_backups) {
$backups->slice($max_backups)->each(function ($backup) {
$this->repository->remove($backup->timestamp);
});
if ($backups->count() > $maxBackups) {
$backups
->slice($maxBackups)
->each(fn(BackupDto $backup): ?BackupDto => $this->repository->remove($backup->timestamp));
}
}
}
Loading
Loading