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

ResourceAdapter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
dl 0
loc 40
c 0
b 0
f 0
wmc 5
lcom 1
cbo 1
ccs 8
cts 9
cp 0.8889
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A supports() 0 4 2
A convert() 0 11 2
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
15
/**
16
 * Class ResourceAdapter
17
 *
18
 * PHP resource to stream adapter.
19
 *
20
 * @package RunOpenCode\Sax\Contract
21
 */
22
class ResourceAdapter implements StreamAdapterInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    private $streamClass;
28
29
    /**
30
     * ResourceAdapter constructor.
31
     *
32
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Psr7\Stream is used by default.
33
     */
34 4
    public function __construct($streamClass = 'GuzzleHttp\\Psr7\\Stream')
35
    {
36 4
        $this->streamClass = $streamClass;
37 4
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    public function supports($xmlDocument)
43
    {
44 4
        return is_resource($xmlDocument) && get_resource_type($xmlDocument) === 'stream';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 1
    public function convert($xmlDocument)
51
    {
52
        /**
53
         * @var resource $xmlDocument
54
         */
55 1
        if (class_exists($this->streamClass)) {
56 1
            return new $this->streamClass($xmlDocument);
57
        }
58
59
        throw new RuntimeException(sprintf('Provided StreamInterface implementation "%s" is not available on this system.', $this->streamClass));
60
    }
61
}
62