Laravel: Export customer record as CSV
up vote
-1
down vote
favorite
Using Laravel, I need to export some value from the DB in a CSV and then upload the CSV in a sftp or return it in a Response.
I'm trying to be SOLID but with this scenario, I'm not sure how to proceed. As I understand I should have one class that handle the CSV, one that handle the sFTP, one for the Response, and one to handle the logic of the model (the mapping in my case). But I don't understand how I can separate them.
Later one, I will have more Model to export.
<?php
namespace AppServices;
use AppLine;
use SymfonyComponentHttpFoundationStreamedResponse;
use Storage;
class LinesCsv
{
const DOCUMENT_TYPE = 20;
const DELIMITER = ';';
public function exportCSVFileToSftp($filename = 'export.csv')
{
$handle = fopen('php://temp', 'w');
$handle = $this->buildCsv($handle);
return Storage::disk('sftp')->put($filename, $handle);
}
public function exportCSVFileToResponse($filename = 'export.csv')
{
return new StreamedResponse(function () use ($filename) {
$handle = fopen('php://output', 'w');
$handle = $this->buildCsv($handle);
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
]);
}
public function buildCsv($handle, $header = false)
{
if ($header) {
fputcsv(
$handle,
array_keys($this->lineMapping(Line::first())),
self::DELIMITER
);
}
Line::with(['invoice', 'invoice.customer', 'item'])
->whereHas('invoice', function ($query) {
$query->where('is_exportable', 1);
})
->chunk(200, function ($lines) use ($handle) {
foreach ($lines as $line) {
fputcsv(
$handle,
$this->lineMapping($line),
self::DELIMITER
);
}
});
return $handle;
}
protected function lineMapping(Line $line)
{
return [
'Invoice number' => $line->invoice->id,
'Document type' => self::DOCUMENT_TYPE,
'Date' => $line->invoice->date,
];
}
}
php object-oriented csv laravel network-file-transfer
New contributor
This question has an open bounty worth +50
reputation from cbaconnier ending in 5 days.
This question has not received enough attention.
add a comment |
up vote
-1
down vote
favorite
Using Laravel, I need to export some value from the DB in a CSV and then upload the CSV in a sftp or return it in a Response.
I'm trying to be SOLID but with this scenario, I'm not sure how to proceed. As I understand I should have one class that handle the CSV, one that handle the sFTP, one for the Response, and one to handle the logic of the model (the mapping in my case). But I don't understand how I can separate them.
Later one, I will have more Model to export.
<?php
namespace AppServices;
use AppLine;
use SymfonyComponentHttpFoundationStreamedResponse;
use Storage;
class LinesCsv
{
const DOCUMENT_TYPE = 20;
const DELIMITER = ';';
public function exportCSVFileToSftp($filename = 'export.csv')
{
$handle = fopen('php://temp', 'w');
$handle = $this->buildCsv($handle);
return Storage::disk('sftp')->put($filename, $handle);
}
public function exportCSVFileToResponse($filename = 'export.csv')
{
return new StreamedResponse(function () use ($filename) {
$handle = fopen('php://output', 'w');
$handle = $this->buildCsv($handle);
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
]);
}
public function buildCsv($handle, $header = false)
{
if ($header) {
fputcsv(
$handle,
array_keys($this->lineMapping(Line::first())),
self::DELIMITER
);
}
Line::with(['invoice', 'invoice.customer', 'item'])
->whereHas('invoice', function ($query) {
$query->where('is_exportable', 1);
})
->chunk(200, function ($lines) use ($handle) {
foreach ($lines as $line) {
fputcsv(
$handle,
$this->lineMapping($line),
self::DELIMITER
);
}
});
return $handle;
}
protected function lineMapping(Line $line)
{
return [
'Invoice number' => $line->invoice->id,
'Document type' => self::DOCUMENT_TYPE,
'Date' => $line->invoice->date,
];
}
}
php object-oriented csv laravel network-file-transfer
New contributor
This question has an open bounty worth +50
reputation from cbaconnier ending in 5 days.
This question has not received enough attention.
The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
Nov 15 at 10:33
@TobySpeight I did the best I could. Perhaps, could you suggest me a better title?
– cbaconnier
Nov 15 at 10:40
It's hard to advise when the description is also quite vague, but something like "Export customer record as CSV" might work?
– Toby Speight
Nov 15 at 10:43
@TobySpeight Thanks
– cbaconnier
Nov 15 at 10:46
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
Using Laravel, I need to export some value from the DB in a CSV and then upload the CSV in a sftp or return it in a Response.
I'm trying to be SOLID but with this scenario, I'm not sure how to proceed. As I understand I should have one class that handle the CSV, one that handle the sFTP, one for the Response, and one to handle the logic of the model (the mapping in my case). But I don't understand how I can separate them.
Later one, I will have more Model to export.
<?php
namespace AppServices;
use AppLine;
use SymfonyComponentHttpFoundationStreamedResponse;
use Storage;
class LinesCsv
{
const DOCUMENT_TYPE = 20;
const DELIMITER = ';';
public function exportCSVFileToSftp($filename = 'export.csv')
{
$handle = fopen('php://temp', 'w');
$handle = $this->buildCsv($handle);
return Storage::disk('sftp')->put($filename, $handle);
}
public function exportCSVFileToResponse($filename = 'export.csv')
{
return new StreamedResponse(function () use ($filename) {
$handle = fopen('php://output', 'w');
$handle = $this->buildCsv($handle);
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
]);
}
public function buildCsv($handle, $header = false)
{
if ($header) {
fputcsv(
$handle,
array_keys($this->lineMapping(Line::first())),
self::DELIMITER
);
}
Line::with(['invoice', 'invoice.customer', 'item'])
->whereHas('invoice', function ($query) {
$query->where('is_exportable', 1);
})
->chunk(200, function ($lines) use ($handle) {
foreach ($lines as $line) {
fputcsv(
$handle,
$this->lineMapping($line),
self::DELIMITER
);
}
});
return $handle;
}
protected function lineMapping(Line $line)
{
return [
'Invoice number' => $line->invoice->id,
'Document type' => self::DOCUMENT_TYPE,
'Date' => $line->invoice->date,
];
}
}
php object-oriented csv laravel network-file-transfer
New contributor
Using Laravel, I need to export some value from the DB in a CSV and then upload the CSV in a sftp or return it in a Response.
I'm trying to be SOLID but with this scenario, I'm not sure how to proceed. As I understand I should have one class that handle the CSV, one that handle the sFTP, one for the Response, and one to handle the logic of the model (the mapping in my case). But I don't understand how I can separate them.
Later one, I will have more Model to export.
<?php
namespace AppServices;
use AppLine;
use SymfonyComponentHttpFoundationStreamedResponse;
use Storage;
class LinesCsv
{
const DOCUMENT_TYPE = 20;
const DELIMITER = ';';
public function exportCSVFileToSftp($filename = 'export.csv')
{
$handle = fopen('php://temp', 'w');
$handle = $this->buildCsv($handle);
return Storage::disk('sftp')->put($filename, $handle);
}
public function exportCSVFileToResponse($filename = 'export.csv')
{
return new StreamedResponse(function () use ($filename) {
$handle = fopen('php://output', 'w');
$handle = $this->buildCsv($handle);
fclose($handle);
}, 200, [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename="' . $filename . '"',
]);
}
public function buildCsv($handle, $header = false)
{
if ($header) {
fputcsv(
$handle,
array_keys($this->lineMapping(Line::first())),
self::DELIMITER
);
}
Line::with(['invoice', 'invoice.customer', 'item'])
->whereHas('invoice', function ($query) {
$query->where('is_exportable', 1);
})
->chunk(200, function ($lines) use ($handle) {
foreach ($lines as $line) {
fputcsv(
$handle,
$this->lineMapping($line),
self::DELIMITER
);
}
});
return $handle;
}
protected function lineMapping(Line $line)
{
return [
'Invoice number' => $line->invoice->id,
'Document type' => self::DOCUMENT_TYPE,
'Date' => $line->invoice->date,
];
}
}
php object-oriented csv laravel network-file-transfer
php object-oriented csv laravel network-file-transfer
New contributor
New contributor
edited Nov 15 at 10:46
New contributor
asked Nov 15 at 7:54
cbaconnier
494
494
New contributor
New contributor
This question has an open bounty worth +50
reputation from cbaconnier ending in 5 days.
This question has not received enough attention.
This question has an open bounty worth +50
reputation from cbaconnier ending in 5 days.
This question has not received enough attention.
The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
Nov 15 at 10:33
@TobySpeight I did the best I could. Perhaps, could you suggest me a better title?
– cbaconnier
Nov 15 at 10:40
It's hard to advise when the description is also quite vague, but something like "Export customer record as CSV" might work?
– Toby Speight
Nov 15 at 10:43
@TobySpeight Thanks
– cbaconnier
Nov 15 at 10:46
add a comment |
The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
Nov 15 at 10:33
@TobySpeight I did the best I could. Perhaps, could you suggest me a better title?
– cbaconnier
Nov 15 at 10:40
It's hard to advise when the description is also quite vague, but something like "Export customer record as CSV" might work?
– Toby Speight
Nov 15 at 10:43
@TobySpeight Thanks
– cbaconnier
Nov 15 at 10:46
The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
Nov 15 at 10:33
The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
Nov 15 at 10:33
@TobySpeight I did the best I could. Perhaps, could you suggest me a better title?
– cbaconnier
Nov 15 at 10:40
@TobySpeight I did the best I could. Perhaps, could you suggest me a better title?
– cbaconnier
Nov 15 at 10:40
It's hard to advise when the description is also quite vague, but something like "Export customer record as CSV" might work?
– Toby Speight
Nov 15 at 10:43
It's hard to advise when the description is also quite vague, but something like "Export customer record as CSV" might work?
– Toby Speight
Nov 15 at 10:43
@TobySpeight Thanks
– cbaconnier
Nov 15 at 10:46
@TobySpeight Thanks
– cbaconnier
Nov 15 at 10:46
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Try to think about this in a more generic way, you basically have:
- Select data from DB
- Put that data in a CSV (will you in the future want to use other formats like XLSX, etc? If so, we should abstract this step too, but I wont do this right now)
- Return that CSV file (doesn't matter to who you're returning)
So we can create a few classes from those steps
- Any class that can query the DB, your app probably has a bunch of them
- AbstractWriter - this class is an abstraction/interface to write anywhere, you'll need to implement it for specific formats, like Writer/Csv, Writer/Xlsx, Writer/JSON, etc.
- AbstractFileReturner - this class is an abstraction/interface to return it to anyone, you'll need to implement it for specific cases, AbstractFileReturner/like Sftp, AbstractFileReturner/HttpResponse, etc.
Any time you need more File formats or Returner, you just implement the AbstractWriter or FileReturner respectively one more time.
Doing all these, you'll be using Single Responsability Principle (each class do only one thing) and Open/Closed (the abstractions/interfaces are closed for modification, but open for extension).
I already understand this. But in practice, when I'm looking the code code and tri to dissassemble them, I Just don't understand how.
– cbaconnier
15 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Try to think about this in a more generic way, you basically have:
- Select data from DB
- Put that data in a CSV (will you in the future want to use other formats like XLSX, etc? If so, we should abstract this step too, but I wont do this right now)
- Return that CSV file (doesn't matter to who you're returning)
So we can create a few classes from those steps
- Any class that can query the DB, your app probably has a bunch of them
- AbstractWriter - this class is an abstraction/interface to write anywhere, you'll need to implement it for specific formats, like Writer/Csv, Writer/Xlsx, Writer/JSON, etc.
- AbstractFileReturner - this class is an abstraction/interface to return it to anyone, you'll need to implement it for specific cases, AbstractFileReturner/like Sftp, AbstractFileReturner/HttpResponse, etc.
Any time you need more File formats or Returner, you just implement the AbstractWriter or FileReturner respectively one more time.
Doing all these, you'll be using Single Responsability Principle (each class do only one thing) and Open/Closed (the abstractions/interfaces are closed for modification, but open for extension).
I already understand this. But in practice, when I'm looking the code code and tri to dissassemble them, I Just don't understand how.
– cbaconnier
15 hours ago
add a comment |
up vote
0
down vote
Try to think about this in a more generic way, you basically have:
- Select data from DB
- Put that data in a CSV (will you in the future want to use other formats like XLSX, etc? If so, we should abstract this step too, but I wont do this right now)
- Return that CSV file (doesn't matter to who you're returning)
So we can create a few classes from those steps
- Any class that can query the DB, your app probably has a bunch of them
- AbstractWriter - this class is an abstraction/interface to write anywhere, you'll need to implement it for specific formats, like Writer/Csv, Writer/Xlsx, Writer/JSON, etc.
- AbstractFileReturner - this class is an abstraction/interface to return it to anyone, you'll need to implement it for specific cases, AbstractFileReturner/like Sftp, AbstractFileReturner/HttpResponse, etc.
Any time you need more File formats or Returner, you just implement the AbstractWriter or FileReturner respectively one more time.
Doing all these, you'll be using Single Responsability Principle (each class do only one thing) and Open/Closed (the abstractions/interfaces are closed for modification, but open for extension).
I already understand this. But in practice, when I'm looking the code code and tri to dissassemble them, I Just don't understand how.
– cbaconnier
15 hours ago
add a comment |
up vote
0
down vote
up vote
0
down vote
Try to think about this in a more generic way, you basically have:
- Select data from DB
- Put that data in a CSV (will you in the future want to use other formats like XLSX, etc? If so, we should abstract this step too, but I wont do this right now)
- Return that CSV file (doesn't matter to who you're returning)
So we can create a few classes from those steps
- Any class that can query the DB, your app probably has a bunch of them
- AbstractWriter - this class is an abstraction/interface to write anywhere, you'll need to implement it for specific formats, like Writer/Csv, Writer/Xlsx, Writer/JSON, etc.
- AbstractFileReturner - this class is an abstraction/interface to return it to anyone, you'll need to implement it for specific cases, AbstractFileReturner/like Sftp, AbstractFileReturner/HttpResponse, etc.
Any time you need more File formats or Returner, you just implement the AbstractWriter or FileReturner respectively one more time.
Doing all these, you'll be using Single Responsability Principle (each class do only one thing) and Open/Closed (the abstractions/interfaces are closed for modification, but open for extension).
Try to think about this in a more generic way, you basically have:
- Select data from DB
- Put that data in a CSV (will you in the future want to use other formats like XLSX, etc? If so, we should abstract this step too, but I wont do this right now)
- Return that CSV file (doesn't matter to who you're returning)
So we can create a few classes from those steps
- Any class that can query the DB, your app probably has a bunch of them
- AbstractWriter - this class is an abstraction/interface to write anywhere, you'll need to implement it for specific formats, like Writer/Csv, Writer/Xlsx, Writer/JSON, etc.
- AbstractFileReturner - this class is an abstraction/interface to return it to anyone, you'll need to implement it for specific cases, AbstractFileReturner/like Sftp, AbstractFileReturner/HttpResponse, etc.
Any time you need more File formats or Returner, you just implement the AbstractWriter or FileReturner respectively one more time.
Doing all these, you'll be using Single Responsability Principle (each class do only one thing) and Open/Closed (the abstractions/interfaces are closed for modification, but open for extension).
answered yesterday
Edson Horacio Junior
1236
1236
I already understand this. But in practice, when I'm looking the code code and tri to dissassemble them, I Just don't understand how.
– cbaconnier
15 hours ago
add a comment |
I already understand this. But in practice, when I'm looking the code code and tri to dissassemble them, I Just don't understand how.
– cbaconnier
15 hours ago
I already understand this. But in practice, when I'm looking the code code and tri to dissassemble them, I Just don't understand how.
– cbaconnier
15 hours ago
I already understand this. But in practice, when I'm looking the code code and tri to dissassemble them, I Just don't understand how.
– cbaconnier
15 hours ago
add a comment |
cbaconnier is a new contributor. Be nice, and check out our Code of Conduct.
cbaconnier is a new contributor. Be nice, and check out our Code of Conduct.
cbaconnier is a new contributor. Be nice, and check out our Code of Conduct.
cbaconnier is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207704%2flaravel-export-customer-record-as-csv%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles.
– Toby Speight
Nov 15 at 10:33
@TobySpeight I did the best I could. Perhaps, could you suggest me a better title?
– cbaconnier
Nov 15 at 10:40
It's hard to advise when the description is also quite vague, but something like "Export customer record as CSV" might work?
– Toby Speight
Nov 15 at 10:43
@TobySpeight Thanks
– cbaconnier
Nov 15 at 10:46