Completed
Pull Request — master (#10)
by Harry
06:44
created

ReFormat::modify()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 0
cts 8
cp 0
rs 8.8571
cc 5
eloc 8
nc 2
nop 2
crap 30
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\Modify;
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\Format\Parser\ParserFactory;
21
use Graze\DataFile\Format\Parser\ParserFactoryInterface;
22
use Graze\DataFile\Helper\Builder\BuilderAwareInterface;
23
use Graze\DataFile\Helper\Builder\BuilderInterface;
24
use Graze\DataFile\Helper\FileHelper;
25
use Graze\DataFile\Helper\GetOptionTrait;
26
use Graze\DataFile\Helper\OptionalLoggerTrait;
27
use Graze\DataFile\IO\FileReader;
28
use Graze\DataFile\IO\FileWriter;
29
use Graze\DataFile\Node\FileNodeInterface;
30
use Graze\DataFile\Node\LocalFileNodeInterface;
31
use InvalidArgumentException;
32
use Psr\Log\LoggerAwareInterface;
33
34
class ReFormat implements FileModifierInterface, LoggerAwareInterface, BuilderAwareInterface
35
{
36
    use OptionalLoggerTrait;
37
    use GetOptionTrait;
38
    use FileProcessTrait;
39
    use FileHelper;
40
41
    /** @var FormatterFactoryInterface */
42
    private $formatterFactory;
43
    /** @var ParserFactoryInterface */
44
    private $parserFactory;
45
46
    /**
47
     * ReFormat constructor.
48
     *
49
     * @param FormatterFactoryInterface|null $formatterFactory
50
     * @param ParserFactoryInterface|null    $parserFactory
51
     * @param BuilderInterface               $builder
0 ignored issues
show
Documentation introduced by
Should the type for parameter $builder not be null|BuilderInterface?

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...
52
     */
53 8
    public function __construct(
54
        FormatterFactoryInterface $formatterFactory = null,
55
        ParserFactoryInterface $parserFactory = null,
56
        BuilderInterface $builder = null
57
    ) {
58 8
        $this->builder = $builder;
59 8
        $this->formatterFactory = $formatterFactory ?: $this->getBuilder()->build(FormatterFactory::class);
60 8
        $this->parserFactory = $parserFactory ?: $this->getBuilder()->build(ParserFactory::class);
61 8
    }
62
63
    /**
64
     * Can this file be modified by this modifier
65
     *
66
     * @param FileNodeInterface $file
67
     *
68
     * @return bool
69
     */
70 2
    public function canModify(FileNodeInterface $file)
71
    {
72 2
        return ($file->exists()
73 2
            && $file instanceof FormatAwareInterface
74 2
            && $file->getFormat() !== null
75 2
            && $this->parserFactory->getParser($file->getFormat()) !== null);
76
    }
77
78
    /**
79
     * Modify the file
80
     *
81
     * @param FileNodeInterface $file
82
     * @param array             $options
83
     *
84
     * @return FileNodeInterface
85
     */
86
    public function modify(FileNodeInterface $file, array $options = [])
87
    {
88
        $this->options = $options;
89
90
        $format = $this->getOption('format', null);
91
        $output = $this->getOption('output', null);
92
        if ((is_null($format) && is_null($output)
93
            || ($format instanceof FormatInterface || $output instanceof LocalFileNodeInterface))
94
        ) {
95
            throw new InvalidArgumentException("Missing a Required option: 'format' or 'output'");
96
        }
97
98
        return $this->reFormat($file, $format);
99
    }
100
101
    /**
102
     * @param FileNodeInterface $file
103
     * @param FormatInterface   $outputFormat
0 ignored issues
show
Documentation introduced by
Should the type for parameter $outputFormat not be null|FormatInterface?

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...
Documentation introduced by
Should the type for parameter $output not be null|FileNodeInterface?

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...
104
     * @param FileNodeInterface $output
105
     * @param FormatInterface   $inputFormat
0 ignored issues
show
Documentation introduced by
Should the type for parameter $inputFormat not be null|FormatInterface?

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...
106
     *
107
     * @return FileNodeInterface
108
     */
109 5
    public function reFormat(
110
        FileNodeInterface $file,
111
        FormatInterface $outputFormat = null,
112
        FileNodeInterface $output = null,
113
        FormatInterface $inputFormat = null
114
    ) {
115 5
        if (is_null($output)) {
116 2
            if ($file instanceof LocalFileNodeInterface) {
117 1
                $output = $this->getTargetFile($file, $this->getOption('postfix', 'format'));
118
            } else {
119 1
                $output = $this->getTemporaryFile();
120
            }
121
        }
122
123 5
        if ($output instanceof FormatAwareInterface
124 5
            && $outputFormat != null
125
        ) {
126 2
            $output->setFormat($outputFormat);
127
        }
128
129
        /** @var FileReader $reader */
130 5
        $reader = $this->getBuilder()->build(FileReader::class, $file, $inputFormat, $this->parserFactory);
131
        /** @var FileWriter $writer */
132 5
        $writer = $this->getBuilder()->build(FileWriter::class, $output, $outputFormat, $this->formatterFactory);
133
134 5
        foreach ($reader->fetch() as $row) {
135 5
            $writer->insertOne($row);
136
        }
137
138 5
        return $output;
139
    }
140
}
141