Passed
Branch master (e96466)
by Chubarov
03:06
created

XmlHandler   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 219
Duplicated Lines 6.85 %

Coupling/Cohesion

Components 2
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
dl 15
loc 219
ccs 70
cts 70
cp 1
rs 10
c 3
b 0
f 1
wmc 21
lcom 2
cbo 1

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getAccept() 0 4 1
A getContentType() 0 4 1
A checkIntegrity() 0 8 2
A getData() 0 4 1
A toArray() 0 4 1
A toJson() 0 4 1
B parse() 0 21 5
A toArrayCollect() 0 4 1
A workspace() 0 7 2
A arrayMany() 8 8 2
A arrayOne() 7 7 1
B create() 0 38 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 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 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...
169
    {
170 3
            foreach ($this->response->children( $this->namespaces['NamespaceAtom'] )->entry as $item ) {
171 2
                $this->validText[] =   $item->content->children( $this->namespaces['NamespaceMetadata'] )
172 2
                    ->children($this->namespaces['NamespaceDataServices']);
173 3
            }
174 3
            return $this;
175
    }
176
    /**
177
     *  Get one Element
178
     * @return mixed
179
     */
180 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...
181
    {
182 1
        $this->validText = $this->response->children( $this->namespaces['NamespaceAtom'] )->content
183 1
            ->children( $this->namespaces['NamespaceMetadata'] )
184 1
            ->children( $this->namespaces['NamespaceDataServices'] );
185 1
        return $this;
186
    }
187
188
189
    /**
190
     * Xml text for request in Bpm Online
191
     * @param $data array
192
     * @return string
193
     */
194 1
    public function create(array $data)
195
    {
196
197
        //----------  Base  ----------//
198 1
        $dom          = new \DOMDocument('1.0', 'utf-8');
199 1
        $entry        = $dom->createElement('entry');
200 1
        $dom->appendChild($entry);
201
202
        //----------  NameSpaces  ----------//
203 1
        foreach ($this->listNamespaces as $key => $value) {
204
205 1
            $xmlBase  = $dom->createAttribute(key($value));
206 1
            $entry->appendChild($xmlBase);
207
208 1
            $value    = $dom->createTextNode($value[key($value)]);
209 1
            $xmlBase->appendChild($value);
210 1
        }
211
212
        //----------  <content type="application/xml">  ----------//
213 1
        $content      = $dom->createElement('content');
214 1
        $entry->appendChild($content);
215 1
        $xmlns_dcd    = $dom->createAttribute('type');
216 1
        $content->appendChild($xmlns_dcd);
217 1
        $valued       = $dom->createTextNode('application/xml');
218 1
        $xmlns_dcd->appendChild($valued);
219
220
        //----------  properties  ----------//
221 1
        $properties   = $dom->createElement('m:properties');
222 1
        $content->appendChild($properties);
223
224 1
        foreach ($data as $nameField => $valueField) {
225 1
            $element  = $dom->createElement($this->prefixNamespace.':'.$nameField);
226 1
            $properties->appendChild($element);
227 1
            $valued   = $dom->createTextNode($valueField);
228 1
            $element->appendChild($valued);
229 1
        }
230 1
        return $this->buildXml = $dom->saveXML();
231
    }
232
}