Completed
Push — master ( 7e536e...5c7ae7 )
by Dev
24:11 queued 09:15
created

RobotsTxtTrait::setRobotsTxt()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
3
namespace PiedWeb\UrlHarvester;
4
5
use Spatie\Robots\RobotsTxt;
6
use PiedWeb\Curl\Request as CurlRequest;
7
8
trait RobotsTxtTrait
9
{
10
    /** @var RobotsTxt|string (empty string) */
11
    protected $robotsTxt;
12
13
    /**
14
     * @return RobotsTxt|string containing the current Robots.txt or NULL if an error occured
15
     *                          or empty string if robots is empty file
16
     */
17
    public function getRobotsTxt()
18
    {
19
        if (null === $this->robotsTxt) {
20
            $url = $this->getDomainAndScheme().'/robots.txt';
0 ignored issues
show
Bug introduced by
It seems like getDomainAndScheme() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
            $url = $this->/** @scrutinizer ignore-call */ getDomainAndScheme().'/robots.txt';
Loading history...
21
22
            $request = new CurlRequest($url);
23
            $request
24
                ->setDefaultSpeedOptions()
25
                ->setDownloadOnly('0-500000')
26
                ->setUserAgent($this->getResponse()->getRequest()->getUserAgent())
0 ignored issues
show
Bug introduced by
It seems like getResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
                ->setUserAgent($this->/** @scrutinizer ignore-call */ getResponse()->getRequest()->getUserAgent())
Loading history...
27
            ;
28
            $result = $request->exec();
29
30
            if (!$result instanceof \PiedWeb\Curl\Response
31
                || false === stripos($result->getContentType(), 'text/plain')
32
                || empty(trim($result->getContent()))
33
            ) {
34
                $this->robotsTxt = '';
35
            } else {
36
                $this->robotsTxt = new RobotsTxt($result->getContent());
37
            }
38
        }
39
40
        return $this->robotsTxt;
41
    }
42
43
    /**
44
     * @param RobotsTxt|string $robotsTxt
45
     *
46
     * @return self
47
     */
48
    public function setRobotsTxt($robotsTxt)
49
    {
50
        $this->robotsTxt = is_string($robotsTxt) ? (empty($robotsTxt) ? '' : new RobotsTxt($robotsTxt)) : $robotsTxt;
51
52
        return $this;
53
    }
54
}
55