ResponseFactory::build()   B
last analyzed

Complexity

Conditions 10
Paths 28

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 7.2896
c 0
b 0
f 0
nc 28
cc 10
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * This file is part of the php-epp2 library.
5
 *
6
 * (c) Gunter Grodotzki <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace AfriCC\EPP\Frame;
13
14
use AfriCC\EPP\Frame\Response\MessageQueue;
15
use AfriCC\EPP\ObjectSpec;
16
use DOMDocument;
17
use DOMXPath;
18
19
class ResponseFactory
20
{
21
    /**
22
     * Build response frame
23
     *
24
     * @param string $buffer
25
     * @param ObjectSpec $objectSpec
26
     *
27
     * @return string|\AfriCC\EPP\Frame\Response\MessageQueue|\AfriCC\EPP\Frame\Response
28
     */
29
    public static function build($buffer, ObjectSpec $objectSpec = null)
30
    {
31
        $xml = new DOMDocument('1.0', 'UTF-8');
32
        $xml->formatOutput = true;
33
        $xml->registerNodeClass('\DOMElement', '\AfriCC\EPP\DOM\DOMElement');
34
        $xml->loadXML($buffer);
35
36
        $xpath = new DOMXPath($xml);
37
38
        if (is_null($objectSpec)) {
39
            $objectSpec = new ObjectSpec();
40
        }
41
42
        foreach ($objectSpec->specs as $prefix => $spec) {
43
            $xpath->registerNamespace($prefix, $spec['xmlns']);
44
        }
45
46
        $nodes = $xml->getElementsByTagNameNS($objectSpec->xmlns('epp'), 'epp');
47
        if ($nodes === null || $nodes->length !== 1) {
48
            return $buffer;
49
        }
50
51
        if (!$nodes->item(0)->hasChildNodes()) {
52
            return $buffer;
53
        }
54
55
        foreach ($nodes->item(0)->childNodes as $node) {
56
            // ignore non-nodes
57
            if ($node->nodeType !== XML_ELEMENT_NODE) {
58
                continue;
59
            }
60
61
            // ok now we can create an object according to the response-frame
62
            $frame_type = strtolower($node->localName);
63
            if ($frame_type === 'response') {
64
                // check if it is a message queue @todo this should go into the response object!
65
                $results = $xpath->query('//epp:epp/epp:response/epp:msgQ');
66
                if ($results->length > 0) {
67
                    return new MessageQueue($xml, $objectSpec);
68
                }
69
70
                return new Response($xml, $objectSpec);
71
            }
72
        }
73
74
        return $buffer;
75
    }
76
}
77