Skip to content

Commit 72cf040

Browse files
author
Nicolas Boisvert
committed
Commented the code and remove useless directory
1 parent 1468046 commit 72cf040

File tree

3 files changed

+155
-41
lines changed

3 files changed

+155
-41
lines changed

src/Import.php

Lines changed: 86 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
namespace Nicklayb\LaravelDbImport;
44

5+
/**
6+
* Class for Import process
7+
*
8+
* @author Nicolas Boisvert ([email protected])
9+
*
10+
* Extends this class to match your needs. Don't forget to add it in your
11+
* dbimport.php config file so you will be able to call it
12+
*/
513
abstract class Import
614
{
715
/**
@@ -52,25 +60,37 @@ abstract class Import
5260
*/
5361
protected $refresh = false;
5462

63+
/**
64+
* Specify table by table the select statement of which column to load
65+
*
66+
* @var array
67+
*/
5568
protected $selects = [];
5669

70+
/**
71+
* Show table command, it may change depending on your database server
72+
*
73+
* @var string
74+
*/
5775
protected $showTablesCommand = 'SHOW TABLES';
5876

59-
public function getShowTablesCommand()
60-
{
61-
return $this->showTablesCommand;
62-
}
63-
64-
public function getSourceConnection()
65-
{
66-
return $this->sourceConnection;
67-
}
68-
77+
/**
78+
* Checks if provided table has specific selected columns
79+
*
80+
* @param string $table
81+
* @return bool
82+
*/
6983
public function hasSelects($table)
7084
{
7185
return isset($this->selects[$table]);
7286
}
7387

88+
/**
89+
* Gets specific selects for defined table
90+
*
91+
* @param string $table
92+
* @return array
93+
*/
7494
public function getSelects($table)
7595
{
7696
if ($this->hasSelects($table)) {
@@ -79,46 +99,62 @@ public function getSelects($table)
7999
return ['*'];
80100
}
81101

82-
public function getDestinationConnection()
83-
{
84-
return $this->destinationConnection;
85-
}
86-
102+
/**
103+
* Return the qualified column name for table select
104+
*
105+
* @return string
106+
*/
87107
public function getQualifiedTableColumnName()
88108
{
89109
return 'Tables_in_'.$this->sourceConnection;
90110
}
91111

112+
/**
113+
* Return the database name from the configuration for the source
114+
*
115+
* @return string
116+
*/
92117
public function getSourceDatabaseName()
93118
{
94119
return config('database.connections.'.$this->sourceConnection.'.database');
95120
}
96121

122+
/**
123+
* Load all tables from the source connection
124+
*
125+
* @return array
126+
*/
97127
public function loadSourceTables()
98128
{
99129
return DB::connection($this->sourceConnection)->select($this->showTablesCommand);
100130
}
101131

132+
/**
133+
* Get a collection of only the table names from the the source connection
134+
*
135+
* @return Collection
136+
*/
102137
public function getSourceTables()
103138
{
104-
return collect($this->getTableSelect())->pluck($this->getQualifiedTableColumnName())
105-
}
106-
107-
public function getIgnoreTables()
108-
{
109-
return $this->ignoreTables;
110-
}
111-
112-
public function getLastTables()
113-
{
114-
return $this->lastTables;
139+
return collect($this->loadSourceTables())->pluck($this->getQualifiedTableColumnName())
115140
}
116141

142+
/**
143+
* Return the count of the pre/post tasks of the import
144+
*
145+
* @return int
146+
*/
117147
public function countImportTasks()
118148
{
119149
return count($this->preImport()) + count($this->postImport());
120150
}
121151

152+
/**
153+
* Return all rows from specified table in the source connection with
154+
* the selected columns
155+
*
156+
* @return Collection
157+
*/
122158
public function getSourceRows($table)
123159
{
124160
return DB::connection($this->sourceConnection)
@@ -127,20 +163,35 @@ public function getSourceRows($table)
127163
->get();
128164
}
129165

166+
/**
167+
* Delete the content of the destination connection table
168+
*
169+
* @return int
170+
*/
130171
public function clearDestinationTable($table)
131172
{
132173
return DB::connection($this->destinationConnection)
133174
->table($table)
134175
->delete();
135176
}
136177

178+
/**
179+
* Insert specific data into the destination connection
180+
*
181+
* @return int
182+
*/
137183
public function insertInDestination($table, $row)
138184
{
139185
return DB::connection($this->destinationConnection)
140186
->table($table)
141187
->insert((array) $this->executeManipulation($table, $row));
142188
}
143189

190+
/**
191+
* Sort the sources tables by ordering last tables and removing the ingored
192+
*
193+
* @return Collection
194+
*/
144195
public function getSortedSourceTables()
145196
{
146197
$tables = $this->getSourceTables();
@@ -149,31 +200,31 @@ public function getSortedSourceTables()
149200
foreach ($tables as $table) {
150201
if ($this->hasLastTable($table)) {
151202
$hold->push($table);
152-
} else {
203+
} elseif (!$this->hasIgnoreTable($table)) {
153204
$filteredTables->push($table);
154205
}
155206
}
156207
return $filteredTables->merge($holds);
157208
}
158209

210+
/**
211+
* Check if a specified table should be ignored
212+
*
213+
* @return bool
214+
*/
159215
public function hasIgnoreTable($table)
160216
{
161217
return in_array($table, $this->ignoreTables);
162218
}
163219

164-
public function hasLastTable($table)
165-
{
166-
return in_array($table, $this->lastTables);
167-
}
168-
169220
/**
170-
* Check if any ignore table is registered in the property
221+
* Check if a specified table should be last
171222
*
172223
* @return bool
173224
*/
174-
public function hasIgnoreTables()
225+
public function hasLastTable($table)
175226
{
176-
return count($this->ignoreTables) > 0;
227+
return in_array($table, $this->lastTables);
177228
}
178229

179230
/**

src/ImportCommand.php

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,49 @@ class ImportCommand extends Command
2929
*/
3030
protected $import = null;
3131

32+
/**
33+
* Name of the import
34+
*
35+
* @var string
36+
*/
3237
protected $importName;
3338

39+
/**
40+
* All the source tables to import
41+
*
42+
* @var array
43+
*/
3444
protected $sourceTables = [];
3545

46+
/**
47+
* Count of all the tasks required to complete de command
48+
*
49+
* @var int
50+
*/
3651
protected $tasksCount = 0;
3752

53+
/**
54+
* List of errors that happens during import
55+
*
56+
* @var array
57+
*/
3858
protected $errors = [];
3959

40-
public function __construct()
41-
{
42-
parent::__construct();
43-
}
44-
60+
/**
61+
* Checks if the provided import is in the config file
62+
*
63+
* @return bool
64+
*/
4565
private function importIsRegistered()
4666
{
4767
return in_array($this->importName, config('dbimport.imports'));
4868
}
4969

70+
/**
71+
* Create the actual import instance
72+
*
73+
* @return bool
74+
*/
5075
private function createImport()
5176
{
5277
if ($this->importIsRegistered()) {
@@ -61,12 +86,22 @@ private function createImport()
6186
return false;
6287
}
6388

89+
/**
90+
* Setup the tasks count for the progress bar
91+
*
92+
* @return void
93+
*/
6494
public function setTasksCount()
6595
{
6696
$this->tasksCount = $this->sourceTables->count() + $this->import->countImportTasks();
6797
$this->bar = $this->output->createProgressBar($this->tasksCount);
6898
}
6999

100+
/**
101+
* Handle the command process. This is overriden from Laravel's command
102+
*
103+
* @return void
104+
*/
70105
public function handle()
71106
{
72107
if ($this->boot()) {
@@ -76,6 +111,11 @@ public function handle()
76111
}
77112
}
78113

114+
/**
115+
* Handle the tables imports process
116+
*
117+
* @return void
118+
*/
79119
private function handleImport()
80120
{
81121
foreach ($this->sourceTables as $table) {
@@ -92,6 +132,11 @@ private function handleImport()
92132
}
93133
}
94134

135+
/**
136+
* Handle specific table import
137+
*
138+
* @return void
139+
*/
95140
public function handleTableImport($table)
96141
{
97142
$count = 0;
@@ -108,6 +153,11 @@ public function handleTableImport($table)
108153
return $count;
109154
}
110155

156+
/**
157+
* Executes array of tasks
158+
*
159+
* @return void
160+
*/
111161
private function executeTasks($tasks)
112162
{
113163
foreach ($tasks as $key => $task) {
@@ -117,6 +167,11 @@ private function executeTasks($tasks)
117167
}
118168
}
119169

170+
/**
171+
* Bootup the command process and return if the command can proceed or not
172+
*
173+
* @return true
174+
*/
120175
private function boot()
121176
{
122177
$this->importName = $this->argument('database');

src/ServiceProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
class ServiceProvider extends BaseProvider
99
{
10+
/**
11+
* Config file name
12+
*
13+
* @var string
14+
*/
1015
const CONFIG_FILE = 'dbimport.php';
1116

1217
/**
@@ -42,6 +47,9 @@ public function register()
4247
*/
4348
public function provides()
4449
{
45-
return ['Nicklayb\LaravelDbImport\DbImport'];
50+
return [
51+
'Nicklayb\LaravelDbImport\ImportCommand',
52+
'Nicklayb\LaravelDbImport\Import'
53+
];
4654
}
4755
}

0 commit comments

Comments
 (0)