Completed
Pull Request — master (#12)
by Harry
05:25
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
    use OptionalLoggerTrait;
29
30
    /**
31
     * FileReader constructor.
32
     *
33
     * @param FileNodeInterface           $file
34
     * @param FormatInterface|null        $format
35
     * @param ParserFactoryInterface|null $parserFactory
36
     */
37 7
    public function __construct(
38
        FileNodeInterface $file,
39
        FormatInterface $format = null,
40
        ParserFactoryInterface $parserFactory = null
41
    ) {
42 7
        if ($file instanceof NodeStreamInterface) {
43 6
            $stream = $file->getStream('r');
44 6
        } else {
45 1
            $stream = new Stream($file->readStream());
46
        }
47
48 7
        if (is_null($format)
49 7
            && $file instanceof FormatAwareInterface
50 7
        ) {
51 2
            $format = $file->getFormat();
52 2
        }
53
54 7
        if (is_null($format)) {
55 1
            throw new InvalidArgumentException("No format could be determined from \$file or \$format");
56
        }
57
58 6
        $factory = $parserFactory ?: new ParserFactory();
59 6
        $parser = $factory->getParser($format);
60
61 6
        parent::__construct($stream, $parser);
62 6
    }
63
}
64