Skip to content
This repository has been archived by the owner on Aug 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from spatie/human-scrutinizer
Browse files Browse the repository at this point in the history
small refactor
  • Loading branch information
freekmurze committed Mar 18, 2016
2 parents 3cd2400 + a118b6a commit ef1fc0f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
27 changes: 11 additions & 16 deletions src/CleanUpModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,46 +49,41 @@ protected function getModelsThatShouldBeCleanedUp() : Collection
{
$directories = config('laravel-model-cleanup.directories');

$modelsFromDirectories = $this->getAllModelsOfEachDirectory($directories);
$modelsFromDirectories = $this->getAllModelsFromEachDirectory($directories);

$cleanableModels = $modelsFromDirectories
return $modelsFromDirectories
->merge(collect(config('laravel-model-cleanup.models')))
->flatten()
->filter(function ($modelClass) {

return in_array(GetsCleanedUp::class, class_implements($modelClass));
});

return $cleanableModels;
}

protected function cleanUp(Collection $cleanableModels)
{
$cleanableModels->each(function (string $class) {

$query = $class::cleanUp($class::query());

$numberOfDeletedRecords = $query->delete();
$numberOfDeletedRecords = $class::cleanUp($class::query())->delete();

$this->info("Deleted {$numberOfDeletedRecords} record(s) from {$class}.");

});
}

protected function getAllModelsOfEachDirectory(array $directories) : Collection
protected function getAllModelsFromEachDirectory(array $directories) : Collection
{
return collect($directories)->map(function ($directory) {

return $this->getClassNames($directory)->all();

});
return collect($directories)
->map(function ($directory) {
return $this->getClassNamesInDirectory($directory)->all();
})
->flatten();
}

protected function getClassNames(string $directory) : Collection
protected function getClassNamesInDirectory(string $directory) : Collection
{
return collect($this->filesystem->files($directory))->map(function ($path) {

return $this->getFullyQualifiedClassNameFromFile($path);

});
}

Expand Down
8 changes: 8 additions & 0 deletions src/GetsCleanedUp.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@

interface GetsCleanedUp
{
/**
* Returns a query that determines which models will get cleaned up. On
* cleanup, the `delete` method will be appended to the query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function cleanUp(Builder $query) : Builder;
}
6 changes: 0 additions & 6 deletions src/ModelCleanupServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,13 @@

class ModelCleanupServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*/
public function boot()
{
$this->publishes([
__DIR__.'/../config/laravel-model-cleanup.php' => config_path('laravel-model-cleanup.php'),
], 'config');
}

/**
* Register the application services.
*/
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/laravel-model-cleanup.php', 'laravel-model-cleanup');
Expand Down

0 comments on commit ef1fc0f

Please sign in to comment.