Completed
Push — master ( 58d0b6...f4d8f3 )
by Nikola
01:55
created

DomDocumentAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 63
Duplicated Lines 38.1 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 82.35%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 24
loc 63
ccs 14
cts 17
cp 0.8235
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A supports() 0 4 1
B convert() 24 24 4

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
/*
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\RuntimeException;
14
use RunOpenCode\Sax\Exception\StreamAdapterException;
15
16
/**
17
 * Class DomAdapter
18
 *
19
 * DOMDocument to stream adapter.
20
 *
21
 * @package RunOpenCode\Sax\Contract
22
 */
23
class DomDocumentAdapter implements StreamAdapterInterface
24
{
25
    /**
26
     * @var string
27
     */
28
    private $streamClass;
29
30
    /**
31
     * @var array
32
     */
33
    private $options;
34
35
    /**
36
     * DomDocumentAdapter constructor.
37
     *
38
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Psr7\Stream is used by default.
39
     * @param array $options Adapter options.
40
     */
41 4
    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...
42
    {
43 4
        $this->streamClass = $streamClass;
44 4
        $this->options = array_merge(array(
45 4
            'stream' => 'php://memory',
46
            'save_xml_options' => null
47
        ), $options);
48 4
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 4
    public function supports($xmlDocument)
54
    {
55 4
        return $xmlDocument instanceof \DOMDocument;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 1 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...
62
    {
63
        /**
64
         * @var \DOMDocument $xmlDocument
65
         */
66 1
        if (class_exists($this->streamClass)) {
67
68 1
            $stream = @fopen($this->options['stream'],'r+b');
69
70 1
            if (false === $stream) {
71
                throw new StreamAdapterException(sprintf('Unable to acquire resource handler on "%s".', $this->options['stream']));
72
            }
73
74 1
            fwrite($stream, $xmlDocument->saveXML($this->options['save_xml_options']));
75
76 1
            if (false === rewind($stream)) {
77
                throw new StreamAdapterException('Unable to to rewind stream.');
78
            }
79
80 1
            return new $this->streamClass($stream);
81
        }
82
83
        throw new RuntimeException(sprintf('Provided StreamInterface implementation "%s" is not available on this system.', $this->streamClass));
84
    }
85
}
86