Completed
Push — master ( 85851c...4b020b )
by Harry
03:30
created

FileWriter::insertAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
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\Formatter\FormatterFactory;
19
use Graze\DataFile\Format\Formatter\FormatterFactoryInterface;
20
use Graze\DataFile\Helper\OptionalLoggerTrait;
21
use Graze\DataFile\Node\FileNodeInterface;
22
use Graze\DataFile\Node\NodeStreamInterface;
23
use InvalidArgumentException;
24
use Psr\Log\LoggerAwareInterface;
25
use Traversable;
26
27
class FileWriter implements WriterInterface, LoggerAwareInterface
28
{
29
    use OptionalLoggerTrait;
30
31
    /** @var FileNodeInterface */
32
    private $file;
33
    /** @var FormatInterface */
34
    private $format;
35
    /** @var StreamWriter */
36
    private $writer;
37
38
    /**
39
     * FileReader constructor.
40
     *
41
     * @param FileNodeInterface              $file
42
     * @param FormatInterface|null           $format
43
     * @param FormatterFactoryInterface|null $formatterFactory
44
     */
45 7
    public function __construct(
46
        FileNodeInterface $file,
47
        FormatInterface $format = null,
48
        FormatterFactoryInterface $formatterFactory = null
49
    ) {
50 7
        $this->file = $file;
51 7
        $this->format = $format;
52
53 7
        if ($this->file instanceof NodeStreamInterface) {
54 6
            $stream = $this->file->getStream('a+');
55
        } else {
56 1
            throw new InvalidArgumentException(
57 1
                "Only files that implement " . NodeStreamInterface::class . "can be written to"
58
            );
59
        }
60
61 6
        if (is_null($this->format)
62 6
            && $file instanceof FormatAwareInterface
63
        ) {
64 1
            $this->format = $file->getFormat();
65
        }
66
67 6
        if (is_null($this->format)) {
68 1
            throw new InvalidArgumentException("No format could be determined from \$file or \$format");
69
        }
70
71 5
        $factory = $formatterFactory ?: new FormatterFactory();
72 5
        $formatter = $factory->getFormatter($this->format);
73
74 5
        $this->writer = new StreamWriter($stream, $formatter);
75 5
    }
76
77
    /**
78
     * Adds multiple items to the file
79
     *
80
     * @param Traversable|array $rows a multidimensional array or a Traversable object
81
     *
82
     * @throws InvalidArgumentException If the given rows format is invalid
83
     *
84
     * @return static
85
     */
86 1
    public function insertAll($rows)
87
    {
88 1
        $this->writer->insertAll($rows);
89 1
        return $this;
90
    }
91
92
    /**
93
     * Adds a single item
94
     *
95
     * @param mixed $row an item to insert
96
     *
97
     * @return static
98
     */
99 1
    public function insertOne($row)
100
    {
101 1
        $this->writer->insertOne($row);
102 1
        return $this;
103
    }
104
}
105