Completed
Push — master ( fa5569...0fb0f7 )
by Nikola
03:21
created

DomDocumentAdapter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4286
cc 1
eloc 6
nc 1
nop 2
crap 1
1
<?php
2
/*
3
 * This file is part of the runopencode/sax, an RunOpenCode project.
4
 *
5
 * (c) 2016 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace RunOpenCode\Sax\StreamAdapter;
11
12
use RunOpenCode\Sax\Contract\StreamAdapterInterface;
13
14
/**
15
 * Class DomAdapter
16
 *
17
 * DOMDocument to stream adapter.
18
 *
19
 * @package RunOpenCode\Sax\Contract
20
 */
21
class DomDocumentAdapter implements StreamAdapterInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $streamClass;
27
28
    /**
29
     * @var array
30
     */
31
    private $options;
32
33
    /**
34
     * DomDocumentAdapter constructor.
35
     *
36
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Psr7\Stream is used by default.
37
     * @param array $options Adapter options.
38
     */
39 8
    public function __construct($streamClass = 'GuzzleHttp\\Psr7\\Stream', array $options = array())
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...
40
    {
41 8
        $this->streamClass = $streamClass;
42 8
        $this->options = array_merge(array(
43 8
            'stream' => 'php://memory',
44
            'save_xml_options' => null
45 8
        ), $options);
46 8
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 8
    public function supports($xmlDocument)
52
    {
53 8
        return $xmlDocument instanceof \DOMDocument;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 2
    public function convert($xmlDocument)
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...
60
    {
61
        /**
62
         * @var \DOMDocument $xmlDocument
63
         */
64 2
        if (class_exists($this->streamClass)) {
65
66 2
            $stream = fopen($this->options['stream'],'r+');
67 2
            fwrite($stream, $xmlDocument->saveXML($this->options['save_xml_options']));
68 2
            rewind($stream);
69
70 2
            return new $this->streamClass($stream);
71
        }
72
73
        throw new \RuntimeException(sprintf('Provided StreamInterface implementation "%s" is not available on this system.', $this->streamClass));
74
    }
75
}
76