Skip to content

Commit

Permalink
Merge pull request #1 from thetribeio/clean-code
Browse files Browse the repository at this point in the history
Clean code
  • Loading branch information
qboisson authored Mar 23, 2018
2 parents 9176a4e + 0dfebb9 commit 0ea0c7c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"doctrine/doctrine-bundle": "^1.6",
"doctrine/orm": "^2.5",
"incenteev/composer-parameter-handler": "^2.0",
"phpoffice/phpspreadsheet": "1.0.0-beta",
"phpoffice/phpspreadsheet": "1.0.0",
"sensio/distribution-bundle": "^5.0.19",
"sensio/framework-extra-bundle": "^3.0.2",
"symfony/monolog-bundle": "^3.1.0",
Expand Down
22 changes: 15 additions & 7 deletions src/AppBundle/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class DefaultController extends Controller
/**
* @Route("/", name="homepage")
*/
public function indexAction(Request $request) {
public function indexAction(Request $request)
{
return $this->render('AppBundle::index.html.twig');
}

Expand All @@ -46,12 +47,15 @@ public function exportAction(Request $request)

switch ($format) {
case 'ods':
$contentType = 'application/vnd.oasis.opendocument.spreadsheet';
$writer = new Ods($spreadsheet);
break;
case 'xlsx':
$contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
$writer = new Xlsx($spreadsheet);
break;
case 'csv':
$contentType = 'text/csv';
$writer = new Csv($spreadsheet);
break;
default:
Expand All @@ -61,7 +65,7 @@ public function exportAction(Request $request)
}

$response = new StreamedResponse();
$response->headers->set('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->headers->set('Content-Type', $contentType);
$response->headers->set('Content-Disposition', 'attachment;filename="'.$filename.'"');
$response->setPrivate();
$response->headers->addCacheControlDirective('no-cache', true);
Expand All @@ -78,7 +82,8 @@ public function exportAction(Request $request)
]);
}

protected function createSpreadsheet() {
protected function createSpreadsheet()
{
$spreadsheet = new Spreadsheet();
// Get active sheet - also possible to retrieve a specific sheet
$sheet = $spreadsheet->getActiveSheet();
Expand Down Expand Up @@ -143,7 +148,7 @@ public function importAction(Request $request)
{
$filename = $this->get('kernel')->getRootDir().'/../export/'.self::FILENAME.'.xlsx';
if (!file_exists($filename)) {
exit('File does not exist.');
throw new \Exception('File does not exist');
}

$spreadsheet = $this->readFile($filename);
Expand All @@ -154,11 +159,13 @@ public function importAction(Request $request)
]);
}

protected function loadFile($filename) {
protected function loadFile($filename)
{
return IOFactory::load($filename);
}

protected function readFile($filename) {
protected function readFile($filename)
{
$extension = pathinfo($filename, PATHINFO_EXTENSION);
switch ($extension) {
case 'ods':
Expand All @@ -177,7 +184,8 @@ protected function readFile($filename) {
return $reader->load($filename);
}

protected function createDataFromSpreadsheet($spreadsheet) {
protected function createDataFromSpreadsheet($spreadsheet)
{
$data = [];
foreach ($spreadsheet->getWorksheetIterator() as $worksheet) {
$worksheetTitle = $worksheet->getTitle();
Expand Down
5 changes: 2 additions & 3 deletions src/AppBundle/Form/Type/ExcelFormatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,13 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('format', ChoiceType::class, [
'label' => false,
'choices' => [
'Select a format' => '',
'xlsx' => 'xlsx',
'ods' => 'ods',
'csv' => 'csv',
],
'required' => false,
'label' => false,
'placeholder' => 'Select a format',
]);
}
}

0 comments on commit 0ea0c7c

Please sign in to comment.