Completed
Push — 8.x ( 41ca52 )
by Tim
10:18
created

FileResolver::getPatternElements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\Configuration\Subject\FilesystemAdapter
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms\Configuration\Subject;
22
23
use JMS\Serializer\Annotation\Type;
24
use JMS\Serializer\Annotation\SerializedName;
25
use JMS\Serializer\Annotation\PostDeserialize;
26
use TechDivision\Import\Utils\BunchKeys;
27
use TechDivision\Import\Utils\DependencyInjectionKeys;
28
use TechDivision\Import\Configuration\Subject\FileResolverConfigurationInterface;
29
30
/**
31
 * The file resolver's configuration.
32
 *
33
 * @author    Tim Wagner <[email protected]>
34
 * @copyright 2016 TechDivision GmbH <[email protected]>
35
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
36
 * @link      https://github.com/techdivision/import-configuration-jms
37
 * @link      http://www.techdivision.com
38
 */
39
class FileResolver implements FileResolverConfigurationInterface
40
{
41
42
    /**
43
     * The file resolver's class name.
44
     *
45
     * @var string
46
     * @Type("string")
47
     */
48
    protected $id = DependencyInjectionKeys::IMPORT_SUBJECT_FILE_RESOLVER_SIMPLE;
49
50
    /**
51
     * The prefix/meta sequence of the import files.
52
     *
53
     * @var string
54
     * @Type("string")
55
     */
56
    protected $prefix = '.*';
57
58
    /**
59
     * The filename/meta sequence of the import files.
60
     *
61
     * @var string
62
     * @Type("string")
63
     */
64
    protected $filename = '.*';
65
66
    /**
67
     * The counter/meta sequence of the import files.
68
     *
69
     * @var string
70
     * @Type("string")
71
     */
72
    protected $counter = '\d+';
73
74
    /**
75
     * The file suffix for import files.
76
     *
77
     * @var string
78
     * @Type("string")
79
     */
80
    protected $suffix = 'csv';
81
82
    /**
83
     * The file suffix for OK file.
84
     *
85
     * @var string
86
     * @Type("string")
87
     * @SerializedName("ok-file-suffix")
88
     */
89
    protected $okFileSuffix = 'ok';
90
91
    /**
92
     * The separator char for the elements of the file.
93
     *
94
     * @var string
95
     * @Type("string")
96
     * @SerializedName("element-separator")
97
     */
98
    protected $elementSeparator = '_';
99
100
    /**
101
     * The elements to create the regex pattern that is necessary decide a subject handles a file or not.
102
     *
103
     * @var string
104
     * @Type("array")
105
     * @SerializedName("pattern-elements")
106
     */
107
    protected $patternElements = null;
108
109
    /**
110
     * Return's the file resolver's unique DI identifier.
111
     *
112
     * @return string The file resolvers's unique DI identifier
113
     */
114
    public function getId()
115
    {
116
        return $this->id;
117
    }
118
119
    /**
120
     * Return's the prefix/meta sequence for the import files.
121
     *
122
     * @return string The prefix
123
     */
124
    public function getPrefix()
125
    {
126
        return $this->prefix;
127
    }
128
129
    /**
130
     * Return's the filename/meta sequence of the import files.
131
     *
132
     * @return string The suffix
133
     */
134
    public function getFilename()
135
    {
136
        return $this->filename;
137
    }
138
139
    /**
140
     * Return's the counter/meta sequence of the import files.
141
     *
142
     * @return string The suffix
143
     */
144
    public function getCounter()
145
    {
146
        return $this->counter;
147
    }
148
149
    /**
150
     * Return's the suffix for the import files.
151
     *
152
     * @return string The suffix
153
     */
154
    public function getSuffix()
155
    {
156
        return $this->suffix;
157
    }
158
159
    /**
160
     * Return's the suffix for the OK file.
161
     *
162
     * @return string The OK file suffix
163
     */
164
    public function getOkFileSuffix()
165
    {
166
        return $this->okFileSuffix;
167
    }
168
169
    /**
170
     * Return's the delement separator char.
171
     *
172
     *  @return string The element separator char
173
     */
174
    public function getElementSeparator()
175
    {
176
        return $this->elementSeparator;
177
    }
178
179
    /**
180
     * Return's the elements the filenames consists of.
181
     *
182
     * @return array The array with the filename elements
183
     */
184
    public function getPatternElements()
185
    {
186
        return $this->patternElements;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->patternElements; (string) is incompatible with the return type declared by the interface TechDivision\Import\Conf...ace::getPatternElements of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
187
    }
188
189
    /**
190
     * Initializes the pattern elements with the available bunch keys.
191
     */
192
    public function __construct()
193
    {
194
        $this->patternElements = BunchKeys::getAllKeys();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TechDivision\Import\Utils\BunchKeys::getAllKeys() of type array is incompatible with the declared type string of property $patternElements.

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...
195
    }
196
197
    /**
198
     * Lifecycle callback that will be invoked after deserialization.
199
     *
200
     * @return void
201
     * @PostDeserialize
202
     */
203
    public function postDeserialize()
204
    {
205
206
        // set the default pattern elements
207
        if ($this->patternElements === null) {
208
            $this->patternElements = BunchKeys::getAllKeys();
0 ignored issues
show
Documentation Bug introduced by
It seems like \TechDivision\Import\Utils\BunchKeys::getAllKeys() of type array is incompatible with the declared type string of property $patternElements.

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...
209
        }
210
    }
211
}
212