Test Failed
Push — master ( aaab1b...c0baad )
by
unknown
57s
created

UnixFileMappingReader   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 114
rs 10
c 0
b 0
f 0
wmc 9

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B getMappings() 0 28 3
A rewind() 0 3 1
A key() 0 3 1
A next() 0 3 1
A valid() 0 3 1
A current() 0 3 1
1
<?php
2
/**
3
 * Copyright Mediact. All rights reserved.
4
 * https://www.Mediact.nl
5
 */
6
7
namespace Mediact\FileMapping;
8
9
use ArrayIterator;
10
use Iterator;
11
use SplFileObject;
12
13
class UnixFileMappingReader implements FileMappingReaderInterface
14
{
15
    /** @var array */
16
    private $mappingFilePaths;
17
18
    /** @var string */
19
    private $sourceDirectory;
20
21
    /** @var string */
22
    private $targetDirectory;
23
24
    /** @var Iterator|FileMappingInterface[] */
25
    private $mappings;
26
27
    /**
28
     * Constructor.
29
     *
30
     * @param string   $sourceDirectory
31
     * @param string   $targetDirectory
32
     * @param string[] ...$mappingFilePaths
33
     */
34
    public function __construct(
35
        string $sourceDirectory,
36
        string $targetDirectory,
37
        string ...$mappingFilePaths
38
    ) {
39
        $this->sourceDirectory  = $sourceDirectory;
40
        $this->targetDirectory  = $targetDirectory;
41
        $this->mappingFilePaths = $mappingFilePaths;
42
    }
43
44
    /**
45
     * Get the mappings.
46
     *
47
     * @return Iterator
48
     */
49
    private function getMappings(): Iterator
50
    {
51
        if ($this->mappings === null) {
52
            $filePaths = [];
53
54
            foreach ($this->mappingFilePaths as $mappingFilePath) {
55
                $newFilePaths = iterator_to_array(new SplFileObject($mappingFilePath, 'r'));
56
                $filePaths    = array_merge($filePaths, $newFilePaths);
57
            }
58
59
            $this->mappings = new ArrayIterator(
60
                array_map(
61
                    function (string $mapping) : FileMappingInterface {
62
                        return new UnixFileMapping(
63
                            $this->sourceDirectory,
64
                            $this->targetDirectory,
65
                            trim($mapping)
66
                        );
67
                    },
68
                    // Filter out empty lines.
69
                    array_filter(
70
                        $filePaths
71
                    )
72
                )
73
            );
74
        }
75
76
        return $this->mappings;
77
    }
78
79
    /**
80
     * Move forward to next element.
81
     *
82
     * @return void
83
     */
84
    public function next()
85
    {
86
        $this->getMappings()->next();
87
    }
88
89
    /**
90
     * Return the key of the current element.
91
     *
92
     * @return int
93
     */
94
    public function key(): int
95
    {
96
        return $this->getMappings()->key();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMappings()->key() could return the type string|boolean|double which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
97
    }
98
99
    /**
100
     * Checks if current position is valid.
101
     *
102
     * @return bool
103
     */
104
    public function valid(): bool
105
    {
106
        return $this->getMappings()->valid();
107
    }
108
109
    /**
110
     * Rewind the Iterator to the first element.
111
     *
112
     * @return void
113
     */
114
    public function rewind()
115
    {
116
        $this->getMappings()->rewind();
117
    }
118
119
    /**
120
     * Get the current file mapping.
121
     *
122
     * @return FileMappingInterface
123
     */
124
    public function current(): FileMappingInterface
125
    {
126
        return $this->getMappings()->current();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMappings()->current() returns the type mixed which includes types incompatible with the type-hinted return Mediact\FileMapping\FileMappingInterface.
Loading history...
127
    }
128
}
129