Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
21 / 21 |
GetOffers | |
100.00% |
1 / 1 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
21 / 21 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
handle | |
100.00% |
1 / 1 |
4 | |
100.00% |
18 / 18 |
|||
offersNeeded | |
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\SiteGeneratorOffers\Console\Commands; |
4 | |
5 | use Faker\Factory as Faker; |
6 | use GuzzleHttp\Client; |
7 | use Illuminate\Console\Command; |
8 | use Illuminate\Support\Facades\Redis; |
9 | use Qmp\Laravel\SiteGeneratorOffers\Models\Offer; |
10 | use Qmp\Laravel\SiteGeneratorOffers\Retriever\InterfaceProvider; |
11 | |
12 | class GetOffers extends Command |
13 | { |
14 | /** |
15 | * The name and signature of the console command. |
16 | * |
17 | * @var string |
18 | */ |
19 | protected $signature = 'offers:get'; |
20 | |
21 | /** |
22 | * The console command description. |
23 | * |
24 | * @var string |
25 | */ |
26 | protected $description = 'Get offers on digitalv2'; |
27 | |
28 | /** |
29 | * DB Connection |
30 | * |
31 | * @var string |
32 | */ |
33 | protected $connection = 'siteGeneratorMongo'; |
34 | |
35 | /** |
36 | * Cached redis key |
37 | * |
38 | * @var string |
39 | */ |
40 | protected $redisKey = 'digital_v3_cache:application/site_generator/offers'; |
41 | |
42 | /** |
43 | * Create a new command instance. |
44 | * |
45 | * @return void |
46 | */ |
47 | public function __construct() |
48 | { |
49 | parent::__construct(); |
50 | } |
51 | |
52 | /** |
53 | * Execute the console command. |
54 | * |
55 | * @return mixed |
56 | */ |
57 | public function handle() |
58 | { |
59 | $this->info('begin : Get offers'); |
60 | |
61 | if($this->offersNeeded()) { |
62 | if (\Schema::connection($this->connection)->hasTable('offers')) { |
63 | $offers = resolve(InterfaceProvider::class)->retrieve(); |
64 | |
65 | $offers->each(function ($offerArray) { |
66 | $offer = Offer::find(strval($offerArray['id'])); |
67 | |
68 | if($offer) { |
69 | $offer->update($offerArray); |
70 | } else { |
71 | $offerArray['segmentation'] = [ |
72 | 'type' => '', |
73 | 'rows' => [] |
74 | ]; |
75 | |
76 | $offerArray['disappear'] = false; |
77 | |
78 | $offer= Offer::create($offerArray); |
79 | } |
80 | }); |
81 | |
82 | Redis::connection('cache')->set($this->redisKey, 1); |
83 | Redis::publish('site-generator-config', json_encode(['action' => 'update-offers'])); |
84 | |
85 | $this->info('end : Get offers'); |
86 | }else { |
87 | $this->info('end : Offers collection does not exist'); |
88 | } |
89 | |
90 | } else { |
91 | $this->info('end : Get offers is not necessary '); |
92 | } |
93 | } |
94 | |
95 | /** |
96 | * Check if get offers is necessary |
97 | * |
98 | * @return bool |
99 | */ |
100 | private function offersNeeded() |
101 | { |
102 | return env('APP_ENV') === 'production' || !Redis::connection('cache')->exists($this->redisKey); |
103 | } |
104 | } |