Completed
Push — master ( 91bb83...325361 )
by Nikola
05:28
created

ResourceAdapter::supports()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
cc 2
eloc 2
nc 2
nop 1
crap 6
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 Psr\Http\Message\StreamInterface;
13
use RunOpenCode\Sax\Contract\StreamAdapterInterface;
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\Stream\Stream is used by default.
33
     */
34
    public function __construct($streamClass = 'GuzzleHttp\\Stream\\Stream')
35
    {
36
        $this->streamClass = $streamClass;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function supports($xmlDocument)
43
    {
44
        return is_resource($xmlDocument) && get_resource_type($xmlDocument) === 'stream';
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function convert($xmlDocument)
51
    {
52
        /**
53
         * @var resource $xmlDocument
54
         */
55
        if (class_exists($this->streamClass)) {
56
            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