Completed
Push — master ( 61044a...94728c )
by Rafał
24:26 queued 10:49
created

WebhookContext   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 4
dl 9
loc 27
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A thePayloadReceivedByWebhookShouldBeEqualTo() 0 11 2
A normalizeJson() 9 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Behat\Contexts;
6
7
use Behat\Behat\Context\Context;
8
use Behat\Gherkin\Node\PyStringNode;
9
use GuzzleHttp\Client;
10
11
class WebhookContext implements Context
12
{
13
    /**
14
     * @Given The payload received by :url webhook should be equal to:
15
     */
16
    public function thePayloadReceivedByWebhookShouldBeEqualTo(string $url, PyStringNode $body): void
17
    {
18
        $client = new Client();
19
        $response = $client->request('GET', $url);
20
        $actual = $this->normalizeJson($response->getBody()->getContents());
21
        $expected = $this->normalizeJson($body->getRaw());
22
23
        if ($actual !== $expected) {
24
            throw new \Exception("The actual body: \n $actual \n\n is not equal to expected: \n $expected");
25
        }
26
    }
27
28 View Code Duplication
    private function normalizeJson(string $value): string
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
29
    {
30
        $value = \json_decode($value, true);
31
        if (JSON_ERROR_NONE !== \json_last_error()) {
32
            throw new \Exception("The string '$value' is not valid json");
33
        }
34
35
        return \json_encode($value);
36
    }
37
}
38