Completed
Branch 2.0.0 (a6ce5f)
by Chubarov
06:30 queued 03:11
created

XmlHandler::parse()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 12
nc 4
nop 1
dl 0
loc 21
rs 8.7624
c 1
b 0
f 1
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
    public function getAccept()
66
    {
67
        return '';
68
    }
69
70
    /**
71
     * @return string
72
     */
73
    public function getContentType()
74
    {
75
        return 'application/atom+xml;type=entry';
76
    }
77
78
    /**
79
     * @param $response
80
     * @return XmlHandler|array|mixed
81
     */
82
    public function parse($response)
83
    {
84
        $this->response      = simplexml_load_string($response);
85
        $copyXml             = $this->response;
86
87
        if ( $this->response === false || $this->checkIntegrity($this->response) === false )
88
        {
89
            return [];
90
        }
91
92
            $array_vars_list    = get_object_vars($copyXml);
93
94
            if (key_exists('content', $array_vars_list)) {
95
                return $this->arrayOne();
96
            }
97
            if (key_exists('workspace', $array_vars_list)) {
98
                return  $this->workspace();
99
            } else {
100
                return $this->arrayMany();
101
            }
102
    }
103
104
    /**
105
     * @param $response
106
     * @return bool
107
     */
108
    public function checkIntegrity($response)
109
    {
110
        if ( empty($response->message) )
111
        {
112
            return true;
113
        }
114
        return false;
115
    }
116
117
    /**
118
     * @return array
119
     */
120
    public function getData()
121
    {
122
        return $this->validText;
123
    }
124
125
    /**
126
     * @return array|null
127
     */
128
    public function toArray()
129
    {
130
       return  $this->xmlToArrayRecursive($this->validText);
131
    }
132
133
    /**
134
     * @return \Illuminate\Support\Collection
135
     */
136
    public function toArrayCollect()
137
    {
138
        return  collect($this->xmlToArrayRecursive($this->validText));
139
    }
140
141
    /**
142
     * @return string
143
     */
144
    public function toJson()
145
    {
146
        return  json_encode($this->xmlToArrayRecursive($this->validText));
147
    }
148
149
    /**
150
     * Return All Collection Bpm
151
     * if not specified all parameters in url
152
     * return list all collection from bpm
153
     * @throws \Exception
154
     */
155
    private function workspace()
156
    {
157 View Code Duplication
        if ( !empty($this->response->message->collection->title) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
158
            throw new \Exception("responce BPM API : ".
159
                $this->response->innererror->message.". ENG :".  $this->response->message);
160
        }
161
        foreach ($this->response->workspace->collection as $item) {
162
            $this->validText[] = get_object_vars($item->children(  $this->namespaces['NamespaceAtom'] ))['title'];
163
        }
164
        return $this;
165
    }
166
167
    /**
168
     * Extraction array in response XML , more element one
169
     * @return XmlHandler
170
     * @throws \Exception
171
     */
172
    private function arrayMany()
173
    {
174
        try {
175
            foreach ($this->response->children( $this->namespaces['NamespaceAtom'] )->entry as $item ) {
176
                $this->validText[] =   $item->content->children( $this->namespaces['NamespaceMetadata'] )
177
                    ->children($this->namespaces['NamespaceDataServices']);
178
            }
179
            return $this;
180
        } catch (\Exception $e) {
181
            dd($this->responceXML);
0 ignored issues
show
Bug introduced by
The property responceXML does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
182
        }
183
    }
184
    /**
185
     *  Get one Element
186
     * @return mixed
187
     */
188
    private function arrayOne()
189
    {
190
        $this->validText = $this->response->children( $this->namespaces['NamespaceAtom'] )->content
191
            ->children( $this->namespaces['NamespaceMetadata'] )
192
            ->children( $this->namespaces['NamespaceDataServices'] );
193
        return $this;
194
    }
195
196
197
    /**
198
     * Xml text for request in Bpm Online
199
     * @param $data array
200
     * @return string
201
     */
202
    public function create(array $data)
203
    {
204
205
        //----------  Base  ----------//
206
        $dom          = new \DOMDocument('1.0', 'utf-8');
207
        $entry        = $dom->createElement('entry');
208
        $dom->appendChild($entry);
209
210
        //----------  NameSpaces  ----------//
211 View Code Duplication
        foreach ($this->listNamespaces as $key => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
212
213
            $xmlBase  = $dom->createAttribute(key($value));
214
            $entry->appendChild($xmlBase);
215
216
            $value    = $dom->createTextNode($value[key($value)]);
217
            $xmlBase->appendChild($value);
218
        }
219
220
        //----------  <content type="application/xml">  ----------//
221
        $content      = $dom->createElement('content');
222
        $entry->appendChild($content);
223
        $xmlns_dcd    = $dom->createAttribute('type');
224
        $content->appendChild($xmlns_dcd);
225
        $valued       = $dom->createTextNode('application/xml');
226
        $xmlns_dcd->appendChild($valued);
227
228
        //----------  properties  ----------//
229
        $properties   = $dom->createElement('m:properties');
230
        $content->appendChild($properties);
231
232 View Code Duplication
        foreach ($data as $nameField => $valueField) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
233
            $element  = $dom->createElement($this->prefixNamespace.':'.$nameField);
234
            $properties->appendChild($element);
235
            $valued   = $dom->createTextNode($valueField);
236
            $element->appendChild($valued);
237
        }
238
        return $this->buildXml = $dom->saveXML();
239
    }
240
}