FileReader::__construct()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 13
cts 13
cp 1
rs 8.439
c 0
b 0
f 0
cc 6
eloc 16
nc 12
nop 3
crap 6
1
<?php
2
/**
3
 * This file is part of graze/data-file
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/data-file/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/data-file
12
 */
13
14
namespace Graze\DataFile\IO;
15
16
use Graze\DataFile\Format\FormatAwareInterface;
17
use Graze\DataFile\Format\FormatInterface;
18
use Graze\DataFile\Format\Parser\ParserFactory;
19
use Graze\DataFile\Format\Parser\ParserFactoryInterface;
20
use Graze\DataFile\Node\FileNodeInterface;
21
use Graze\DataFile\Node\NodeStreamInterface;
22
use GuzzleHttp\Psr7\Stream;
23
use InvalidArgumentException;
24
25
class FileReader extends StreamReader
26
{
27
    /**
28
     * FileReader constructor.
29
     *
30
     * @param FileNodeInterface           $file
31
     * @param FormatInterface|null        $format
32
     * @param ParserFactoryInterface|null $parserFactory
33
     */
34 8
    public function __construct(
35
        FileNodeInterface $file,
36
        FormatInterface $format = null,
37
        ParserFactoryInterface $parserFactory = null
38
    ) {
39 8
        if ($file instanceof NodeStreamInterface) {
40 7
            $stream = $file->getStream('r');
41
        } else {
42 1
            $stream = new Stream($file->readStream());
43
        }
44
45 8
        if (is_null($format)
46 8
            && $file instanceof FormatAwareInterface
47
        ) {
48 2
            $format = $file->getFormat();
49
        }
50
51 8
        if (is_null($format)) {
52 1
            throw new InvalidArgumentException("No format could be determined from \$file or \$format");
53
        }
54
55 7
        $factory = $parserFactory ?: new ParserFactory();
56 7
        $parser = $factory->getParser($format);
57
58 7
        parent::__construct($stream, $parser);
59 7
    }
60
}
61