Completed
Push — master ( cfe9e7...bf4c92 )
by Alex
03:16
created

ExchangeHistoryTableFeeder::getSymbolsFormDB()   C

Complexity

Conditions 7
Paths 26

Size

Total Lines 35
Code Lines 23

Duplication

Lines 25
Ratio 71.43 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 25
loc 35
rs 6.7272
cc 7
eloc 23
nc 26
nop 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\InteractionMethodsMOD\GetMethods;
6
use Carbon\Carbon;
7
use DB;
8
use GuzzleHttp\Client;
9
use Illuminate\Console\Command;
10
use Symfony\Component\Config\Definition\Exception\Exception;
11
12
/**
13
 * Class ExchangeHistoryTableFeeder
14
 * @package App\Console\Commands
15
 */
16
class ExchangeHistoryTableFeeder extends Command
17
{
18
    /**
19
     * The name and signature of the console command.
20
     *
21
     * @var string
22
     */
23
    protected $signature = 'history_table:feed';
24
25
    /**
26
     * The console command description.
27
     *
28
     * @var string
29
     */
30
    protected $description = 'Feed the table exchange_history from DB';
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     */
36
    public function __construct()
37
    {
38
        parent::__construct();
39
    }
40
41
    /**
42
     * Execute the console command.
43
     *
44
     * @return mixed
45
     */
46
    public function handle()
47
    {
48
        $this->getSymbolsFormDB();
49
    }
50
51
    /**
52
     *Elimina les dades antigues i n'insereix unes de noves
53
     */
54
    public function getSymbolsFormDB()
55
    {
56
        DB::table('exchange_history')->truncate();
57
58
        $symbols = DB::table('companies')->select('symbol')->get();
59
60
61 View Code Duplication
            for($i=0; $i<count($symbols); $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...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
            {
63
                try{
64
                    $symbol=$symbols[$i]->symbol;
65
                    $gm = new GetMethods();
66
                    $this->httpCall($gm,$symbol);
67
68
                    sleep(4);
69
                }catch(Exception $e){}
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
70
71
                switch($i){
72
                    case 0:
73
                        echo "0%\n";
74
                        break;
75
                    case 24:
76
                        echo "25%\n";
77
                        break;
78
                    case 49:
79
                        echo "50%\n";
80
                        break;
81
                    case 74:
82
                        echo "75%\n";
83
                }
84
85
            }
86
87
            echo("100% Table exchange_history fed\n");
88
    }
89
90
    /**
91
     * Aconegueix les dades de la api i fa la crida de la funció que les guardara a la BD
92
     *
93
     * @param GetMethods $interaction_methods
94
     * @param $symbol
95
     */
96
    public function httpCall(GetMethods $interaction_methods, $symbol)
97
    {
98
        $glz_cli=new Client();
99
100
        $data = $interaction_methods->interactiveChart($glz_cli,$symbol);
101
102
        $this->storeDataInDB($data);
103
    }
104
105
    /**
106
     *
107
     * Persisteix les dades al MYSQL
108
     *
109
     * @param $data
110
     */
111
    public function storeDataInDB($data)
112
    {
113
        DB::table('exchange_history')->insert([
114
            [
115
                'symbol' => $data->Elements[0]->Symbol,
116
                'positions' => json_encode($data->Positions),
117
                'dates' => json_encode($data->Dates),
118
                'values' => json_encode($data->Elements[0]->DataSeries->close->values),
119
                'max_date' =>  $data->Elements[0]->DataSeries->close->maxDate,
120
                'min_date' => $data->Elements[0]->DataSeries->close->minDate,
121
                'min_value' => $data->Elements[0]->DataSeries->close->min,
122
                'max_value' =>  $data->Elements[0]->DataSeries->close->max,
123
                'created_at' => Carbon::now()->toDateTimeString(),
124
                'updated_at' => Carbon::now()->toDateTimeString()
125
            ]
126
        ]);
127
    }
128
129
}
130