Completed
Push — 9.x ( 6695e2...a92616 )
by Tim
03:19 queued 01:32
created

Plugin::getName()   A

Complexity

Conditions 2
Paths 2

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 2
nc 2
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
31
/**
32
 * A simple plugin configuration implementation.
33
 *
34
 * @author    Tim Wagner <[email protected]>
35
 * @copyright 2016 TechDivision GmbH <[email protected]>
36
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
37
 * @link      https://github.com/techdivision/import-configuration-jms
38
 * @link      http://www.techdivision.com
39
 */
40
class Plugin implements PluginConfigurationInterface, ListenerAwareConfigurationInterface
41
{
42
43
    /**
44
     * The trait that provides parameter configuration functionality.
45
     *
46
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait
47
     */
48
    use ParamsTrait;
49
50
    /**
51
     * Trait that provides CSV configuration functionality.
52
     *
53
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ListenersTrait
54
     */
55
    use ListenersTrait;
56
57
    /**
58
     * The main configuration.
59
     *
60
     * @var string
61
     */
62
    protected $configuration;
63
64
    /**
65
     * The plugin's unique DI identifier.
66
     *
67
     * @var string
68
     * @Type("string")
69
     * @SerializedName("id")
70
     */
71
    protected $id;
72
73
    /**
74
     * The plugin's name.
75
     *
76
     * @var string
77
     * @Type("string")
78
     * @SerializedName("name")
79
     */
80
    protected $name;
81
82
    /**
83
     * ArrayCollection with the information of the configured subjects.
84
     *
85
     * @var \Doctrine\Common\Collections\ArrayCollection
86
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Subject>")
87
     */
88
    protected $subjects;
89
90
    /**
91
     * The swift mailer configuration to use.
92
     *
93
     * @var \TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer
94
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer")
95
     * @SerializedName("swift-mailer")
96
     */
97
    protected $swiftMailer;
98
99
    /**
100
     * Lifecycle callback that will be invoked after deserialization.
101
     *
102
     * @return void
103
     * @PostDeserialize
104
     */
105
    public function postDeserialize()
106
    {
107
108
        // create an empty collection if no subjects has been specified
109
        if ($this->subjects === null) {
110
            $this->subjects = new ArrayCollection();
111
        }
112
    }
113
114
    /**
115
     * Set's the reference to the configuration instance.
116
     *
117
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance
118
     *
119
     * @return void
120
     */
121
    public function setConfiguration(ConfigurationInterface $configuration)
122
    {
123
        $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...
124
    }
125
126
    /**
127
     * Return's the reference to the configuration instance.
128
     *
129
     * @return \TechDivision\Import\ConfigurationInterface The configuration instance
130
     */
131
    public function getConfiguration()
132
    {
133
        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...
134
    }
135
136
    /**
137
     * Return's the plugin's unique DI identifier.
138
     *
139
     * @return string The plugin's unique DI identifier
140
     */
141
    public function getId()
142
    {
143
        return $this->id;
144
    }
145
146
    /**
147
     * Return's the plugin's name or the ID, if the name is NOT set.
148
     *
149
     * @return string The plugin's name
150
     * @see \TechDivision\Import\Configuration\PluginConfigurationInterface::getId()
151
     */
152
    public function getName()
153
    {
154
        return $this->name ? $this->name : $this->getId();
155
    }
156
157
    /**
158
     * Return's the ArrayCollection with the operation's subjects.
159
     *
160
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operation's subjects
161
     */
162
    public function getSubjects()
163
    {
164
165
        // initialize the array with the subject configurations
166
        $subjects = array();
167
168
        // iterate over the subject configurations
169
        foreach ($this->subjects as $subject) {
170
            // inject the parent plugin configuration
171
            $subject->setPluginConfiguration($this);
172
            // add the subject to the array
173
            $subjects[] = $subject;
174
        }
175
176
        // return the array with subject configurations
177
        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...
178
    }
179
180
    /**
181
     * Return's the swift mailer configuration to use.
182
     *
183
     * @return \TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer The swift mailer configuration to use
184
     */
185
    public function getSwiftMailer()
186
    {
187
        return $this->swiftMailer;
188
    }
189
}
190