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

CompaniesTableFeeder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 125
Duplicated Lines 17.6 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 13
c 3
b 0
f 0
lcom 1
cbo 4
dl 22
loc 125
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A handle() 0 4 1
B getSymbolsFormArray() 22 35 6
A httpCall() 0 9 1
A discardRepeated() 0 14 3
A storeDataInDB() 0 12 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
/**
12
 * Class CompaniesTableFeeder
13
 * @package App\Console\Commands
14
 */
15
class CompaniesTableFeeder extends Command
16
{
17
    /**
18
     * The name and signature of the console command.
19
     *
20
     * @var string
21
     */
22
    protected $signature = 'companies_table:feed';
23
24
    /**
25
     * The console command description.
26
     *
27
     * @var string
28
     */
29
    protected $description = 'Feed the table companies from DB';
30
31
    /**
32
     * Create a new command instance.
33
     *
34
     */
35
    public function __construct()
36
    {
37
        parent::__construct();
38
    }
39
40
    /**
41
     * Execute the console command.
42
     *
43
     * @return mixed
44
     */
45
    public function handle()
46
    {
47
        $this->getSymbolsFormArray();
48
    }
49
50
    /**
51
     *
52
     */
53
    public function getSymbolsFormArray()
54
    {
55
        DB::table('companies')->truncate();
56
57
        $symbols_nasdq= '{"SymbolsNASDAQ":["AAL","AAPL","ADBE","ADI","ADP","ADSK","AKAM","ALXN","AMAT","AMGN","AMZN","ATVI","BBBY","BIDU","BIIB","BMRN","CA","CELG","CERN","CHKP","CHTR","CMCSA","COST","CSCO","CSX","CTRP","CTSH","CTXS","DISCA","DISCK","DISH","DLTR","EA","EBAY","ENDP","ESRX","EXPE","FAST","FB","FISV","FOX","FOXA","GILD","GOOG","GOOGL","HSIC","INCY","INTC","INTU","ILMN","ISRG","JD","KHC","LBTYA","LBTYK","LLTC","LMCA","LRCX","LVNTA","MAR","MAT","MDLZ","MNST","MSFT","MU","MXIM","MYL","NCLH","NFLX","NTAP","NVDA","NXPI","ORLY","PAYX","PCAR","PCLN","PYPL","QCOM","QVCA","REGN","ROST","SBAC","SBUX","SIRI","SNDK","SRCL","STX","SWKS","SYMC","TMUS","TSCO","TSLA","TRIP","TXN","ULTA","VIAB","VOD","VRSK","VRTX","WBA","WDC","WFM","XLNX","YHOO"]}';
58
59
        $symbols_nasdq = json_decode($symbols_nasdq);
60
61
        $symbols_nasdq = $symbols_nasdq->SymbolsNASDAQ;
62
63 View Code Duplication
        for($i=0; $i<count($symbols_nasdq); $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...
64
        {
65
            $symbol=$symbols_nasdq[$i];
66
            $gm = new GetMethods();
67
            $this->httpCall($gm,$symbol);
68
69
            sleep(4);
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
        echo("100 % Table companies fed\n");
86
87
    }
88
89
    /**
90
     * @param GetMethods $interaction_methods
91
     * @param $symbol
92
     */
93
    public function httpCall(GetMethods $interaction_methods, $symbol)
94
    {
95
        $glz_cli=new Client();
96
97
        $data = $interaction_methods->companyLookup($glz_cli,$symbol);
98
99
        $this->discardRepeated($data,$symbol);
100
101
    }
102
103
    /**
104
     * @param $data
105
     * @param $symbol
106
     */
107
    public function discardRepeated($data, $symbol)
108
    {
109
        for($i=0;$i<count($data);$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...
110
111
            $symbol_to_compare=(string) $data[$i]->Symbol;
112
113
            if($symbol_to_compare==$symbol){
114
115
                $node=$data[$i];
116
117
                $this->storeDataInDB($node);
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param $data
124
     */
125
    public function storeDataInDB($data)
126
    {
127
        DB::table('companies')->insert([
128
            [
129
                'symbol' => $data->Symbol,
130
                'name' => $data->Name,
131
                'exchange' => $data->Exchange,
132
                'created_at' => Carbon::now()->toDateTimeString(),
133
                'updated_at' => Carbon::now()->toDateTimeString()
134
            ]
135
        ]);
136
    }
137
138
139
}