Passed
Push — master ( dce6b5...90a3ce )
by Roy
04:47
created

SynchronizerRepository::createRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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