Completed
Push — master ( f5e63f...0e343e )
by Alex
09:04
created

ExchangeHistoryTableFeeder::getSymbolsFormDB()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 7

Duplication

Lines 7
Ratio 46.67 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 7
loc 15
rs 9.4285
cc 2
eloc 7
nc 2
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
11
class ExchangeHistoryTableFeeder extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'history_table:feed';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Command description';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $this->getSymbolsFormDB();
44
    }
45
46
    /**
47
     *Elimina les dades antigues i n'insereix unes de noves
48
     */
49
    public function getSymbolsFormDB()
50
    {
51
        DB::table('exchange_history')->truncate();
52
53
        $symbols = DB::table('companies')->select('symbol')->get();
54
55 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...
56
        {
57
            $symbol=$symbols[$i]->symbol;
58
            $gm = new GetMethods();
59
            $this->httpCall($gm,$symbol);
60
61
        }
62
63
    }
64
65
    /**
66
     * Aconegueix les dades de la api i fa la crida de la funció que les guardara a la BD
67
     *
68
     * @param GetMethods $interaction_methods
69
     * @param $symbol
70
     */
71
    public function httpCall(GetMethods $interaction_methods, $symbol)
72
    {
73
        $glz_cli=new Client();
74
75
        $data = $interaction_methods->interactiveChart($glz_cli,$symbol);
76
77
        $this->storeDataInDB($data);
78
    }
79
80
    /**
81
     *
82
     * Persisteix les dades al MYSQL
83
     *
84
     * @param $data
85
     */
86
    public function storeDataInDB($data)
87
    {
88
        DB::table('exchange_history')->insert([
89
            [
90
                'symbol' => $data->Elements[0]->Symbol,
91
                'positions' => json_encode($data->Positions),
92
                'dates' => json_encode($data->Dates),
93
                'values' => json_encode($data->Elements[0]->DataSeries->close->values),
94
                'max_date' =>  $data->Elements[0]->DataSeries->close->maxDate,
95
                'min_date' => $data->Elements[0]->DataSeries->close->minDate,
96
                'min_value' => $data->Elements[0]->DataSeries->close->min,
97
                'max_value' =>  $data->Elements[0]->DataSeries->close->max,
98
                'created_at' => Carbon::now()->toDateTimeString(),
99
                'updated_at' => Carbon::now()->toDateTimeString()
100
            ]
101
        ]);
102
    }
103
104
}
105