Completed
Push — master ( 942037...e7ceff )
by Alex
09:07
created

DBToFile   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 94
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 12 2
A transformData() 0 21 1
A createAndStore() 0 7 1
A getDataFromBD() 0 6 1
1
<?php
2
3
namespace App\Console\Commands;
4
5
use DB;
6
use Illuminate\Console\Command;
7
use SoapBox\Formatter\Formatter;
8
9
/**
10
 * Class DBToFile
11
 * @package App\Console\Commands
12
 */
13
class DBToFile extends Command
14
{
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'file_creator:create';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Create files from DB and transform him';
28
29
    /**
30
     * Create a new command instance.
31
     *
32
     */
33
    public function __construct()
34
    {
35
        parent::__construct();
36
    }
37
38
    /**
39
     * Execute the console command.
40
     *
41
     * @return mixed
42
     */
43
    public function handle()
44
    {
45
        $exchange_history = $this->getDataFromBD();
46
47
        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...
48
        {
49
            $company_history = $exchange_history[$i];
50
51
            $this->transformData($company_history);
52
        }
53
54
    }
55
56
    /**
57
     * @param $company_history
58
     */
59
    public function transformData($company_history)
60
    {
61
        $name_file = $company_history->symbol;
62
63
        $this->createAndStore($company_history, "json", $name_file);
64
65
        $company_history=json_encode($company_history);
66
67
        $formatter = Formatter::make($company_history, Formatter::JSON);
68
69
        $csv   = $formatter->toCsv();
70
        $xml   = $formatter->toXml();
71
        $yaml  = $formatter->toYaml();
72
73
        $this->createAndStore($csv, "csv", $name_file);
74
75
        $this->createAndStore($xml, "xml", $name_file);
76
77
        $this->createAndStore($yaml, "yaml", $name_file);
78
79
    }
80
81
    /**
82
     * @param $to_store
83
     * @param $extension_file
84
     * @param $name_file
85
     */
86
    public function createAndStore($to_store, $extension_file, $name_file)
87
    {
88
        $myfile = fopen("public/historic_info_files/".$extension_file."/".$name_file.".".$extension_file, "w");
89
        //$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...
90
        fwrite($myfile, json_encode($to_store));
91
        fclose($myfile);
92
    }
93
94
    /**
95
     * @return array|static[]
96
     */
97
    public function getDataFromBD()
98
    {
99
        $company_history = DB::table('exchange_history')->get();
100
101
        return $company_history;
102
    }
103
    
104
    
105
    
106
}
107