Completed
Push — master ( 2c928e...ac95bd )
by Nikola
04:13
created

DomDocumentAdapter::convert()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 16
loc 16
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 3
1
<?php
2
/*
3
 * This file is part of the runopencode/sax, an RunOpenCode project.
4
 *
5
 * (c) 2017 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
use RunOpenCode\Sax\Exception\StreamAdapterException;
14
15
/**
16
 * Class DomAdapter
17
 *
18
 * DOMDocument to stream adapter.
19
 *
20
 * @package RunOpenCode\Sax\Contract
21
 */
22
class DomDocumentAdapter implements StreamAdapterInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $streamClass;
28
29
    /**
30
     * @var array
31
     */
32
    private $options;
33
34
    /**
35
     * DomDocumentAdapter constructor.
36
     *
37
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Psr7\Stream is used by default.
38
     * @param array $options Adapter options.
39
     */
40 7
    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...
41
    {
42 7
        $this->streamClass = $streamClass;
43 7
        $this->options = array_merge(array(
44 7
            'stream' => 'php://memory',
45
            'save_xml_options' => null,
46 7
        ), $options);
47 7
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 5
    public function supports($xmlDocument)
53
    {
54 5
        return $xmlDocument instanceof \DOMDocument;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3 View Code Duplication
    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...
61
    {
62 3
        $stream = @fopen($this->options['stream'], 'r+b');
63
64 3
        if (false === $stream) {
65 1
            throw new StreamAdapterException(sprintf('Unable to acquire resource handler on "%s".', $this->options['stream']));
66
        }
67
68 2
        fwrite($stream, $xmlDocument->saveXML($this->options['save_xml_options']));
69
70 2
        if (false === @rewind($stream)) {
71 1
            throw new StreamAdapterException('Unable to to rewind stream.');
72
        }
73
74 1
        return new $this->streamClass($stream);
75
    }
76
}
77