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

ResourceAdapter::convert()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 3
cts 4
cp 0.75
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2.0625
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 ResourceAdapter
16
 *
17
 * PHP resource to stream adapter.
18
 *
19
 * @package RunOpenCode\Sax\Contract
20
 */
21
class ResourceAdapter implements StreamAdapterInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $streamClass;
27
28
    /**
29
     * ResourceAdapter constructor.
30
     *
31
     * @param string $streamClass FQCN of StreamInterface implementation, GuzzleHttp\Psr7\Stream is used by default.
32
     */
33 8
    public function __construct($streamClass = 'GuzzleHttp\\Psr7\\Stream')
34
    {
35 8
        $this->streamClass = $streamClass;
36 8
    }
37
38
    /**
39
     * {@inheritdoc}
40
     */
41 8
    public function supports($xmlDocument)
42
    {
43 8
        return is_resource($xmlDocument) && get_resource_type($xmlDocument) === 'stream';
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 2
    public function convert($xmlDocument)
50
    {
51
        /**
52
         * @var resource $xmlDocument
53
         */
54 2
        if (class_exists($this->streamClass)) {
55 2
            return new $this->streamClass($xmlDocument);
56
        }
57
58
        throw new \RuntimeException(sprintf('Provided StreamInterface implementation "%s" is not available on this system.', $this->streamClass));
59
    }
60
}
61