use Cubist\Net\Util;
use Cubist\Util\CommandLine;
use Cubist\Util\Files\Files;
+use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class AuditLink extends Base
{
- public $c;
/**
* Create a new job instance.
*/
- public function __construct(FluidbookCollection $collection) {
- $this->c = $collection;
+ public function __construct() {
+ //
}
public function handle()
{
- $allLinks = FluidbookAuditLink::all();
+ $allLinks = FluidbookAuditLink::orderBy('last_date_test', 'asc')->limit(10);
+ $allLinks = $allLinks->inRandomOrder()->get()->toArray();
+ $externalLinks = [];
foreach ($allLinks as $fb => $link) {
// Error code start with 4 or 5
// Redirection code start with 3
- $httpCode = FluidbookAuditLink::getHttpCode($link['url']);
+ $curlResponse = FluidbookAuditLink::getHttpCode($link['url']);
+ $httpCode = $curlResponse['httpcode'];
+ $finalurl = '';
+ $finalcodeurl = '';
+
+ if(str_starts_with($httpCode, 3)) {
+ $finalurl = $curlResponse['finalurl'];
+ $finalcodeurl = FluidbookAuditLink::getHttpCode($curlResponse['finalurl'])['httpcode'];
+ }
+
$comment = FluidbookAuditLink::getHttpCodeComment($httpCode);
+ $firstTimeError = '';
+ if(str_starts_with($httpCode, 3) || str_starts_with($httpCode, 4) || str_starts_with($httpCode, 5)) {
+ $firstTimeError = null === $link['first_time_error'] || $httpCode !== $link['error_code'] ? date('Y-m-d H:i:s') : $link['first_time_error'];
+ }
+
$externalLinks[] = [
+ 'id' => $link['id'],
'error_code' => str_starts_with($httpCode, 4) || str_starts_with($httpCode, 5) ? $httpCode.$comment : "",
- 'first_time_error' => "", // Datetime of the first time we saw this error
+ 'first_time_error' => $firstTimeError, // Datetime of the first time we saw this error
'last_date_test' => date('Y-m-d H:i:s'),
'redirection_code' => str_starts_with($httpCode, 3) ? $httpCode : "",
- 'final_code_url' => '',
- 'final_target' => '',
+ 'final_code_url' => $finalcodeurl,
+ 'final_target' => $finalurl,
'updated_at'=> date('Y-m-d H:i:s')
];
}
- FluidbookAuditLink::update($externalLinks);
+ $keys = array_slice(array_keys($externalLinks[0]), 1);
+
+ DB::table('fluidbook_audit_link')->upsert($externalLinks, ['id'], $keys);
+
+ dd($externalLinks);
Log::info('Job exécuté avec succès');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+ curl_setopt($ch, CURLOPT_HEADER, true);
curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
+ $finalUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
+
curl_close($ch);
- return $httpcode;
+ return ['httpcode' => $httpcode, 'finalurl' => $finalUrl];
}
public static function getHttpCodeComment($httpcode) {