Completed
Push — 15.x ( 2d1ea5...d23069 )
by Tim
01:41
created

Plugin::getFullOperationName()   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 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\Configuration\Plugin
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;
22
23
use JMS\Serializer\Annotation\Type;
24
use JMS\Serializer\Annotation\SerializedName;
25
use JMS\Serializer\Annotation\PostDeserialize;
26
use Doctrine\Common\Collections\ArrayCollection;
27
use TechDivision\Import\ConfigurationInterface;
28
use TechDivision\Import\Configuration\PluginConfigurationInterface;
29
use TechDivision\Import\Configuration\OperationConfigurationInterface;
30
use TechDivision\Import\Configuration\ListenerAwareConfigurationInterface;
31
use TechDivision\Import\Configuration\Jms\Configuration\Subject\ImportAdapter;
32
use TechDivision\Import\Configuration\Jms\Configuration\Subject\ExportAdapter;
33
34
/**
35
 * A simple plugin configuration implementation.
36
 *
37
 * @author    Tim Wagner <[email protected]>
38
 * @copyright 2016 TechDivision GmbH <[email protected]>
39
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
40
 * @link      https://github.com/techdivision/import-configuration-jms
41
 * @link      http://www.techdivision.com
42
 */
43
class Plugin implements PluginConfigurationInterface, ListenerAwareConfigurationInterface
44
{
45
46
    /**
47
     * The trait that provides parameter configuration functionality.
48
     *
49
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait
50
     */
51
    use ParamsTrait;
52
53
    /**
54
     * Trait that provides CSV configuration functionality.
55
     *
56
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ListenersTrait
57
     */
58
    use ListenersTrait;
59
60
    /**
61
     * The main configuration.
62
     *
63
     * @var string
64
     */
65
    protected $configuration;
66
67
    /**
68
     * The execution context.
69
     *
70
     * @var \TechDivision\Import\Configuration\OperationConfigurationInterface
71
     */
72
    protected $operationConfiguration;
73
74
    /**
75
     * The plugin's unique DI identifier.
76
     *
77
     * @var string
78
     * @Type("string")
79
     * @SerializedName("id")
80
     */
81
    protected $id;
82
83
    /**
84
     * The plugin's name.
85
     *
86
     * @var string
87
     * @Type("string")
88
     * @SerializedName("name")
89
     */
90
    protected $name;
91
92
    /**
93
     * ArrayCollection with the information of the configured subjects.
94
     *
95
     * @var \Doctrine\Common\Collections\ArrayCollection
96
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Subject>")
97
     */
98
    protected $subjects;
99
100
    /**
101
     * The swift mailer configuration to use.
102
     *
103
     * @var \TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer
104
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer")
105
     * @SerializedName("swift-mailer")
106
     */
107
    protected $swiftMailer;
108
109
    /**
110
     * The import adapter configuration instance.
111
     *
112
     * @var \TechDivision\Import\Configuration\Subject\ImportAdapterConfigurationInterface
113
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\ImportAdapter")
114
     * @SerializedName("import-adapter")
115
     */
116
    protected $importAdapter;
117
118
    /**
119
     * The export adapter configuration instance.
120
     *
121
     * @var \TechDivision\Import\Configuration\Subject\ExportAdapterConfigurationInterface
122
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\ExportAdapter")
123
     * @SerializedName("export-adapter")
124
     */
125
    protected $exportAdapter;
126
127
    /**
128
     * Lifecycle callback that will be invoked after deserialization.
129
     *
130
     * @return void
131
     * @PostDeserialize
132
     */
133
    public function postDeserialize()
134
    {
135
136
        // create an empty collection if no subjects has been specified
137
        if ($this->subjects === null) {
138
            $this->subjects = new ArrayCollection();
139
        }
140
141
        // set a default import adatper if none has been configured
142
        if ($this->importAdapter === null) {
143
            $this->importAdapter = new ImportAdapter();
144
        }
145
146
        // set a default export adatper if none has been configured
147
        if ($this->exportAdapter === null) {
148
            $this->exportAdapter = new ExportAdapter();
149
        }
150
    }
151
152
    /**
153
     * Set's the reference to the configuration instance.
154
     *
155
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance
156
     *
157
     * @return void
158
     */
159
    public function setConfiguration(ConfigurationInterface $configuration)
160
    {
161
        $this->configuration = $configuration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $configuration of type object<TechDivision\Impo...ConfigurationInterface> is incompatible with the declared type string of property $configuration.

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...
162
    }
163
164
    /**
165
     * Return's the reference to the configuration instance.
166
     *
167
     * @return \TechDivision\Import\ConfigurationInterface The configuration instance
168
     */
169
    public function getConfiguration()
170
    {
171
        return $this->configuration;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->configuration; (string) is incompatible with the return type declared by the interface TechDivision\Import\Conf...rface::getConfiguration of type TechDivision\Import\ConfigurationInterface.

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...
172
    }
173
174
    /**
175
     * Return's the plugin's unique DI identifier.
176
     *
177
     * @return string The plugin's unique DI identifier
178
     */
179
    public function getId()
180
    {
181
        return $this->id;
182
    }
183
184
    /**
185
     * Return's the plugin's name or the ID, if the name is NOT set.
186
     *
187
     * @return string The plugin's name
188
     * @see \TechDivision\Import\Configuration\PluginConfigurationInterface::getId()
189
     */
190
    public function getName()
191
    {
192
        return $this->name ? $this->name : $this->getId();
193
    }
194
195
    /**
196
     * Return's the ArrayCollection with the operation's subjects.
197
     *
198
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operation's subjects
199
     */
200
    public function getSubjects()
201
    {
202
203
        // initialize the array with the subject configurations
204
        $subjects = array();
205
206
        // iterate over the subject configurations
207
        foreach ($this->subjects as $subject) {
208
            // inject the parent plugin configuration
209
            $subject->setPluginConfiguration($this);
210
            // add the subject to the array
211
            $subjects[] = $subject;
212
        }
213
214
        // return the array with subject configurations
215
        return $subjects;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $subjects; (array) is incompatible with the return type declared by the interface TechDivision\Import\Conf...nInterface::getSubjects of type Doctrine\Common\Collections\ArrayCollection.

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...
216
    }
217
218
    /**
219
     * Return's the swift mailer configuration to use.
220
     *
221
     * @return \TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer The swift mailer configuration to use
222
     */
223
    public function getSwiftMailer()
224
    {
225
        return $this->swiftMailer;
226
    }
227
228
    /**
229
     * Return's the import adapter configuration instance.
230
     *
231
     * @return \TechDivision\Import\Configuration\Subject\ImportAdapterConfigurationInterface The import adapter configuration instance
232
     */
233
    public function getImportAdapter()
234
    {
235
        return $this->importAdapter;
236
    }
237
238
    /**
239
     * Return's the export adapter configuration instance.
240
     *
241
     * @return \TechDivision\Import\Configuration\Subject\ExportAdapterConfigurationInterface The export adapter configuration instance
242
     */
243
    public function getExportAdapter()
244
    {
245
        return $this->exportAdapter;
246
    }
247
248
    /**
249
     * Set's the configuration of the operation the plugin has been configured for.
250
     *
251
     * @param \\TechDivision\Import\Configuration\OperationConfigurationInterface $operationConfiguration The operation configuration
252
     *
253
     * @return void
254
     */
255
    public function setOperationConfiguration(OperationConfigurationInterface $operationConfiguration)
256
    {
257
        $this->operationConfiguration = $operationConfiguration;
258
    }
259
260
    /**
261
     * Return's the configuration of the operation the plugin has been configured for.
262
     *
263
     * @return \TechDivision\Import\Configuration\OperationConfigurationInterface The operation configuration
264
     */
265
    public function getOperationConfiguration()
266
    {
267
        return $this->operationConfiguration;
268
    }
269
270
    /**
271
     * Return's the execution context configuration for the actualy plugin configuration.
272
     *
273
     * @return \TechDivision\Import\ExecutionContextInterface The execution context to use
274
     */
275
    public function getExecutionContext()
276
    {
277
        return $this->getOperationConfiguration()->getExecutionContext();
278
    }
279
280
    /**
281
     * Return's the full opration name, which consists of the Magento edition, the entity type code and the operation name.
282
     *
283
     * @param string $separator The separator used to seperate the elements
284
     *
285
     * @return string The full operation name
286
     */
287
    public function getFullOperationName($separator = '/')
0 ignored issues
show
Unused Code introduced by
The parameter $separator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
288
    {
289
        return $this->getOperationConfiguration()->getFullName();
290
    }
291
}
292