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

SimpleXmlAdapter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 92.86%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 54
ccs 13
cts 14
cp 0.9286
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A supports() 0 4 1
A convert() 0 16 2
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
class SimpleXmlAdapter implements StreamAdapterInterface
15
{
16
    /**
17
     * @var string
18
     */
19
    private $streamClass;
20
21
    /**
22
     * @var array
23
     */
24
    private $options;
25
26
    /**
27
     * SimpleXmlAdapter constructor.
28
     *
29
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Psr7\Stream is used by default.
30
     * @param array $options Adapter options.
31
     */
32 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...
33
    {
34 8
        $this->streamClass = $streamClass;
35 8
        $this->options = array_merge(array(
36
            'stream' => 'php://memory'
37 8
        ), $options);
38 8
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 8
    public function supports($xmlDocument)
44
    {
45 8
        return $xmlDocument instanceof \SimpleXMLElement;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    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...
52
    {
53
        /**
54
         * @var \SimpleXMLElement $xmlDocument
55
         */
56 6
        if (class_exists($this->streamClass)) {
57
58 6
            $stream = fopen($this->options['stream'],'r+');
59 6
            fwrite($stream, $xmlDocument->asXML());
60 6
            rewind($stream);
61
62 6
            return new $this->streamClass($stream);
63
        }
64
65
        throw new \RuntimeException(sprintf('Provided StreamInterface implementation "%s" is not available on this system.', $this->streamClass));
66
    }
67
}
68