Runner   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 10
Bugs 1 Features 5
Metric Value
c 10
b 1
f 5
dl 0
loc 94
rs 10
wmc 5
lcom 1
cbo 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B run() 0 70 4
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
                                $validatorResult = null;
79
80
                                if ($tranferStats->hasResponse()) {
81
                                    $validatorResult = $url->getValidator()->check((string) $tranferStats->getResponse()->getBody());
82
83
                                    if (false === $validatorResult) {
84
                                        $validatorError = $url->getValidator()->getError();
85
                                    }
86
87
                                    $statusCode = $tranferStats->getResponse()->getStatusCode();
88
                                    $transferTime = $tranferStats->getTransferTime();
89
                                } else {
90
                                    // If we have a connection error
91
                                    $statusCode = 400;
92
                                    $transferTime = 0;
93
                                    $handlerError = curl_strerror($tranferStats->getHandlerErrorData());
94
                                }
95
96
                                $resultCollection->offsetSet(
97
                                    $url->getName(),
98
                                    (new Result(
99
                                        $url,
100
                                        $statusCode,
101
                                        $transferTime,
102
                                        $handlerError,
103
                                        $validatorResult,
104
                                        $validatorError
105
                                    ))
106
                                );
107
                            },
108
                        ]
109
                    );
110
                };
111
            }
112
        };
113
114
        $pool = new Pool($this->client, $requests(), [
115
            'concurrency' => 5,
116
        ]);
117
118
        $promise = $pool->promise();
119
120
        $promise->wait();
121
122
        return $resultCollection;
123
    }
124
}
125