Scanner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 4
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 phm\HttpWebdriverClient\Http\Response\SequenceAwareResponse;
8
use phmLabs\Components\Annovent\Dispatcher;
9
use phmLabs\Components\Annovent\Event\Event;
10
use Psr\Http\Message\ResponseInterface;
11
use whm\Smoke\Extensions\SmokeResponseRetriever\Retriever\Retriever;
12
use whm\Smoke\Http\ErrorResponse;
13
use whm\Smoke\Rules\CheckResult;
14
use whm\Smoke\Rules\ErrorResponseAwareRule;
15
use whm\Smoke\Rules\Rule;
16
17
18
class Scanner
19
{
20
    const ERROR = 'error';
21
    const PASSED = 'passed';
22
23
    /**
24
     * @var Rule[]
25
     */
26
    private $rules;
27
    private $eventDispatcher;
28
29
    private $responseRetriever;
30
31
    private $status = 0;
32
33
    public function __construct(array $rules, HttpClient $client, Dispatcher $eventDispatcher, Retriever $responseRetriever)
34
    {
35
        $eventDispatcher->simpleNotify('Scanner.Init', array('rules' => $rules, 'httpClient' => $client, 'dispatcher' => $eventDispatcher));
36
37
        $this->initRules($rules, $client);
38
39
        $this->eventDispatcher = $eventDispatcher;
40
41
        $this->responseRetriever = $responseRetriever;
42
43
        $this->eventDispatcher->simpleNotify('Scanner.Init.ResponseRetriever', array('responseRetriever' => $this->responseRetriever));
44
    }
45
46
    private function initRules($rules, HttpClient $client)
47
    {
48
        $this->rules = $rules;
49
        foreach ($this->rules as $rule) {
50
            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...
51
                $rule->setClient($client);
52
            }
53
        }
54
    }
55
56
    public function scan()
57
    {
58
        $this->eventDispatcher->simpleNotify('Scanner.Scan.Begin');
59
60
        while (($response = $this->responseRetriever->next()) && !$this->eventDispatcher->notifyUntil(new Event('Scanner.Scan.isStopped'))) {
61
62
            // this is the url filter
63
            if ($this->eventDispatcher->notifyUntil(new Event('Scanner.ProcessHtml.isFiltered', array('uri' => $response->getUri())))) {
64
                continue;
65
            }
66
67
            if ($response instanceof ErrorResponse) {
68
                $this->eventDispatcher->simpleNotify('Scanner.Scan.Response.Error', array('response' => $response));
69
            }
70
71
            $results = $this->checkResponse($response);
72
73
            if (count($results) === 0) {
74
                $checkResult = new CheckResult(CheckResult::STATUS_NONE, '');
75
                $checkResult->setResponse($response);
76
                $results = [$checkResult];
77
            }
78
79
            $this->eventDispatcher->simpleNotify('Scanner.Scan.Validate', array('results' => $results, 'response' => $response));
80
        }
81
82
        $this->eventDispatcher->simpleNotify('Scanner.Scan.Exceptions.Handle', array('occuredExceptions' => $this->responseRetriever->getOccuredExceptions()));
83
84
        $this->eventDispatcher->simpleNotify('Scanner.Scan.Finish');
85
    }
86
87
    public function getStatus()
88
    {
89
        return $this->status;
90
    }
91
92
    private function checkResponse(ResponseInterface $response)
93
    {
94
        $results = [];
95
96
        foreach ($this->rules as $name => $rule) {
97
            if ($this->eventDispatcher->notifyUntil(new Event('Scanner.CheckResponse.isFiltered', array('ruleName' => $name, 'rule' => $rule, 'response' => $response)))) {
98
                continue;
99
            }
100
101
            if ($response instanceof ErrorResponse && !($rule instanceof ErrorResponseAwareRule)) {
102
                continue;
103
            }
104
105
            try {
106
                $result = $rule->validate($response);
107
                if (!$result) {
108
                    $result = new CheckResult(CheckResult::STATUS_SUCCESS, 'Check successful.');
109
                }
110
            } catch (\Exception $e) {
111
                $result = new CheckResult(CheckResult::STATUS_FAILURE, 'An error occurred: ' . $e->getMessage());
112
                $this->eventDispatcher->simpleNotify('Scanner.CheckResponse.Rule.Error', array('checkResult' => $result, 'ruleName' => $name, 'exception' => $e));
113
            }
114
115
            $this->eventDispatcher->simpleNotify('Scanner.CheckResponse.Rule', array('checkResult' => $result, 'ruleName' => $name));
116
117
            $result->setResponse($response);
118
            $result->setRuleName($name);
119
            $results[$name] = $result;
120
        }
121
122
        if ($response instanceof InteractiveResponse) {
123
            if (!($response instanceof SequenceAwareResponse && $response->getSequenceResult())) {
124
                $response->getInteractionProcessor()->endInteraction();
125
            }
126
        }
127
128
        return $results;
129
    }
130
}
131