XmlHandler::getError()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
1
<?php
2
namespace agoalofalife\bpm\Handlers;
3
4
use agoalofalife\bpm\Contracts\Collection;
5
use agoalofalife\bpm\Contracts\Handler;
6
7
/**
8
 * Class XmlHandler
9
 * @property string buildXml
10
 * @property string response
11
 * @property array validText
12
 * @package agoalofalife\bpm\Handlers
13
 */
14
class XmlHandler implements Handler, Collection
15
{
16
    use XmlConverter;
17
18
    private $response;
19
20
    private $validText = [];
21
22
    private $buildXml;
23
24
    /*
25
    |--------------------------------------------------------------------------
26
    | Namespaces XML API BPM
27
    |--------------------------------------------------------------------------
28
    | Namespaces in BPM API to parse the XML response
29
    |
30
    */
31
    private $namespaces = [
32
        'NamespaceAtom'         => 'http://www.w3.org/2005/Atom',
33
        'NamespaceMetadata'     => 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata',
34
        'NamespaceDataServices' => 'http://schemas.microsoft.com/ado/2007/08/dataservices',
35
    ];
36
37
    /*
38
    |--------------------------------------------------------------------------
39
    | List Namespaces for post request in BPM
40
    |--------------------------------------------------------------------------
41
    |   Namespaces To specify a file in XML
42
    |
43
    */
44
    private $listNamespaces = [
45
            ['xml:base'        => 'http://softex-iis:7503/0/ServiceModel/EntityDataService.svc/'],
46
            ['xmlns'           => 'http://www.w3.org/2005/Atom'],
47
            ['xmlns:d'         => 'http://schemas.microsoft.com/ado/2007/08/dataservices'],
48
            ['xmlns:m'         => 'http://schemas.microsoft.com/ado/2007/08/dataservices/metadata'],
49
            ['xmlns:georss'    => 'http://www.georss.org/georss'],
50
            ['xmlns:gml'       => 'http://www.opengis.net/gml'],
51
    ];
52
53
    /*
54
    |--------------------------------------------------------------------------
55
    |   Prefix XML document
56
    |--------------------------------------------------------------------------
57
    |   The prefix for insertion into Namespace XML document to be sent to API BPM
58
    |
59
    */
60
    private $prefixNamespace = 'd';
61
62
    /**
63
     * @return string
64
     */
65 2
    public function getAccept()
66
    {
67 2
        return 'application/atom+xml;type=entry';
68
    }
69
70
    /**
71
     * @return string
72
     */
73 2
    public function getContentType()
74
    {
75 2
        return '';
76
    }
77
78
    /**
79
     * @param $response
80
     * @return XmlHandler|array|mixed
81
     */
82 6
    public function parse($response)
83
    {
84 6
        $this->response      = simplexml_load_string($response);
85 6
        $copyXml             = $this->response;
86
87 6
        if ( $this->response === false || $this->checkIntegrity($this->response) === false )
88 6
        {
89 1
            return [];
90
        }
91
92 5
            $array_vars_list    = get_object_vars($copyXml);
93
94 5
            if (key_exists('content', $array_vars_list)) {
95 1
                return $this->arrayOne();
96
            }
97 4
            if (key_exists('workspace', $array_vars_list)) {
98 1
                return  $this->workspace();
99
            } else {
100 3
                return $this->arrayMany();
101
            }
102
    }
103
104
    /**
105
     * @param $response
106
     * @return bool
107
     */
108 8
    public function checkIntegrity($response)
109
    {
110 8
        if ( empty($response->message) )
111 8
        {
112 6
            return true;
113
        }
114 2
        return false;
115
    }
116
117
    /**
118
     * @return array
119
     */
120 1
    public function getData()
121
    {
122 1
        return $this->validText;
123
    }
124
    
125
     /**
126
     * @return array|null
127
     */
128
    public function getError()
129
    {
130
        if (isset($this->response->innererror)) {
131
            return (array)$this->response->innererror;
132
        }
133
134
        return null;
135
    }
136
137
    /**
138
     * @return array|null
139
     */
140 2
    public function toArray()
141
    {
142 2
       return  $this->xmlToArrayRecursive($this->validText);
143
    }
144
145
    /**
146
     * @return \Illuminate\Support\Collection
147
     */
148 1
    public function toArrayCollect()
149
    {
150 1
        return  collect($this->xmlToArrayRecursive($this->validText));
151
    }
152
153
    /**
154
     * @return string
155
     */
156 1
    public function toJson()
157
    {
158 1
        return  json_encode($this->xmlToArrayRecursive($this->validText));
159
    }
160
161
    /**
162
     * Return All Collection Bpm
163
     * if not specified all parameters in url
164
     * return list all collection from bpm
165
     * @throws \Exception
166
     */
167 1
    private function workspace()
168
    {
169 1
        foreach ($this->response->workspace->collection as $item) {
170 1
            $this->validText[] = get_object_vars($item->children(  $this->namespaces['NamespaceAtom'] ))['title'];
171 1
        }
172 1
        return $this;
173
    }
174
175
    /**
176
     * Extraction array in response XML , more element one
177
     * @return XmlHandler
178
     * @throws \Exception
179
     */
180 3 View Code Duplication
    private function arrayMany()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
181
    {
182 3
            foreach ($this->response->children( $this->namespaces['NamespaceAtom'] )->entry as $item ) {
183 2
                $this->validText[] =   $item->content->children( $this->namespaces['NamespaceMetadata'] )
184 2
                    ->children($this->namespaces['NamespaceDataServices']);
185 3
            }
186 3
            return $this;
187
    }
188
    /**
189
     *  Get one Element
190
     * @return mixed
191
     */
192 1 View Code Duplication
    private function arrayOne()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
193
    {
194 1
        $this->validText = $this->response->children( $this->namespaces['NamespaceAtom'] )->content
195 1
            ->children( $this->namespaces['NamespaceMetadata'] )
196 1
            ->children( $this->namespaces['NamespaceDataServices'] );
197 1
        return $this;
198
    }
199
200
201
    /**
202
     * Xml text for request in Bpm Online
203
     * @param $data array
204
     * @return string
205
     */
206 1
    public function create(array $data)
207
    {
208
209
        //----------  Base  ----------//
210 1
        $dom          = new \DOMDocument('1.0', 'utf-8');
211 1
        $entry        = $dom->createElement('entry');
212 1
        $dom->appendChild($entry);
213
214
        //----------  NameSpaces  ----------//
215 1
        foreach ($this->listNamespaces as $key => $value) {
216
217 1
            $xmlBase  = $dom->createAttribute(key($value));
218 1
            $entry->appendChild($xmlBase);
219
220 1
            $value    = $dom->createTextNode($value[key($value)]);
221 1
            $xmlBase->appendChild($value);
222 1
        }
223
224
        //----------  <content type="application/xml">  ----------//
225 1
        $content      = $dom->createElement('content');
226 1
        $entry->appendChild($content);
227 1
        $xmlns_dcd    = $dom->createAttribute('type');
228 1
        $content->appendChild($xmlns_dcd);
229 1
        $valued       = $dom->createTextNode('application/xml');
230 1
        $xmlns_dcd->appendChild($valued);
231
232
        //----------  properties  ----------//
233 1
        $properties   = $dom->createElement('m:properties');
234 1
        $content->appendChild($properties);
235
236 1
        foreach ($data as $nameField => $valueField) {
237 1
            $element  = $dom->createElement($this->prefixNamespace.':'.$nameField);
238 1
            $properties->appendChild($element);
239 1
            $valued   = $dom->createTextNode($valueField);
240 1
            $element->appendChild($valued);
241 1
        }
242 1
        return $this->buildXml = $dom->saveXML();
243
    }
244
}
245