Completed
Push — master ( 695a14...c5a701 )
by Nils
02:26
created

Scanner::scan()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 8.8017
c 0
b 0
f 0
cc 6
nc 5
nop 0
1
<?php
2
3
namespace whm\Smoke\Scanner;
4
5
use phm\HttpWebdriverClient\Http\Client\HttpClient;
6
use phm\HttpWebdriverClient\Http\Response\InteractiveResponse;
7
use phmLabs\Components\Annovent\Dispatcher;
8
use phmLabs\Components\Annovent\Event\Event;
9
use Psr\Http\Message\ResponseInterface;
10
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
11
use whm\Smoke\Http\ErrorResponse;
12
use whm\Smoke\Rules\CheckResult;
13
use whm\Smoke\Rules\Rule;
14
use whm\Smoke\Rules\ValidationFailedException;
15
16
class Scanner
17
{
18
    const ERROR = 'error';
19
    const PASSED = 'passed';
20
21
    /**
22
     * @var Rule[]
23
     */
24
    private $rules;
25
    private $eventDispatcher;
26
27
    private $responseRetriever;
28
29
    private $status = 0;
30
31
    public function __construct(array $rules, HttpClient $client, Dispatcher $eventDispatcher, Retriever $responseRetriever)
32
    {
33
        $eventDispatcher->simpleNotify('Scanner.Init', array('rules' => $rules, 'httpClient' => $client, 'dispatcher' => $eventDispatcher));
34
35
        $this->initRules($rules, $client);
36
37
        $this->eventDispatcher = $eventDispatcher;
38
39
        $this->responseRetriever = $responseRetriever;
40
41
        $this->eventDispatcher->simpleNotify('Scanner.Init.ResponseRetriever', array('responseRetriever' => $this->responseRetriever));
42
    }
43
44
    private function initRules($rules, HttpClient $client)
45
    {
46
        $this->rules = $rules;
47
        foreach ($this->rules as $rule) {
48
            if ($rule instanceof ClientAware) {
0 ignored issues
show
Bug introduced by
The class whm\Smoke\Scanner\ClientAware does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
49
                $rule->setClient($client);
50
            }
51
        }
52
    }
53
54
    public function scan()
55
    {
56
        $this->eventDispatcher->simpleNotify('Scanner.Scan.Begin');
57
58
        while (($response = $this->responseRetriever->next()) && !$this->eventDispatcher->notifyUntil(new Event('Scanner.Scan.isStopped'))) {
59
60
            // this is the url filter
61
            if ($this->eventDispatcher->notifyUntil(new Event('Scanner.ProcessHtml.isFiltered', array('uri' => $response->getUri())))) {
62
                continue;
63
            }
64
65
            if ($response instanceof ErrorResponse) {
66
                $this->eventDispatcher->simpleNotify('Scanner.Scan.Response.Error', array('response' => $response));
67
                continue;
68
            } else {
69
                $results = $this->checkResponse($response);
70
            }
71
72
            if (count($results) === 0) {
73
                $checkResult = new CheckResult(CheckResult::STATUS_NONE, '');
74
                $checkResult->setResponse($response);
75
                $results = [$checkResult];
76
            }
77
78
            $this->eventDispatcher->simpleNotify('Scanner.Scan.Validate', array('results' => $results, 'response' => $response));
79
        }
80
81
        $this->eventDispatcher->simpleNotify('Scanner.Scan.Exceptions.Handle', array('occuredExceptions' => $this->responseRetriever->getOccuredExceptions()));
82
83
        $this->eventDispatcher->simpleNotify('Scanner.Scan.Finish');
84
    }
85
86
    public function getStatus()
87
    {
88
        return $this->status;
89
    }
90
91
    private function checkResponse(ResponseInterface $response)
92
    {
93
        $results = [];
94
95
        foreach ($this->rules as $name => $rule) {
96
            if ($this->eventDispatcher->notifyUntil(new Event('Scanner.CheckResponse.isFiltered', array('ruleName' => $name, 'rule' => $rule, 'response' => $response)))) {
97
                continue;
98
            }
99
100
            try {
101
                $result = $rule->validate($response);
102
                if (!$result) {
103
                    $result = new CheckResult(CheckResult::STATUS_SUCCESS, 'Check successful.');
104
                }
105
            } catch (\Exception $e) {
106
                $result = new CheckResult(CheckResult::STATUS_FAILURE, 'An error occurred: ' . $e->getMessage());
107
                $this->eventDispatcher->simpleNotify('Scanner.CheckResponse.Rule.Error', array('checkResult' => $result, 'ruleName' => $name, 'exception' => $e));
108
            }
109
110
            $this->eventDispatcher->simpleNotify('Scanner.CheckResponse.Rule', array('checkResult' => $result, 'ruleName' => $name));
111
112
            $result->setResponse($response);
113
            $result->setRuleName($name);
114
            $results[$name] = $result;
115
        }
116
117
        if ($response instanceof InteractiveResponse) {
0 ignored issues
show
Bug introduced by
The class phm\HttpWebdriverClient\...nse\InteractiveResponse does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
118
            $response->getInteractionProcessor()->endInteraction();
119
        }
120
121
        return $results;
122
    }
123
}
124