ResponseFactory   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 58
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B build() 0 47 10
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