Test Setup Failed
Branch dev-master (287e12)
by Petr
04:25
created

Request::buildBasePayload()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 119.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace P2A\YourMembership\Core;
4
5
/**
6
 *
7
 */
8
class Request
9
{
10
11
    public static $API_VERSION = '2.25';
12
    public static $BASE_URL = 'https://api.yourmembership.com';
13
14
    public static $callID = 1;
15
16
    private $apiKey;
17
    private $saPasscode;
18
    private $version;
19
20
21
    function __construct(string $apiKey, string $saPasscode)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
22
    {
23
        $this->apiKey = $apiKey;
24
        $this->saPasscode = $saPasscode;
25
    }
26
27
    /**
28
     * Create the Base Envelope for an API call to YourMembership
29
     * @method buildBasePayload
30
     * @author PA
31
     * @date   2017-01-09
32
     * @return SimpleXMLElement  XML Envelope with necessary credential parameters
33
     */
34
    public function buildBasePayload() : \SimpleXMLElement
35
    {
36
        /*
37
            <YourMembership>
38
            <Version>2.25</Version>
39
            <ApiKey>3D638C5F-CCE2-4638-A2C1-355FA7BBC917</ApiKey>
40
            <CallID>001</CallID>
41
            <SaPasscode>************</SaPasscode>
42
            <Call Method="Sa.People.Profile.Get">
43
                <ID>8D88D43A-B15B-4041-BEA0-89B05B2D9540</ID>
44
            </Call>
45
            </YourMembership>
46
    */
47
48
        $xml = new \SimpleXMLElement('<YourMembership></YourMembership>');
49
        $xml->addChild('Version', self::$API_VERSION);
50
        $xml->addChild('ApiKey', $this->apiKey);
51
        $xml->addChild('CallID', self::$callID);
52
        $xml->addChild('SaPasscode', $this->saPasscode);
53
54
        return $xml;
55
    }
56
57
    /**
58
     * Generates the XML for a API method call within
59
     * @method createCallPayload
60
     * @author PA
61
     * @date   2017-01-09
62
     * @param  string            $method    YourMembership API Function Name
63
     * @param  array             $arguments Array of Arguments to be passed as part of the YourMembership "Call"
64
     * @return \SimpleXMLElement
65
     */
66
    public function createCallPayload(string $method, array $arguments) : \SimpleXMLElement
67
    {
68
69
        $call = new \SimpleXMLElement('<Call></Call>');
70
        $call->addAttribute('method',$method);
71
72
        foreach ($arguments as $key => $value) {
73
            $call->addChild($key,$value);
74
        }
75
76
        return $call;
77
    }
78
79
    public function buildXMLPayload(string $method, array $arguments) : \SimpleXMLElement
80
    {
81
        $xml = $this->buildBasePayload();
82
        $callPayload = $this->createCallPayload($method,$arguments);
83
84
        $this->sxmlAppend($xml,$callPayload);
85
86
        return $xml;
87
    }
88
    /**
89
     * Complete XML envelope
90
     * @method call
91
     * @author PA
92
     * @date   2017-01-09
93
     * @param  string     $method    [description]
94
     * @param  array      $arguments [description]
95
     * @return [type]                [description]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
96
     */
97
    public function call(string $method, array $arguments) {
98
99
        $xml = $this->buildXMLPayload($method, $arguments);
100
101
        self::$callId++; //Update the Call ID as they need to be unique per call
102
103
        return $xml;
104
    }
105
    /**
106
     * Deep Copy for SimpleXML
107
     * @method sxmlAppend
108
     * @author PA
109
     * @date   2017-01-09
110
     * @param  SimpleXMLElement $to
111
     * @param  SimpleXMLElement $from
112
     */
113
    private function sxmlAppend(\SimpleXMLElement $to, \SimpleXMLElement $from)  {
114
        $toDom = dom_import_simplexml($to);
115
        $fromDom = dom_import_simplexml($from);
116
        $toDom->appendChild($toDom->ownerDocument->importNode($fromDom, true));
117
    }
118
119
};
120