Completed
Pull Request — master (#113)
by Kristof
06:16
created

ExportEventsJSONDeserializer::deserialize()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 26
rs 8.439
cc 5
eloc 13
nc 9
nop 1
1
<?php
2
3
namespace CultuurNet\UDB3\EventExport\Command;
4
5
use CultuurNet\Deserializer\JSONDeserializer;
6
use CultuurNet\Deserializer\MissingValueException;
7
use CultuurNet\UDB3\EventExport\EventExportQuery;
8
use ValueObjects\String\String;
9
use ValueObjects\Web\EmailAddress;
10
11
abstract class ExportEventsJSONDeserializer extends JSONDeserializer
12
{
13
    /**
14
     * @param \ValueObjects\String\String $data
15
     * @return ExportEvents
16
     */
17
    public function deserialize(String $data)
18
    {
19
        $data = parent::deserialize($data);
20
21
        if (!isset($data->query)) {
22
            throw new MissingValueException('query is missing');
23
        }
24
25
        $query = new EventExportQuery($data->query);
26
        $email = $selection = $include = null;
27
28
        // @todo This throws an exception when the e-mail is invalid. How do we handle this?
29
        if (isset($data->email)) {
30
            $email = new EmailAddress($data->email);
31
        }
32
33
        if (isset($data->selection)) {
34
            $selection = $data->selection;
35
        }
36
37
        if (isset($data->include)) {
38
            $include = $data->include;
39
        }
40
41
        return $this->createCommand($query, $email, $selection, $include);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->createComm... $selection, $include); (CultuurNet\UDB3\EventExport\Command\ExportEvents) is incompatible with the return type of the parent method CultuurNet\Deserializer\...serializer::deserialize of type stdClass.

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...
42
    }
43
44
    /**
45
     * @param EventExportQuery $query
46
     * @param EmailAddress|null $address
47
     * @param string[]|null $selection
48
     * @param string[]|null $include
49
     * @return ExportEvents
50
     */
51
    abstract protected function createCommand(
52
        EventExportQuery $query,
53
        EmailAddress $address = null,
54
        $selection = null,
55
        $include = null
56
    );
57
}
58