Completed
Push — master ( dac95d...cf9e7d )
by Thomas
02:37
created

Xml::load()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 22
Code Lines 17

Duplication

Lines 15
Ratio 68.18 %

Code Coverage

Tests 17
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
nc 2
nop 3
dl 15
loc 22
ccs 17
cts 17
cp 1
crap 4
rs 8.9197
c 1
b 0
f 0
1
<?php
2
/**
3
 * Load a DOM document from a xml file or string
4
 *
5
 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
6
 * @copyright Copyright (c) 2009-2014 Bastian Feder, Thomas Weinert
7
 */
8
9
namespace FluentDOM\Loader {
10
11
  use FluentDOM\Document;
12
  use FluentDOM\DocumentFragment;
13
  use FluentDOM\Loadable;
14
15
  /**
16
   * Load a DOM document from a xml file or string
17
   */
18
  class Xml implements Loadable {
19
20
    use Supports\Libxml;
21
22
    const LIBXML_OPTIONS = 'libxml';
23
24
    /**
25
     * @return string[]
26
     */
27 12
    public function getSupported() {
28 12
      return array('xml', 'application/xml', 'text/xml');
29 5
    }
30
31
    /**
32
     * @see Loadable::load
33
     * @param string $source
34
     * @param string $contentType
35
     * @param array|\Traversable|Options $options
36
     * @return Document|Result|NULL
37
     */
38 6
    public function load($source, $contentType, $options = []) {
39 6
      if ($this->supports($contentType)) {
40 5
        return (new Libxml\Errors())->capture(
41 View Code Duplication
          function() use ($source, $contentType, $options) {
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...
42 5
            $document = new Document();
43 5
            $document->preserveWhiteSpace = FALSE;
44 5
            $settings = $this->getOptions($options);
45 5
            $settings->isAllowed($sourceType = $settings->getSourceType($source));
46
            switch ($sourceType) {
47 5
            case Options::IS_FILE :
48 2
              $document->load($source, $settings[Options::LIBXML_OPTIONS]);
49 2
              break;
50 3
            case Options::IS_STRING :
51 3
            default :
52 3
              $document->loadXML($source, $settings[Options::LIBXML_OPTIONS]);
53 3
            }
54 5
            return $document;
55
          }
56 5
        );
57
      }
58 1
      return NULL;
59
    }
60
61
    /**
62
     * @see LoadableFragment::loadFragment
63
     * @param string $source
64
     * @param string $contentType
65
     * @param array|\Traversable|Options $options
66
     * @return DocumentFragment|NULL
67
     */
68 4
    public function loadFragment($source, $contentType, $options = []) {
69 4
      if ($this->supports($contentType)) {
70 3
        return (new Libxml\Errors())->capture(
71 3
          function() use ($source, $contentType, $options) {
72 3
            $dom = new Document();
73 3
            $fragment = $dom->createDocumentFragment();
74 3
            $fragment->appendXml($source);
75 3
            return $fragment;
76
          }
77 3
        );
78
      }
79 1
      return NULL;
80
    }
81
  }
82
}