Passed
Pull Request — master (#8)
by Kevin
17:44
created

PingTaskRunner::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 4
c 1
b 0
f 1
dl 0
loc 7
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Zenstruck\ScheduleBundle\Schedule\Task\Runner;
4
5
use Symfony\Component\HttpClient\HttpClient;
6
use Symfony\Contracts\HttpClient\HttpClientInterface;
7
use Zenstruck\ScheduleBundle\Schedule\Extension\PingExtension;
8
use Zenstruck\ScheduleBundle\Schedule\Task;
9
use Zenstruck\ScheduleBundle\Schedule\Task\PingTask;
10
use Zenstruck\ScheduleBundle\Schedule\Task\Result;
11
use Zenstruck\ScheduleBundle\Schedule\Task\TaskRunner;
12
13
/**
14
 * @author Kevin Bond <[email protected]>
15
 */
16
final class PingTaskRunner implements TaskRunner
17
{
18
    private $httpClient;
19
20
    public function __construct(HttpClientInterface $httpClient = null)
21
    {
22
        if (null === $httpClient && !\class_exists(HttpClient::class)) {
23
            throw new \LogicException(\sprintf('Symfony HttpClient is required to use the "%s" extension. Install with "composer require symfony/http-client".', PingExtension::class));
24
        }
25
26
        $this->httpClient = $httpClient ?: HttpClient::create();
27
    }
28
29
    /**
30
     * @param PingTask|Task $task
31
     */
32
    public function __invoke(Task $task): Result
33
    {
34
        $response = $this->httpClient->request($task->getMethod(), $task->getUrl(), $task->getOptions());
0 ignored issues
show
Bug introduced by
The method getMethod() does not exist on Zenstruck\ScheduleBundle\Schedule\Task. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Task such as Zenstruck\ScheduleBundle\Schedule\Task\PingTask. ( Ignorable by Annotation )

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

34
        $response = $this->httpClient->request($task->/** @scrutinizer ignore-call */ getMethod(), $task->getUrl(), $task->getOptions());
Loading history...
Bug introduced by
The method getOptions() does not exist on Zenstruck\ScheduleBundle\Schedule\Task. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Task such as Zenstruck\ScheduleBundle\Schedule\Task\PingTask. ( Ignorable by Annotation )

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

34
        $response = $this->httpClient->request($task->getMethod(), $task->getUrl(), $task->/** @scrutinizer ignore-call */ getOptions());
Loading history...
Bug introduced by
The method getUrl() does not exist on Zenstruck\ScheduleBundle\Schedule\Task. It seems like you code against a sub-type of Zenstruck\ScheduleBundle\Schedule\Task such as Zenstruck\ScheduleBundle\Schedule\Task\PingTask. ( Ignorable by Annotation )

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

34
        $response = $this->httpClient->request($task->getMethod(), $task->/** @scrutinizer ignore-call */ getUrl(), $task->getOptions());
Loading history...
35
        $content = $response->getContent();
36
        $output = \array_merge($response->getInfo('response_headers'), ['', $content]);
37
38
        return Result::successful($task, \implode("\n", $output));
39
    }
40
41
    public function supports(Task $task): bool
42
    {
43
        return $task instanceof PingTask;
44
    }
45
}
46