DBToFile::createAndStore()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Console\AuxiliaryClasses\DataBaseFunctions;
6
use DB;
7
use Illuminate\Console\Command;
8
use SoapBox\Formatter\Formatter;
9
10
/**
11
 * Class DBToFile
12
 * @package App\Console\Commands
13
 */
14
class DBToFile extends Command
15
{
16
    /**
17
     * @var DataBaseFunctions
18
     */
19
    protected $db_functions;
20
    /**
21
     * The name and signature of the console command.
22
     *
23
     * @var string
24
     */
25
    protected $signature = 'file_creator:create';
26
27
    /**
28
     * The console command description.
29
     *
30
     * @var string
31
     */
32
    protected $description = 'Create files from DB and transform him';
33
34
    /**
35
     * Create a new command instance.
36
     * @param DataBaseFunctions $db_functions
37
     */
38
    public function __construct(DataBaseFunctions $db_functions)
39
    {
40
        parent::__construct();
41
42
        $this->db_functions=$db_functions;
43
44
    }
45
46
    /**
47
     * Execute the console command.
48
     *
49
     * @return mixed
50
     */
51
    public function handle()
52
    {
53
        $exchange_history = $this->db_functions->getAllDataFromExchangeHistory();
54
55
        for($i=0; $i<count($exchange_history); $i++)
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
56
        {
57
            $company_history = $exchange_history[$i];
58
59
            $this->transformData($company_history);
60
        }
61
62
        echo ("The files have been created successfully\n");
63
64
    }
65
66
    /**
67
     * @param $company_history
68
     */
69
    public function transformData($company_history)
70
    {
71
        $name_file = $company_history->symbol;
72
73
        $this->createAndStore($company_history, "json", $name_file);
74
75
        $company_history=json_encode($company_history);
76
77
        $formatter = Formatter::make($company_history, Formatter::JSON);
78
79
        $csv   = $formatter->toCsv();
80
        $xml   = $formatter->toXml();
81
        $yaml  = $formatter->toYaml();
82
83
        $this->createAndStore($csv, "csv", $name_file);
84
85
        $this->createAndStore($xml, "xml", $name_file);
86
87
        $this->createAndStore($yaml, "yaml", $name_file);
88
89
    }
90
91
    /**
92
     * @param $to_store
93
     * @param $extension_file
94
     * @param $name_file
95
     */
96
    public function createAndStore($to_store, $extension_file, $name_file)
97
    {
98
        $myfile = fopen("public/historic_info_files/".$extension_file."/".$name_file.".".$extension_file, "w");
99
        //$myfile = fopen("public/historic_info_files/testfile.txt", "w");
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
        fwrite($myfile, json_encode($to_store));
101
        fclose($myfile);
102
    }
103
    
104
}
105