Passed
Branch 2.0.0 (174594)
by Chubarov
03:48
created

XmlHandler::toArrayCollect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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 'application/atom+xml;type=entry';
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 2
    public function toArray()
129
    {
130 2
       return  $this->xmlToArrayRecursive($this->validText);
131
    }
132
133
    /**
134
     * @return \Illuminate\Support\Collection
135
     */
136 1
    public function toArrayCollect()
137
    {
138 1
        return  collect($this->xmlToArrayRecursive($this->validText));
139
    }
140
141
    /**
142
     * @return string
143
     */
144 1
    public function toJson()
145
    {
146 1
        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 1
    private function workspace()
156
    {
157 1
        foreach ($this->response->workspace->collection as $item) {
158 1
            $this->validText[] = get_object_vars($item->children(  $this->namespaces['NamespaceAtom'] ))['title'];
159 1
        }
160 1
        return $this;
161
    }
162
163
    /**
164
     * Extraction array in response XML , more element one
165
     * @return XmlHandler
166
     * @throws \Exception
167
     */
168 3
    private function arrayMany()
169
    {
170
        try {
171 3
            foreach ($this->response->children( $this->namespaces['NamespaceAtom'] )->entry as $item ) {
172 2
                $this->validText[] =   $item->content->children( $this->namespaces['NamespaceMetadata'] )
173 2
                    ->children($this->namespaces['NamespaceDataServices']);
174 3
            }
175 3
            return $this;
176
        } catch (\Exception $e) {
177
            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...
178
        }
179
    }
180
    /**
181
     *  Get one Element
182
     * @return mixed
183
     */
184 1
    private function arrayOne()
185
    {
186 1
        $this->validText = $this->response->children( $this->namespaces['NamespaceAtom'] )->content
187 1
            ->children( $this->namespaces['NamespaceMetadata'] )
188 1
            ->children( $this->namespaces['NamespaceDataServices'] );
189 1
        return $this;
190
    }
191
192
193
    /**
194
     * Xml text for request in Bpm Online
195
     * @param $data array
196
     * @return string
197
     */
198 1
    public function create(array $data)
199
    {
200
201
        //----------  Base  ----------//
202 1
        $dom          = new \DOMDocument('1.0', 'utf-8');
203 1
        $entry        = $dom->createElement('entry');
204 1
        $dom->appendChild($entry);
205
206
        //----------  NameSpaces  ----------//
207 1
        foreach ($this->listNamespaces as $key => $value) {
208
209 1
            $xmlBase  = $dom->createAttribute(key($value));
210 1
            $entry->appendChild($xmlBase);
211
212 1
            $value    = $dom->createTextNode($value[key($value)]);
213 1
            $xmlBase->appendChild($value);
214 1
        }
215
216
        //----------  <content type="application/xml">  ----------//
217 1
        $content      = $dom->createElement('content');
218 1
        $entry->appendChild($content);
219 1
        $xmlns_dcd    = $dom->createAttribute('type');
220 1
        $content->appendChild($xmlns_dcd);
221 1
        $valued       = $dom->createTextNode('application/xml');
222 1
        $xmlns_dcd->appendChild($valued);
223
224
        //----------  properties  ----------//
225 1
        $properties   = $dom->createElement('m:properties');
226 1
        $content->appendChild($properties);
227
228 1
        foreach ($data as $nameField => $valueField) {
229 1
            $element  = $dom->createElement($this->prefixNamespace.':'.$nameField);
230 1
            $properties->appendChild($element);
231 1
            $valued   = $dom->createTextNode($valueField);
232 1
            $element->appendChild($valued);
233 1
        }
234 1
        return $this->buildXml = $dom->saveXML();
235
    }
236
}