public function deleteOldBackups(BackupCollection $backups) { // Don't ever delete the newest backup. $this->newestBackup = $backups->shift(); $dateRanges = $this->calculateDateRanges(); $backupsPerPeriod = $dateRanges->map(function (Period $period) use($backups) { return $backups->filter(function (Backup $backup) use($period) { return $backup->date()->between($period->startDate(), $period->endDate()); }); }); $backupsPerPeriod['daily'] = $this->groupByDateFormat($backupsPerPeriod['daily'], 'Ymd'); $backupsPerPeriod['weekly'] = $this->groupByDateFormat($backupsPerPeriod['weekly'], 'YW'); $backupsPerPeriod['monthly'] = $this->groupByDateFormat($backupsPerPeriod['monthly'], 'Ym'); $backupsPerPeriod['yearly'] = $this->groupByDateFormat($backupsPerPeriod['yearly'], 'Y'); $this->removeBackupsForAllPeriodsExceptOne($backupsPerPeriod); $this->removeBackupsOlderThan($dateRanges['yearly']->endDate(), $backups); $this->removeOldBackupsUntilUsingLessThanMaximumStorage($backups); }