Completed
Push — master ( 291e85...e418c2 )
by Guillaume
02:40
created

Runner::run()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 68
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
cc 4
eloc 40
nc 1
nop 0
dl 0
loc 68
rs 8.7864
c 5
b 0
f 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Runner;
17
18
use GuzzleHttp\Client;
19
use GuzzleHttp\Pool;
20
use GuzzleHttp\Promise;
21
use GuzzleHttp\Psr7\Request;
22
use GuzzleHttp\TransferStats;
23
use Hogosha\Monitor\Client\GuzzleClient;
24
use Hogosha\Monitor\Model\Result;
25
use Hogosha\Monitor\Model\ResultCollection;
26
use Hogosha\Monitor\Model\UrlProvider;
27
28
/**
29
 * @author Guillaume Cavana <[email protected]>
30
 */
31
class Runner
32
{
33
    protected $urlProvider;
34
35
    protected $client;
36
37
    /**
38
     * Constructor.
39
     *
40
     * @param UrlProvider  $urlProvider
41
     * @param GuzzleClient $client
42
     */
43
    public function __construct(UrlProvider $urlProvider, Client $client)
44
    {
45
        $this->urlProvider = $urlProvider;
46
        $this->client = $client;
47
    }
48
49
    /**
50
     * run.
51
     *
52
     * @return array
53
     */
54
    public function run()
55
    {
56
        $urls = $this->urlProvider->getUrls();
57
        $client = $this->client;
58
59
        //This is a bit messie, need a refacto
60
        $resultCollection = new ResultCollection();
61
62
        $requests = function () use ($urls, $client, $resultCollection) {
63
            foreach ($urls as $url) {
64
                yield function () use ($client, $url, $resultCollection) {
65
                    return $client->sendAsync(
66
                        new Request(
67
                            $url->getMethod(),
68
                            $url->getUrl(),
69
                            $url->getHeaders()
70
                        ),
71
                        [
72
                            'timeout' => $url->getTimeout(),
73
                            'connect_timeout' => $url->getTimeout(),
74
                            'on_stats' => function (TransferStats $tranferStats) use ($url, $resultCollection) {
75
76
                                $handlerError = null;
77
                                $validatorError = null;
78
79
                                if ($tranferStats->hasResponse()) {
80
                                    $validatorResult = $url->getValidator()->check((string) $tranferStats->getResponse()->getBody());
81
82
                                    if (false === $validatorResult) {
83
                                        $validatorError = $url->getValidator()->getError();
84
                                    }
85
86
                                    $statusCode = $tranferStats->getResponse()->getStatusCode();
87
                                    $transferTime = $tranferStats->getTransferTime();
88
                                } else {
89
                                    // If we have a connection error
90
                                    $statusCode = 400;
91
                                    $transferTime = 0;
92
                                    $handlerError = curl_strerror($tranferStats->getHandlerErrorData());
93
                                }
94
95
                                $resultCollection->append(
96
                                    (new Result(
97
                                        $url,
98
                                        $statusCode,
99
                                        $transferTime,
100
                                        $handlerError,
101
                                        $validatorResult,
0 ignored issues
show
Bug introduced by
The variable $validatorResult does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
102
                                        $validatorError
103
                                    ))
104
                                );
105
                            },
106
                        ]
107
                    );
108
                };
109
            }
110
        };
111
112
        $pool = new Pool($this->client, $requests(), [
113
            'concurrency' => 5,
114
        ]);
115
116
        $promise = $pool->promise();
117
118
        $promise->wait();
119
120
        return $resultCollection;
121
    }
122
}
123