Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 54 |
| SendEmail | |
0.00% |
0 / 1 |
|
0.00% |
0 / 4 |
110 | |
0.00% |
0 / 54 |
| handle | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 26 |
|||
| setConnections | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 17 |
|||
| createModels | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 4 |
|||
| check | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 7 |
|||
| 1 | <?php |
| 2 | |
| 3 | namespace Qmp\Laravel\Deduplication\Console\Commands; |
| 4 | |
| 5 | use Carbon\Carbon; |
| 6 | use Illuminate\Console\Command; |
| 7 | use Illuminate\Database\Eloquent\Model; |
| 8 | use Illuminate\Support\Facades\Config; |
| 9 | use Qmp\Laravel\Deduplication\Mail\DedupMailableFactory; |
| 10 | use Qmp\Laravel\Deduplication\Models\Session; |
| 11 | use Qmp\Laravel\Mail\MailSender; |
| 12 | |
| 13 | class SendEmail extends Command |
| 14 | { |
| 15 | /** |
| 16 | * The name and signature of the console command. |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | protected $signature = 'dedup:mail {sessionId} {emailType} {from} {test=true}'; |
| 21 | /** |
| 22 | * The console command description. |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $description = 'Send dedup email'; |
| 27 | |
| 28 | /** |
| 29 | * Undocumented variable |
| 30 | * |
| 31 | * @var [type] |
| 32 | */ |
| 33 | protected $session; |
| 34 | |
| 35 | /** |
| 36 | * Undocumented variable |
| 37 | * |
| 38 | * @var [type] |
| 39 | */ |
| 40 | protected $user; |
| 41 | |
| 42 | /** |
| 43 | * Undocumented variable |
| 44 | * |
| 45 | * @var array |
| 46 | */ |
| 47 | protected $usersFrom = [ |
| 48 | 'jb' => 'Jean-Baptiste Leturque', |
| 49 | 'alexis' => 'Alexis Baudran', |
| 50 | 'marjorie' => 'Marjorie Dien', |
| 51 | 'stephane' => 'Stpehane Ziliani', |
| 52 | 'aurore' => 'Aurore Gary' |
| 53 | ]; |
| 54 | |
| 55 | /** |
| 56 | * Undocumented variable |
| 57 | * |
| 58 | * @var array |
| 59 | */ |
| 60 | protected $authorized = ['inform','relaunch','available']; |
| 61 | |
| 62 | /** |
| 63 | * Execute the console command. |
| 64 | * |
| 65 | * @return mixed |
| 66 | */ |
| 67 | public function handle(): void |
| 68 | { |
| 69 | $this->setConnections()->createModels(); |
| 70 | $this->check(); |
| 71 | |
| 72 | $session = $this->session->findOrFail($this->argument('sessionId')); |
| 73 | $emailType = $this->argument('emailType'); |
| 74 | $user = $this->usersFrom[$this->argument('from')]; |
| 75 | |
| 76 | Carbon::setLocale('fr'); |
| 77 | |
| 78 | $data = [ |
| 79 | 'campaign' => $session->name, |
| 80 | 'date' => Carbon::parse($session->deadLine)->translatedFormat('l d F Y H:i'), |
| 81 | 'dateAvailable' => Carbon::parse($session->availableOn)->translatedFormat('l d F Y'), |
| 82 | 'hash' => $session->hashType, |
| 83 | 'user' => $user |
| 84 | ]; |
| 85 | |
| 86 | $mailable = DedupMailableFactory::$emailType($data); |
| 87 | |
| 88 | $ids = $emailType === 'available' ? $session->getUsersIdsWithFiles() : $session->getUsersIdsNoFiles(); |
| 89 | |
| 90 | $emails = collect($this->user->all())->whereIn('id', $ids)->pluck('email'); |
| 91 | |
| 92 | $emails = collect(['david.quartino@qwamplify-performance.com']); |
| 93 | |
| 94 | $this->table(['email'], [$emails->toArray()]); |
| 95 | |
| 96 | $go = $this->choice( |
| 97 | "Send email type '$emailType' to session $session->name with user $user ?", |
| 98 | ['Yes', 'No'], |
| 99 | 1 |
| 100 | ); |
| 101 | |
| 102 | if($go === 'Yes') { |
| 103 | foreach($emails as $email) { |
| 104 | $this->info("Send to $email"); |
| 105 | MailSender::model($mailable)->to($email)->dispatch(); |
| 106 | } |
| 107 | } else { |
| 108 | $this->error('Abort'); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Undocumented function |
| 114 | * |
| 115 | * @return void |
| 116 | */ |
| 117 | protected function setConnections() |
| 118 | { |
| 119 | Config::set("database.connections.deduplicationMongoTemp", [ |
| 120 | 'driver' => 'mongodb', |
| 121 | 'dsn' => 'mongodb://51.178.79.192:27017/admin', |
| 122 | 'host' => '51.178.79.192', |
| 123 | 'port' => '27017', |
| 124 | 'database' => 'deduplication', |
| 125 | 'username' => 'admin', |
| 126 | 'password' => 'HzdgVEf564465(jy-GVECjh', |
| 127 | 'options' => [ |
| 128 | 'database' => 'admin' |
| 129 | ] |
| 130 | ]); |
| 131 | |
| 132 | Config::set("database.connections.externalUsersTemp", [ |
| 133 | 'driver' => 'mysql', |
| 134 | 'url' => env('DATABASE_URL'), |
| 135 | 'host' => '51.178.79.192', |
| 136 | 'port' => '3306', |
| 137 | 'database' => 'external_users', |
| 138 | 'username' => 'digital', |
| 139 | 'password' => 'jcg8HvAKsGb^?4H*y8_*=uBfyKb7D5#v', |
| 140 | 'unix_socket' => env('DB_SOCKET', ''), |
| 141 | 'charset' => 'utf8mb4', |
| 142 | 'collation' => 'utf8mb4_unicode_ci', |
| 143 | 'prefix' => '', |
| 144 | 'prefix_indexes' => true, |
| 145 | 'strict' => true, |
| 146 | 'engine' => null, |
| 147 | 'options' => extension_loaded('pdo_mysql') ? array_filter([ |
| 148 | \PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'), |
| 149 | ]) : [], |
| 150 | ]); |
| 151 | |
| 152 | return $this; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Undocumented function |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | protected function createModels() |
| 161 | { |
| 162 | $this->session = new Session; |
| 163 | $this->session->setConnection('deduplicationMongoTemp'); |
| 164 | |
| 165 | $this->user = new class extends Model |
| 166 | { |
| 167 | protected $connection = 'externalUsersTemp'; |
| 168 | protected $table = 'users'; |
| 169 | }; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Undocumented function |
| 174 | * |
| 175 | * @return void |
| 176 | */ |
| 177 | protected function check() { |
| 178 | if(!in_array($this->argument('emailType'), $this->authorized)) { |
| 179 | $this->error('Email type not found in ' . implode(', ', $this->authorized)); |
| 180 | exit(); |
| 181 | } |
| 182 | |
| 183 | if(!array_key_exists($this->argument('from'), $this->usersFrom)) { |
| 184 | $this->error('User not found in ' . implode(', ', array_keys($this->usersFrom))); |
| 185 | exit(); |
| 186 | } |
| 187 | } |
| 188 | } |