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