Skip to content

Commit ea1f2f0

Browse files
committed
Add importer page
1 parent 5027d76 commit ea1f2f0

File tree

3 files changed

+116
-33
lines changed

3 files changed

+116
-33
lines changed

docs/importers.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Importers
2+
3+
Importers define import specific settings.
4+
They define which Eloquent model to use for example.
5+
6+
## Methods
7+
8+
The following methods are required by an importer.
9+
Custom importers should implement `Luna\Importer\Contracts\Importer`.
10+
11+
```php
12+
/**
13+
* Parse a csv line to an importable array for the model
14+
*
15+
* @param array $data
16+
* @return array
17+
*/
18+
public function parseLine(array $data): array;
19+
```
20+
21+
```php
22+
/**
23+
* Validate the raw line data
24+
*
25+
* @param array $data
26+
* @return bool
27+
*/
28+
public function validateLine(array $data): bool;
29+
```
30+
31+
```php
32+
/**
33+
* Get the model class path for the import
34+
*
35+
* @return string
36+
*/
37+
public function getModel(): string;
38+
```
39+
40+
```php
41+
/**
42+
* Get the file to import
43+
*
44+
* @return string
45+
*/
46+
public function getFilePath(): string;
47+
```
48+
49+
```php
50+
/**
51+
* Get the unique column key
52+
*
53+
* This should match a unique column in the table as
54+
* this is used to determine if a row needs to be updated
55+
* or inserted
56+
*
57+
* @return string
58+
*/
59+
public function getUniqueKey(): string;
60+
```
61+
62+
```php
63+
/**
64+
* Determine if the source file should be removed
65+
*
66+
* @return bool
67+
*/
68+
public function shouldCleanup(): bool;
69+
```
70+
71+
## Example
72+
73+
```php
74+
namespace Tests;
75+
76+
use App\Product;
77+
use Luna\Importer\Contracts\Importer;
78+
use Luna\Importer\Importers\BaseImporter;
79+
80+
class ProductImporter extends BaseImporter implements Importer
81+
{
82+
public function parseLine(array $data): array
83+
{
84+
return [
85+
'sku' => $data[0],
86+
'name' => $data[2]
87+
];
88+
}
89+
90+
public function getFilePath(): string
91+
{
92+
return storage_path('import/products.csv');
93+
}
94+
95+
public function getModel(): string
96+
{
97+
return Product::class;
98+
}
99+
100+
public function getUniqueKey(): string
101+
{
102+
return 'sku';
103+
}
104+
105+
public function validateLine(array $data): bool
106+
{
107+
return count($data) === 3;
108+
}
109+
110+
public function shouldCleanup(): bool
111+
{
112+
return true;
113+
}
114+
}
115+
```

tests/Contracts/ImporterTest.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,4 @@ public function it_should_have_unique_key()
8282
$this->importer->getUniqueKey()
8383
);
8484
}
85-
86-
/**
87-
* @test
88-
* @expectedException \Exception
89-
* @expectedExceptionMessage Failed
90-
*/
91-
public function it_should_do_something_on_failure()
92-
{
93-
$this->importer->importFailed([
94-
'exception' => new \Exception("Failed")
95-
]);
96-
}
97-
98-
/**
99-
* @test
100-
* @expectedException \Exception
101-
* @expectedExceptionMessage Success
102-
*/
103-
public function it_should_do_something_on_success()
104-
{
105-
$this->importer->importSuccess();
106-
}
10785
}

tests/TestImporter.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Luna\Importer\Importers\BaseImporter;
77

88
/**
9-
* Test runner
9+
* Test importer
1010
*
1111
* @package Luna\Importer
1212
* @subpackage Tests
@@ -42,16 +42,6 @@ public function getUniqueKey(): string
4242
return 'test';
4343
}
4444

45-
public function importSuccess()
46-
{
47-
throw new \Exception("Success");
48-
}
49-
50-
public function importFailed(array $data)
51-
{
52-
throw $data['exception'];
53-
}
54-
5545
public function validateLine(array $data): bool
5646
{
5747
return count($data) === 3;

0 commit comments

Comments
 (0)