Completed
Push — 15.x ( 64043b...a8a0e5 )
by Tim
02:48
created

Plugin::postDeserialize()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 8
nop 0
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\ListenerAwareConfigurationInterface;
30
use TechDivision\Import\Configuration\Jms\Configuration\Subject\ImportAdapter;
31
use TechDivision\Import\Configuration\Jms\Configuration\Subject\ExportAdapter;
32
use TechDivision\Import\Configuration\SubjectConfigurationInterface;
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 plugin's unique DI identifier.
69
     *
70
     * @var string
71
     * @Type("string")
72
     * @SerializedName("id")
73
     */
74
    protected $id;
75
76
    /**
77
     * The plugin's name.
78
     *
79
     * @var string
80
     * @Type("string")
81
     * @SerializedName("name")
82
     */
83
    protected $name;
84
85
    /**
86
     * ArrayCollection with the information of the configured subjects.
87
     *
88
     * @var \Doctrine\Common\Collections\ArrayCollection
89
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Subject>")
90
     */
91
    protected $subjects;
92
93
    /**
94
     * The swift mailer configuration to use.
95
     *
96
     * @var \TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer
97
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer")
98
     * @SerializedName("swift-mailer")
99
     */
100
    protected $swiftMailer;
101
102
    /**
103
     * The import adapter configuration instance.
104
     *
105
     * @var \TechDivision\Import\Configuration\Subject\ImportAdapterConfigurationInterface
106
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\ImportAdapter")
107
     * @SerializedName("import-adapter")
108
     */
109
    protected $importAdapter;
110
111
    /**
112
     * The export adapter configuration instance.
113
     *
114
     * @var \TechDivision\Import\Configuration\Subject\ExportAdapterConfigurationInterface
115
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\Subject\ExportAdapter")
116
     * @SerializedName("export-adapter")
117
     */
118
    protected $exportAdapter;
119
120
    /**
121
     * Lifecycle callback that will be invoked after deserialization.
122
     *
123
     * @return void
124
     * @PostDeserialize
125
     */
126
    public function postDeserialize()
127
    {
128
129
        // create an empty collection if no subjects has been specified
130
        if ($this->subjects === null) {
131
            $this->subjects = new ArrayCollection();
132
        }
133
134
        // set a default import adatper if none has been configured
135
        if ($this->importAdapter === null) {
136
            $this->importAdapter = new ImportAdapter();
137
        }
138
139
        // set a default export adatper if none has been configured
140
        if ($this->exportAdapter === null) {
141
            $this->exportAdapter = new ExportAdapter();
142
        }
143
    }
144
145
    /**
146
     * Set's the reference to the configuration instance.
147
     *
148
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance
149
     *
150
     * @return void
151
     */
152
    public function setConfiguration(ConfigurationInterface $configuration)
153
    {
154
        $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...
155
    }
156
157
    /**
158
     * Return's the reference to the configuration instance.
159
     *
160
     * @return \TechDivision\Import\ConfigurationInterface The configuration instance
161
     */
162
    public function getConfiguration()
163
    {
164
        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...
165
    }
166
167
    /**
168
     * Return's the plugin's unique DI identifier.
169
     *
170
     * @return string The plugin's unique DI identifier
171
     */
172
    public function getId()
173
    {
174
        return $this->id;
175
    }
176
177
    /**
178
     * Return's the plugin's name or the ID, if the name is NOT set.
179
     *
180
     * @return string The plugin's name
181
     * @see \TechDivision\Import\Configuration\PluginConfigurationInterface::getId()
182
     */
183
    public function getName()
184
    {
185
        return $this->name ? $this->name : $this->getId();
186
    }
187
188
    /**
189
     * Return's the ArrayCollection with the operation's subjects.
190
     *
191
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operation's subjects
192
     */
193
    public function getSubjects()
194
    {
195
196
        // initialize the array with the subject configurations
197
        $subjects = array();
198
199
        // iterate over the subject configurations
200
        foreach ($this->subjects as $subject) {
201
            // inject the parent plugin configuration
202
            $subject->setPluginConfiguration($this);
203
            // add the subject to the array
204
            $subjects[] = $subject;
205
        }
206
207
        // return the array with subject configurations
208
        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...
209
    }
210
211
    /**
212
     * Return's the swift mailer configuration to use.
213
     *
214
     * @return \TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer The swift mailer configuration to use
215
     */
216
    public function getSwiftMailer()
217
    {
218
        return $this->swiftMailer;
219
    }
220
221
    /**
222
     * Return's the import adapter configuration instance.
223
     *
224
     * @return \TechDivision\Import\Configuration\Subject\ImportAdapterConfigurationInterface The import adapter configuration instance
225
     */
226
    public function getImportAdapter()
227
    {
228
        return $this->importAdapter;
229
    }
230
231
    /**
232
     * Return's the export adapter configuration instance.
233
     *
234
     * @return \TechDivision\Import\Configuration\Subject\ExportAdapterConfigurationInterface The export adapter configuration instance
235
     */
236
    public function getExportAdapter()
237
    {
238
        return $this->exportAdapter;
239
    }
240
}
241