Completed
Pull Request — master (#12)
by Tim
01:31
created

Plugin::postDeserialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 3
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
30
/**
31
 * A simple plugin configuration implementation.
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 Plugin implements PluginConfigurationInterface
40
{
41
42
    /**
43
     * The trait that provides parameter configuration functionality.
44
     *
45
     * @var \TechDivision\Import\Configuration\Jms\Configuration\ParamsTrait
46
     */
47
    use ParamsTrait;
48
49
    /**
50
     * The main configuration.
51
     *
52
     * @var string
53
     */
54
    protected $configuration;
55
56
    /**
57
     * The plugin's unique DI identifier.
58
     *
59
     * @var string
60
     * @Type("string")
61
     * @SerializedName("id")
62
     */
63
    protected $id;
64
65
    /**
66
     * ArrayCollection with the information of the configured subjects.
67
     *
68
     * @var \Doctrine\Common\Collections\ArrayCollection
69
     * @Type("ArrayCollection<TechDivision\Import\Configuration\Jms\Configuration\Subject>")
70
     */
71
    protected $subjects;
72
73
    /**
74
     * The swift mailer configuration to use.
75
     *
76
     * @var \TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer
77
     * @Type("TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer")
78
     * @SerializedName("swift-mailer")
79
     */
80
    protected $swiftMailer;
81
82
    /**
83
     * Lifecycle callback that will be invoked after deserialization.
84
     *
85
     * @return void
86
     * @PostDeserialize
87
     */
88
    public function postDeserialize()
89
    {
90
91
        // create an empty collection if no subjects has been specified
92
        if ($this->subjects === null) {
93
            $this->subjects = new ArrayCollection();
94
        }
95
    }
96
97
    /**
98
     * Set's the reference to the configuration instance.
99
     *
100
     * @param \TechDivision\Import\ConfigurationInterface $configuration The configuration instance
101
     *
102
     * @return void
103
     */
104
    public function setConfiguration(ConfigurationInterface $configuration)
105
    {
106
        $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...
107
    }
108
109
    /**
110
     * Return's the reference to the configuration instance.
111
     *
112
     * @return \TechDivision\Import\ConfigurationInterface The configuration instance
113
     */
114
    public function getConfiguration()
115
    {
116
        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...
117
    }
118
119
    /**
120
     * Return's the subject's unique DI identifier.
121
     *
122
     * @return string The subject's unique DI identifier
123
     */
124
    public function getId()
125
    {
126
        return $this->id;
127
    }
128
129
    /**
130
     * Return's the ArrayCollection with the operation's subjects.
131
     *
132
     * @return \Doctrine\Common\Collections\ArrayCollection The ArrayCollection with the operation's subjects
133
     */
134
    public function getSubjects()
135
    {
136
        return $this->subjects;
137
    }
138
139
    /**
140
     * Return's the swift mailer configuration to use.
141
     *
142
     * @return TechDivision\Import\Configuration\Jms\Configuration\SwiftMailer The swift mailer configuration to use
143
     */
144
    public function getSwiftMailer()
145
    {
146
        return $this->swiftMailer;
147
    }
148
}
149