Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SolvFetcher
0.00% covered (danger)
0.00%
0 / 28
0.00% covered (danger)
0.00%
0 / 5
72
0.00% covered (danger)
0.00%
0 / 1
 fetchEventsCsvForYear
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 fetchYearlyResultsJson
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 fetchEventResultsHtml
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 curlInit
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 sleep
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace Olz\Fetchers;
4
5use Olz\Utils\WithUtilsTrait;
6
7class SolvFetcher {
8    use WithUtilsTrait;
9
10    private string $base_url = "https://www.o-l.ch/";
11    /** @var non-empty-string */
12    protected static string $user_agent_string = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36';
13
14    public function fetchEventsCsvForYear(int|string $year): ?string {
15        $this->sleep(3);
16
17        $path = "cgi-bin/fixtures";
18        $query = "?=&year={$year}&kind=-1&csv=1";
19        $url = "{$this->base_url}{$path}{$query}";
20
21        $ch = $this->curlInit($url);
22        $result = $this->httpUtils()->curlExec($ch);
23
24        return iconv('ISO-8859-1', 'UTF-8', $result) ?: '';
25    }
26
27    public function fetchYearlyResultsJson(int|string $year): ?string {
28        $this->sleep(3);
29
30        $path = "cgi-bin/fixtures";
31        $query = "?mode=results&year={$year}&json=1";
32        $url = "{$this->base_url}{$path}{$query}";
33
34        $ch = $this->curlInit($url);
35        $result = $this->httpUtils()->curlExec($ch);
36
37        return iconv('ISO-8859-1', 'UTF-8', $result) ?: '';
38    }
39
40    public function fetchEventResultsHtml(int|string $rank_id): ?string {
41        $this->sleep(3);
42
43        $path = "cgi-bin/results";
44        $query = "?rl_id={$rank_id}&club=OL+Zimmerberg&zwizt=1";
45        $url = "{$this->base_url}{$path}{$query}";
46
47        $ch = $this->curlInit($url);
48        $result = $this->httpUtils()->curlExec($ch);
49
50        return html_entity_decode(iconv('ISO-8859-1', 'UTF-8', $result) ?: '');
51    }
52
53    /** @param non-empty-string $url */
54    protected function curlInit(string $url): \CurlHandle {
55        $ch = $this->httpUtils()->curlInit($url, [
56            'userAgent' => self::$user_agent_string,
57        ]);
58        curl_setopt($ch, CURLOPT_REFERER, 'https://www.o-l.ch/cgi-bin/fixtures');
59        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
60        return $ch;
61    }
62
63    protected function sleep(int $seconds): void {
64        sleep($seconds);
65    }
66}