· 7 years ago · Sep 04, 2018, 11:16 AM
1public function process($reportName)
2{
3
4//load our configration file for easy use.
5$config = config('cwreporter.name.' . $reportName . '');
6$filetype = config('cwreporter.filetype');
7
8
9 if (!in_array($reportName, array_keys(config('cwreporter.name')))) {
10 Log::error("report:process - The entered report ('.$reportName.') does not exist in config/cwreporter file! Please make sure it's filled out correctly or create the report.");
11
12 return;
13 }
14
15 //Check if the table exists in our database.
16 if (!Schema::hasTable($config['table'])) {
17 Log::error('report:process - The table ' . $config['table'] . ' does not exist in your database. Report: ' . $reportName . ' ');
18
19 return;
20 }
21
22 //Get the config filter for the spcific report (XML element => database element)
23 $mapping = $config['columns'];
24
25//Set the XML elements we need from our XML file.
26$xmlFilters = array_keys($config['columns']);
27
28//Find our file on the ftp server.
29$this->findFile();
30
31//If the file is found, get the info from the file.
32$file = $this->getFile($this->findFile());
33
34//Fetch the XML file from the FTP server.
35$xml = XmlParser::extract(Storage::disk('ftp')->get('' . $file['dirname'] . '/' . $file['basename'] . ''));
36
37//Parse the XML data from our XML file.
38$data = $xml->parse([
39 'report' => ['uses' => '' . $config['element'] . '[' . implode(',', $xmlFilters) . ']', 'default' => null]
40]);
41
42[...]
43
44//Find our file on the ftp server.
45$this->findFile();
46
47//If the file is found, get the info from the file.
48$file = $this->getFile($this->findFile());
49
50public function findFile()
51{
52 $filesInFolder = array_filter(Storage::disk('ftp')->files(config('cwreporter.folder')), function ($file) {
53
54 //This get filename from the FTP server (/reports folder), that includes the current date (YYYY-MM-DD) in the file name.
55 return preg_match('/' . date('Y', time()) . '-' . date('m', time()) . '-' . date('d', time()) . '(.*).(?i)' . $this->filetype . '/ms', $file);
56 });
57
58 if (empty($filesInFolder)) {
59 Log::error('report:process - No reports found on the FTP server by this filename.');
60 exit; //Stop the script
61 }
62
63 return $filesInFolder;
64}
65
66public function getFile($files)
67{
68 foreach ($files as $file) {
69 $fileInfo = pathinfo($file);
70 }
71 return $fileInfo;
72}