Completed
Pull Request — master (#8)
by Harry
03:33
created

FileWriter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 8
c 2
b 0
f 1
lcom 1
cbo 4
dl 0
loc 78
ccs 26
cts 26
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 31 6
A insertAll() 0 5 1
A insertOne() 0 5 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 StreamReader */
36
    private $writer;
37
38
    /**
39
     * FileReader constructor.
40
     *
41
     * @param FileNodeInterface         $file
42
     * @param FormatInterface|null      $format
43
     * @param FormatterFactoryInterface $formatterFactory
0 ignored issues
show
Documentation introduced by
Should the type for parameter $formatterFactory not be null|FormatterFactoryInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
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 6
        } else {
56 1
            throw new InvalidArgumentException(
57 1
                "Only files that implement " . NodeStreamInterface::class . "can be written to"
58 1
            );
59
        }
60
61 6
        if (is_null($this->format)
62 6
            && $file instanceof FormatAwareInterface
63 6
        ) {
64 1
            $this->format = $file->getFormat();
65 1
        }
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);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Graze\DataFile\IO\S...er($stream, $formatter) of type object<Graze\DataFile\IO\StreamWriter> is incompatible with the declared type object<Graze\DataFile\IO\StreamReader> of property $writer.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method insertAll() does not seem to exist on object<Graze\DataFile\IO\StreamReader>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method insertOne() does not seem to exist on object<Graze\DataFile\IO\StreamReader>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
102 1
        return $this;
103
    }
104
}
105