Passed
Pull Request — master (#8)
by Kevin
15:51
created

PingTaskRunner   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
eloc 10
c 1
b 0
f 1
dl 0
loc 28
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 7 1
A __construct() 0 7 4
A supports() 0 3 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\Exception\MissingDependency;
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 MissingDependency(PingTask::getMissingDependencyMessage());
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