@@ -16,11 +16,13 @@ class CSVExport implements ExportDataServiceInterface
1616 private $ resultFormatedData ;
1717 private $ headers ;
1818
19- public function __construct (EntityManager $ em ) {
19+ public function __construct (EntityManager $ em )
20+ {
2021 $ this ->em = $ em ;
2122 }
2223
23- public function writeData (array $ dados ) {
24+ public function writeData (array $ dados )
25+ {
2426
2527 $ entity = null ;
2628 $ criteria = Json::decode ($ dados ['criteria ' ], Json::TYPE_ARRAY );
@@ -31,15 +33,15 @@ public function writeData(array $dados) {
3133 $ resultFormated = array ();
3234 $ entityData = array ();
3335
34- if (empty ($ criteria )) {
36+ if (empty ($ criteria )) {
3537 $ entityData = $ this ->em ->getRepository ($ entity )->findAll ();
36- }else {
38+ } else {
3739 $ entityData = $ this ->em ->getRepository ($ entity )->findBy ($ criteria );
3840 }
3941
40- foreach ($ entityData as $ data ) {
42+ foreach ($ entityData as $ data ) {
4143 $ arrayData = $ data ->toArray ();
42- foreach ($ this ->headers as $ head ) {
44+ foreach ($ this ->headers as $ head ) {
4345 $ resultFormated ['result ' ][$ head ] = $ arrayData [$ head ];
4446 }
4547
@@ -50,19 +52,23 @@ public function writeData(array $dados) {
5052
5153 }
5254
53- public function export () {
55+ public function export ()
56+ {
5457
55- $ path = dirname (dirname (dirname (dirname (dirname (dirname (__FILE__ )))))) . '/public/csv/ ' ;
56- $ fileName = str_replace ("/ " ,". " ,base64_encode (Rand::getBytes (6 ,true )).'_ ' .date ("Y-m-d-H:i:s " ).'.csv ' );
57- $ file = fopen ($ path .$ fileName ,"w " );
58+ $ path = __DIR__ . '/../../../../../../public/csv/ ' ;
59+ if (!is_dir ($ path )) {
60+ throw new \Exception ("Please make sure that 'public/csv' directory exists " );
61+ }
62+ $ fileName = str_replace ("/ " , ". " , base64_encode (Rand::getBytes (6 , true )) . '_ ' . date ("Y-m-d-H:i:s " ) . '.csv ' );
63+ $ file = fopen ($ path . $ fileName , "w " );
5864
59- fputcsv ($ file ,$ this ->headers );
60- foreach ($ this ->resultFormatedData as $ data ) {
61- fputcsv ($ file ,$ data );
65+ fputcsv ($ file , $ this ->headers );
66+ foreach ($ this ->resultFormatedData as $ data ) {
67+ fputcsv ($ file , $ data );
6268 }
6369
6470 fclose ($ file );
65- return '/csv/ ' . $ fileName ;
71+ return '/csv/ ' . $ fileName ;
6672
6773 }
6874
0 commit comments