• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tomasnorre / crawler / 11237471329

08 Oct 2024 02:20PM UTC coverage: 68.586% (-1.3%) from 69.862%
11237471329

push

github

web-flow
ci: Update coveralls workflow (#1109)

1834 of 2674 relevant lines covered (68.59%)

3.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.42
/Classes/CrawlStrategy/GuzzleExecutionStrategy.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace AOE\Crawler\CrawlStrategy;
6

7
/*
8
 * (c) 2020 AOE GmbH <dev@aoe.com>
9
 *
10
 * This file is part of the TYPO3 Crawler Extension.
11
 *
12
 * It is free software; you can redistribute it and/or modify it under
13
 * the terms of the GNU General Public License, either version 2
14
 * of the License, or any later version.
15
 *
16
 * For the full copyright and license information, please read the
17
 * LICENSE.txt file that was distributed with this source code.
18
 *
19
 * The TYPO3 project - inspiring people to share!
20
 */
21

22
use GuzzleHttp\Exception\ConnectException;
23
use GuzzleHttp\Exception\RequestException;
24
use Psr\Http\Message\ResponseInterface;
25
use Psr\Http\Message\UriInterface;
26
use Psr\Log\LoggerAwareInterface;
27
use Psr\Log\LoggerAwareTrait;
28
use TYPO3\CMS\Core\Http\Client\GuzzleClientFactory;
29
use TYPO3\CMS\Core\Http\RequestFactory;
30
use TYPO3\CMS\Core\Utility\GeneralUtility;
31

32
/**
33
 * Calls Guzzle / CURL (based on TYPO3 settings) for fetching a URL.
34
 * @internal since v12.0.0
35
 */
36
class GuzzleExecutionStrategy implements LoggerAwareInterface, CrawlStrategyInterface
37
{
38
    use LoggerAwareTrait;
39

40
    /**
41
     * Sets up a CURL / Guzzle Request for fetching the request.
42
     *
43
     * @return bool|mixed
44
     */
45
    public function fetchUrlContents(UriInterface $url, string $crawlerId)
46
    {
47
        $reqHeaders = $this->buildRequestHeaders($crawlerId);
3✔
48

49
        $options = [
3✔
50
            'headers' => $reqHeaders,
3✔
51
        ];
3✔
52
        if ($url->getUserInfo()) {
3✔
53
            $options['auth'] = explode(':', $url->getUserInfo());
×
54
        }
55
        try {
56
            $url = (string) $url;
3✔
57
            $response = $this->getResponse($url, $options);
3✔
58
            return unserialize($response->getHeaderLine('X-T3Crawler-Meta'));
2✔
59
        } catch (RequestException $e) {
1✔
60
            $response = $e->getResponse();
×
61
            $message = ($response ? $response->getStatusCode() : 0)
×
62
                . chr(32)
×
63
                . ($response ? $response->getReasonPhrase() : $e->getMessage());
×
64

65
            $this->logger->debug(
×
66
                sprintf('Error while opening "%s" - ' . $message, $url),
×
67
                [
×
68
                    'crawlerId' => $crawlerId,
×
69
                ]
×
70
            );
×
71
            return $message;
×
72
        } catch (ConnectException $e) {
1✔
73
            $message = $e->getCode() . chr(32) . $e->getMessage();
1✔
74

75
            $this->logger->debug(
1✔
76
                sprintf('Error while opening "%s" - ' . $message, $url),
1✔
77
                [
1✔
78
                    'crawlerId' => $crawlerId,
1✔
79
                ]
1✔
80
            );
1✔
81
            return $message;
1✔
82
        }
83
    }
84

85
    protected function getResponse(string $url, array $options): ResponseInterface
86
    {
87
        $guzzleClientFactory = GeneralUtility::makeInstance(GuzzleClientFactory::class);
2✔
88
        return GeneralUtility::makeInstance(RequestFactory::class, $guzzleClientFactory)
2✔
89
            ->request($url, 'GET', $options);
2✔
90
    }
91

92
    /**
93
     * Builds HTTP request headers.
94
     */
95
    private function buildRequestHeaders(string $crawlerId): array
96
    {
97
        return [
3✔
98
            'Connection' => 'close',
3✔
99
            'X-T3Crawler' => $crawlerId,
3✔
100
            'User-Agent' => 'TYPO3 crawler',
3✔
101
        ];
3✔
102
    }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc