Completed
Push — master ( 6cd933...10e391 )
by Harry
03:44
created

FileReader::fetch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Helper\OptionalLoggerTrait;
21
use Graze\DataFile\Node\FileNodeInterface;
22
use Graze\DataFile\Node\NodeStreamInterface;
23
use GuzzleHttp\Psr7\Stream;
24
use InvalidArgumentException;
25
26
class FileReader extends StreamReader
27
{
28
    /**
29
     * FileReader constructor.
30
     *
31
     * @param FileNodeInterface           $file
32
     * @param FormatInterface|null        $format
33
     * @param ParserFactoryInterface|null $parserFactory
34
     */
35 8
    public function __construct(
36
        FileNodeInterface $file,
37
        FormatInterface $format = null,
38
        ParserFactoryInterface $parserFactory = null
39
    ) {
40 8
        if ($file instanceof NodeStreamInterface) {
41 7
            $stream = $file->getStream('r');
42 7
        } else {
43 1
            $stream = new Stream($file->readStream());
44
        }
45
46 8
        if (is_null($format)
47 8
            && $file instanceof FormatAwareInterface
48 8
        ) {
49 2
            $format = $file->getFormat();
50 2
        }
51
52 8
        if (is_null($format)) {
53 1
            throw new InvalidArgumentException("No format could be determined from \$file or \$format");
54
        }
55
56 7
        $factory = $parserFactory ?: new ParserFactory();
57 7
        $parser = $factory->getParser($format);
58
59 7
        parent::__construct($stream, $parser);
60 7
    }
61
}
62