SapphireSoapServer   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 8
c 3
b 1
f 0
lcom 2
cbo 6
dl 0
loc 97
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A wsdl() 0 6 1
A getWSDLURL() 0 4 1
B Methods() 0 24 3
A TargetNamespace() 0 4 1
A ServiceURL() 0 4 1
A index() 0 14 1
1
<?php
2
/**
3
 * Soap server class which auto-generates a WSDL
4
 * file to initialize PHPs integrated {@link SoapServer} class.
5
 *
6
 * See {@link SOAPModelAccess} for an auto-generated SOAP API for your models.
7
 *
8
 * @todo Improve documentation
9
 */
10
class SapphireSoapServer extends Controller
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
11
{
12
    /**
13
     * @var array Map of method name to arguments.
14
     */
15
    private static $methods = array();
0 ignored issues
show
Unused Code introduced by
The property $methods is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
16
17
    /**
18
     * @var array
19
     */
20
    private static $xsd_types = array(
21
        'int' => 'xsd:int',
22
        'boolean' => 'xsd:boolean',
23
        'string' => 'xsd:string',
24
        'binary' => 'xsd:base64Binary',
25
    );
26
27
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
28
        'index',
29
        'wsdl',
30
    );
31
32
    public function wsdl()
33
    {
34
        $this->getResponse()->addHeader('Content-Type', 'text/xml; charset=utf-8');
35
36
        return $this->renderWith('SapphireSoapServer_wsdl');
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getWSDLURL()
43
    {
44
        return Director::absoluteBaseURLWithAuth().$this->Link().'wsdl';
45
    }
46
47
    /**
48
     * @return SS_List Collection of ArrayData elements describing
49
     *                 the method (keys: 'Name', 'Arguments', 'ReturnType')
50
     */
51
    public function Methods()
52
    {
53
        $methods = array();
54
55
        foreach ($this->stat('methods') as $methodName => $arguments) {
0 ignored issues
show
Bug introduced by
The expression $this->stat('methods') of type array|integer|double|string|boolean is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
56
            $returnType = $arguments['_returns'];
57
            unset($arguments['_returns']);
58
59
            $processedArguments = array();
60
            foreach ($arguments as $argument => $type) {
61
                $processedArguments[] = new ArrayData(array(
62
                    'Name' => $argument,
63
                    'Type' => self::$xsd_types[$type],
64
                ));
65
            }
66
            $methods[] = new ArrayData(array(
67
                'Name' => $methodName,
68
                'Arguments' => new ArrayList($processedArguments),
69
                'ReturnType' => self::$xsd_types[$returnType],
70
            ));
71
        }
72
73
        return new ArrayList($methods);
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function TargetNamespace()
80
    {
81
        return Director::absoluteBaseURL();
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function ServiceURL()
88
    {
89
        return Director::absoluteBaseURLWithAuth().$this->class.'/';
90
    }
91
92
    public function index()
93
    {
94
        $wsdl = $this->getViewer('wsdl')->process($this);
95
        $wsdlFile = TEMP_FOLDER.'/sapphire-wsdl-'.$this->class;
96
        $fh = fopen($wsdlFile, 'w');
97
        fwrite($fh, $wsdl);
98
        fclose($fh);
99
100
        $this->getResponse()->addHeader('Content-Type', 'text/xml; charset=utf-8');
101
102
        $s = new SoapServer($wsdlFile, array('cache_wsdl' => WSDL_CACHE_NONE));
103
        $s->setClass($this->class);
104
        $s->handle();
105
    }
106
}
107