Laravel: How to be SOLID when creating and exporting a CSV file











up vote
0
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,
];
}
}









share|improve this question









New contributor




cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    0
    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,
    ];
    }
    }









    share|improve this question









    New contributor




    cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote

      favorite









      up vote
      0
      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,
      ];
      }
      }









      share|improve this question









      New contributor




      cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago





















      New contributor




      cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 hour ago









      cbaconnier

      1012




      1012




      New contributor




      cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      cbaconnier is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



























          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          cbaconnier is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207704%2flaravel-how-to-be-solid-when-creating-and-exporting-a-csv-file%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          cbaconnier is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          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.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207704%2flaravel-how-to-be-solid-when-creating-and-exporting-a-csv-file%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Mont Emei

          Province de Neuquén

          Journaliste