SynchronizerRepository::getLastBatchNumber()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace LaravelSynchronize\Console\Synchronizer;
4
5
use Illuminate\Support\Facades\DB;
6
use Illuminate\Database\Query\Builder;
7
use Illuminate\Database\ConnectionResolver as Resolver;
8
9
class SynchronizerRepository
10
{
11
12
    /**
13
     * The database connection resolver instance.
14
     *
15
     * @var \Illuminate\Database\ConnectionResolver
16
     */
17
    protected $resolver;
18
19
    /**
20
     * The name of the synchronizations table.
21
     *
22
     * @var string
23
     */
24
    protected $table;
25
26
    /**
27
     * The name of the database connection to use.
28
     *
29
     * @var string
30
     */
31
    protected $connection;
32
33
    /**
34
     * Create a new database synchronizations repository instance.
35
     *
36
     * @param  \Illuminate\Database\ConnectionResolver  $resolver
37
     * @param  string  $table
38
     *
39
     * @return void
40
     */
41
    public function __construct(Resolver $resolver)
42
    {
43
        $this->table = $this->getTable();
44
        $this->resolver = $resolver;
45
    }
46
47
    /**
48
     * Get the ran synchronizations.
49
     *
50
     * @return array
51
     */
52
    public function getRan(): array
53
    {
54
        return $this->table()
55
            ->orderBy('batch', 'asc')
56
            ->orderBy('synchronization', 'asc')
57
            ->pluck('synchronization')->all();
58
    }
59
60
    /**
61
     * Get the last synchronization batch.
62
     *
63
     * @return array
64
     */
65
    public function getLast(): array
66
    {
67
        $query = $this->table()->where('batch', $this->getLastBatchNumber());
68
69
        return $query->orderBy('synchronization', 'desc')->get()->all();
70
    }
71
72
    /**
73
     * Log that a synchronization was run.
74
     *
75
     * @param  string  $file
76
     * @param  int     $batch
77
     * @param  string  $operation
78
     *
79
     * @return void
80
     */
81
    public function log(string $file, int $batch, string $operation): void
82
    {
83
        $record = ['synchronization' => $file];
84
85
        DB::table($this->getTable())
86
            ->when($operation === 'up', function ($query) use ($record, $batch) {
87
                $query->updateOrInsert($record, [
88
                    'batch' => $batch,
89
                ]);
90
            }, function ($query) use ($file) {
91
                $query->where('synchronization', $file)->delete();
92
            });
93
    }
94
95
    /**
96
     * Get the next synchronization batch number.
97
     *
98
     * @return int
99
     */
100
    public function getNextBatchNumber(): int
101
    {
102
        return $this->getLastBatchNumber() + 1;
103
    }
104
105
    /**
106
     * Get the last synchronization batch number.
107
     *
108
     * @return int
109
     */
110
    public function getLastBatchNumber(): int
111
    {
112
        return DB::table($this->getTable())->max('batch') ?? 0;
113
    }
114
115
    /**
116
     * Get a query builder for the synchronization table.
117
     *
118
     * @return \Illuminate\Database\Query\Builder
119
     */
120
    protected function table(): Builder
121
    {
122
        return DB::table($this->getTable());
123
    }
124
125
    /**
126
     * Retrieve the table where synchronizations are being stored
127
     *
128
     * @return string
129
     */
130
    private function getTable(): string
131
    {
132
        return config('synchronizer.table') ?? 'synchronizations';
133
    }
134
}
135