ImporterPrecondition   A
last analyzed

Complexity

Total Complexity 36

Size/Duplication

Total Lines 187
Duplicated Lines 19.25 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.24%

Importance

Changes 0
Metric Value
wmc 36
lcom 1
cbo 4
dl 36
loc 187
ccs 69
cts 74
cp 0.9324
rs 9.52
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A filename() 0 6 1
A format() 0 6 1
A fieldcount() 0 6 1
A field() 0 6 1
A fieldset() 0 6 1
B isSatisfiedBy() 0 28 8
A isSatisfiedFilename() 18 18 5
A isSatisfiedFormat() 18 18 5
A isSatisfiedFieldcount() 0 16 4
A isSatisfiedAnyFields() 0 21 5
A isSatisfiedFieldset() 0 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mathielen\ImportEngine\Importer;
4
5
use Mathielen\ImportEngine\Storage\StorageInterface;
6
use Mathielen\ImportEngine\Storage\Format\Format;
7
use Mathielen\ImportEngine\Exception\InvalidConfigurationException;
8
use Mathielen\ImportEngine\Storage\StorageFormatInterface;
9
use Psr\Log\LoggerInterface;
10
11
class ImporterPrecondition
12
{
13
    private $filenames = array();
14
    private $formats = array();
15
    private $fieldcount = null;
16
    private $anyfields = array();
17
    private $fieldset = null;
18
19
    /**
20
     * @return ImporterPrecondition
21
     */
22 3
    public function filename($pattern)
23
    {
24 3
        $this->filenames[] = $pattern;
25
26 3
        return $this;
27
    }
28
29
    /**
30
     * @return ImporterPrecondition
31
     */
32 3
    public function format($id)
33
    {
34 3
        $this->formats[] = $id;
35
36 3
        return $this;
37
    }
38
39
    /**
40
     * Fieldset must have this number of fields.
41
     *
42
     * @return ImporterPrecondition
43
     */
44 1
    public function fieldcount($count)
45
    {
46 1
        $this->fieldcount = $count;
47
48 1
        return $this;
49
    }
50
51
    /**
52
     * Fieldset must have field with this name, anywhere in fieldset.
53
     *
54
     * @return ImporterPrecondition
55
     */
56 2
    public function field($fieldname)
57
    {
58 2
        $this->anyfields[] = strtolower($fieldname);
59
60 2
        return $this;
61
    }
62
63
    /**
64
     * Add required fields, must exist in the given order.
65
     *
66
     * @return ImporterPrecondition
67
     */
68 2
    public function fieldset(array $fieldset)
69
    {
70 2
        $this->fieldset = array_map('strtolower', $fieldset);
71
72 2
        return $this;
73
    }
74
75 11
    public function isSatisfiedBy(StorageInterface $storage, LoggerInterface $logger = null)
76
    {
77 11
        if (!($storage instanceof StorageFormatInterface) && !empty($this->formats)) {
78 1
            throw new InvalidConfigurationException('Cannot check format when storage does not implement StorageFormatInterface');
79
        }
80
81 10
        if (!$this->isSatisfiedFilename($storage->info()['name'], $logger)) {
82 1
            return false;
83
        }
84
85 9
        if (!$this->isSatisfiedFormat($storage->info()['format'], $logger)) {
86 1
            return false;
87
        }
88
89 8
        if (!$this->isSatisfiedFieldcount(count($storage->getFields()), $logger)) {
90 1
            return false;
91
        }
92
93 8
        if (!$this->isSatisfiedAnyFields($storage->getFields(), $logger)) {
94 1
            return false;
95
        }
96
97 7
        if (!$this->isSatisfiedFieldset($storage->getFields(), $logger)) {
98 1
            return false;
99
        }
100
101 6
        return true;
102
    }
103
104 10 View Code Duplication
    private function isSatisfiedFilename($filename, LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106 10
        if (empty($this->filenames)) {
107 7
            return true;
108
        }
109
110 3
        foreach ($this->filenames as $pattern) {
111 3
            if (preg_match("/$pattern/i", $filename)) {
112 3
                return true;
113
            }
114
        }
115
116 1
        if ($logger) {
117
            $logger->debug("Storage does not meet Preconditions due to filename restriction. Was $filename, should be one of ".implode(',', $this->filenames));
118
        }
119
120 1
        return false;
121
    }
122
123 9 View Code Duplication
    private function isSatisfiedFormat(Format $format, LoggerInterface $logger = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125 9
        if (empty($this->formats)) {
126 7
            return true;
127
        }
128
129 2
        foreach ($this->formats as $formatId) {
130 2
            if ($formatId == $format->getId()) {
131 2
                return true;
132
            }
133
        }
134
135 1
        if ($logger) {
136
            $logger->debug("Storage does not meet Preconditions due to format restriction. Was $format, should be one of ".implode(',', $this->formats));
137
        }
138
139 1
        return false;
140
    }
141
142 8
    private function isSatisfiedFieldcount($fieldCount, LoggerInterface $logger = null)
143
    {
144 8
        if (is_null($this->fieldcount)) {
145 7
            return true;
146
        }
147
148 1
        if ($this->fieldcount == $fieldCount) {
149 1
            return true;
150
        }
151
152 1
        if ($logger) {
153
            $logger->debug("Storage does not meet Preconditions due to fieldcount restriction. Was $fieldCount, should be ".$this->fieldcount);
154
        }
155
156 1
        return false;
157
    }
158
159 8
    private function isSatisfiedAnyFields(array $fields, LoggerInterface $logger = null)
160
    {
161 8
        if (empty($this->anyfields)) {
162 6
            return true;
163
        }
164
165 2
        $fields = array_map('strtolower', $fields);
166 2
        $fields = array_map('trim', $fields);
167
168 2
        foreach ($this->anyfields as $anyField) {
169 2
            if (!in_array($anyField, $fields)) {
170 1
                if ($logger) {
171
                    $logger->debug("Storage does not meet Preconditions due to fields restriction. Missing field: '$anyField'");
172
                }
173
174 2
                return false;
175
            }
176
        }
177
178 1
        return true;
179
    }
180
181 7
    private function isSatisfiedFieldset(array $fieldset, LoggerInterface $logger = null)
182
    {
183 7
        if (empty($this->fieldset)) {
184 5
            return true;
185
        }
186
187 2
        if (array_map('strtolower', $fieldset) == $this->fieldset) {
188 1
            return true;
189
        }
190
191 1
        if ($logger) {
192
            $logger->debug('Storage does not meet Preconditions due to fieldset restriction. Was '.implode(',', $fieldset).', should be '.implode(',', $this->fieldset));
193
        }
194
195 1
        return false;
196
    }
197
}
198