Completed
Pull Request — master (#9)
by Lars
02:23
created

Response   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 22.99 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 20%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 20
loc 87
ccs 10
cts 50
cp 0.2
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getCount() 0 8 1
B getBody() 20 30 6
A isOk() 0 19 4

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
 * SDK to communicate with EDBBrugs
4
 *
5
 * PHP Version 5
6
 *
7
 * @category EDBBrugs
8
 * @package  EDBBrugs
9
 * @author   Lars Olesen <[email protected]>
10
 * @license  MIT Open Source License https://opensource.org/licenses/MIT
11
 * @version  GIT: <git_id>
12
 */
13
14
namespace EDBBrugs;
15
16
use EDBBrugs\ResponseInterface;
17
18
/**
19
 * Service Communicator with EDB-Brugs
20
 *
21
 * @category EDBBrugs
22
 * @package  EDBBrugs
23
 * @author   Lars Olesen <[email protected]>
24
 * @license  MIT Open Source License https://opensource.org/licenses/MIT
25
 * @version  GIT: <git_id>
26
 */
27
class Response implements ResponseInterface
28
{
29
    protected $responseType;
30
    protected $response;
31
32
    /**
33
     * Constructor
34
     *
35
     * @param object $responseType Response type
36
     * @param object $response     Actual response from SOAP
37
     */
38 1
    public function __construct($responseType, $response)
39
    {
40 1
        $this->responseType = $responseType;
41 1
        $this->response = $response;
42 1
    }
43
44 1
    public function getCount()
45
    {
46 1
        return str_replace(
47 1
            'Oprettelse Ok, nye tilmeldinger: ',
48 1
            '',
49 1
            $this->response->NyTilmelding2Result
50 1
        );
51
    }
52
53
    /**
54
     * Add new registration to EDBBrugs
55
     *
56
     * @return mixed (number of successful registrations) or throws Exception
57
     */
58
    public function getBody()
59
    {
60
        switch ($this->responseType) {
61 View Code Duplication
            case 'NyTilmelding2':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
62
                if (!$this->isOk()) {
63
                    throw new \Exception($this->response->NyTilmelding2Result);
64
                }
65
                return str_replace(
66
                    'Oprettelse Ok, nye tilmeldinger: ',
67
                    '',
68
                    $this->response->NyTilmelding2Result
69
                );
70
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
71 View Code Duplication
            case 'SletTilmeldingerV2':
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
72
                if (!$this->isOk()) {
73
                    throw new \Exception($this->response->SletTilmeldingerV2Result);
74
                }
75
                return str_replace(
76
                    'Oprettelse Ok, nye tilmeldinger: ',
77
                    '',
78
                    $this->response->SletTilmeldingerV2Response
79
                );
80
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
81
            case 'HentNyeTilmeldingerV2':
82
                return $this->response->HentNyeTilmeldingerV2Result;
83
            break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
84
            default:
85
                throw new \Exception($this->responseType . ' is not a valid response type');
86
        }
87
    }
88
89
    /**
90
     * Checks whether the communication is OK
91
     *
92
     * @return boolean
93
     */
94
    public function isOk()
95
    {
96
        switch ($this->responseType) {
97
            case 'NyTilmelding2':
98
                $string = 'Oprettelse Ok, nye tilmeldinger';
99
                $result = strpos($this->response->NyTilmelding2Result, $string);
100
                return ($result !== false);
101
            case 'SletTilmeldingerV2':
102
                $string = 'Oprettelse Ok, nye tilmeldinger';
103
                $result = strpos($this->response->SletTilmeldingerV2Result, $string);
104
                return ($result !== false);
105
            case 'HentNyeTilmeldingerV2':
106
                $string = 'Oprettelse Ok, nye tilmeldinger';
107
                $result = strpos($this->response->HentNyeTilmeldingerV2Result, $string);
108
                return ($result !== false);
109
            default:
110
                throw new \Exception('Not a valid response type');
111
        }
112
    }
113
}
114