MyHomeCollection/app/Parser.php

124 lines
2.7 KiB
PHP
Raw Permalink Normal View History

2020-07-23 12:26:10 +02:00
<?php
namespace App;
2020-11-09 17:35:50 +01:00
use App\Exceptions\UnknownParser;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Config;
2020-07-23 12:26:10 +02:00
abstract class Parser
{
protected $client;
protected $url;
protected function __construct($url)
{
$this->url = $url;
$this->client = new Client([
2020-07-23 12:26:10 +02:00
'timeout' => 60,
'verify' => false,
'headers' => [
'user-agent' => 'Dalvik/2.1.0 (Linux; U; Android 6.0.1; D5803 Build/MOB30M.Z1)',
],
]);
}
/**
* @param string $url
*
* @return \App\Parser|null
* @throws \App\Exceptions\UnknownParser
2020-07-23 12:26:10 +02:00
*/
public static function factory(string $url): ?Parser
2020-07-23 12:26:10 +02:00
{
$parsers = Config::get('parser.parsers', []);
foreach ($parsers as $domain => $parser) {
if (false !== strpos($url, $domain)) {
return new $parser($url);
}
2020-07-23 12:26:10 +02:00
}
2020-11-09 17:35:50 +01:00
throw new UnknownParser(sprintf(
'Can\'t find an url in «%s» file with the following URL «%s». Please update the file accordingly.',
'parser.php',
$url
));
2020-07-23 12:26:10 +02:00
}
/**
* @return \App\ParsedHome
*/
abstract public function parse(): ParsedHome;
/**
* @param int $score
*
* @return string
*/
protected function calculateDPE($score)
{
if (empty($score)) {
return 'Inconnu';
}
if ($score <= 50) {
return 'A';
}
if ($score >= 51 && $score <= 90) {
return 'B';
}
if ($score >= 91 && $score <= 150) {
return 'C';
}
if ($score >= 151 && $score <= 230) {
return 'D';
}
if ($score >= 231 && $score <= 330) {
return 'E';
}
if ($score >= 331 && $score <= 450) {
return 'F';
}
if ($score > 450) {
return 'G';
}
return 'Inconnu';
}
/**
* @param $score
*
* @return string
*/
2021-02-03 16:43:17 +01:00
protected function calculateGES($score)
{
if (empty($score)) {
return 'Inconnu';
}
if ($score <= 5) {
return 'A';
}
if ($score >= 6 && $score <= 10) {
return 'B';
}
if ($score >= 11 && $score <= 20) {
return 'C';
}
if ($score >= 21 && $score <= 35) {
return 'D';
}
if ($score >= 36 && $score <= 55) {
return 'E';
}
if ($score >= 56 && $score <= 80) {
return 'F';
}
if ($score > 80) {
return 'G';
}
return 'Inconnu';
}
2020-07-23 12:26:10 +02:00
}