Passed
Push — master ( a8d5c8...21cbf3 )
by Roy
15:41 queued 05:41
created

MigrationEndedEventListener::getClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace LaravelSynchronize\Listeners;
4
5
use SplFileInfo;
6
use Illuminate\Database\Events\MigrationEnded;
7
use LaravelSynchronize\Console\Synchronizer\Synchronizer;
8
use LaravelSynchronize\Console\Synchronizer\SynchronizerRepository;
9
10
class MigrationEndedEventListener
11
{
12
    /**
13
     * The Synchronizer instance.
14
     *
15
     * @var \LaravelSynchronize\Console\Synchronizer\Synchronizer
16
     */
17
    protected $synchronizer;
18
19
    /**
20
     * @var \LaravelSynchronize\Console\Synchronizer\SynchronizerRepository $synchronizerRepository
21
     */
22
    protected $synchronizerRepository;
23
24
    public function __construct(Synchronizer $synchronizer, SynchronizerRepository $synchronizerRepository)
25
    {
26
        $this->synchronizer = $synchronizer;
27
        $this->synchronizerRepository = $synchronizerRepository;
28
    }
29
30
    /**
31
     * Handle the event.
32
     *
33
     * @todo use MigrationsStarted event to collect synchronizations
34
     *
35
     * @param MigrationEnded $migrationEnded
36
     *
37
     * @return void
38
     *
39
     * @author Ramon Bakker <[email protected]>
40
     * @version 1.0.0
41
     */
42
    public function handle(MigrationEnded $migrationEnded)
43
    {
44
        // Synchronizations should execute after going down
45
        if ($migrationEnded->method !== 'down') {
46
            return;
47
        }
48
49
        $class = get_class($migrationEnded->migration) . 'Synchronization';
50
        $files = $this->synchronizer->getSynchronizations();
51
52
        $handledFiles = collect($this->synchronizerRepository->getLast())
53
            ->pluck('synchronization');
54
55
        $filesToHandle = $files->filter(function ($file) use ($handledFiles, $class) {
56
            return $handledFiles->contains($file->getFileName()) && (!$class || ($class === $this->getClassName($file)));
57
        });
58
59
        if ($filesToHandle->isEmpty()) {
60
            echo "No synchronization found for {$class}\n";
61
62
            return;
63
        }
64
65
        $filesToHandle->each(function ($file) {
66
            echo 'Rolling back synchronization: ' . $file->getFileName() . "\n";
67
68
            $this->synchronizer->run($file, 'down');
69
70
            echo 'Rolled back synchronization: ' . $file->getFileName() . "\n";
71
        });
72
    }
73
74
    /**
75
     * Get class name for file
76
     *
77
     * @param SplFileInfo $file
78
     *
79
     * @return string
80
     */
81
    private function getClassName(SplFileInfo $file): string
82
    {
83
        return $this->synchronizer->getClassName(
84
            $this->synchronizer->getSynchronizationName($file->getFilename())
85
        );
86
    }
87
}
88