Completed
Push — master ( 2a6a78...c2b2ef )
by DELEVOYE
05:24
created

SubscriberEncoder::encode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 2
nop 3
crap 3
1
<?php
2
3
/*
4
 * This file is part of the MindbazBundle package.
5
 *
6
 * (c) David DELEVOYE <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Kozikaza\MindbazBundle\Serializer;
13
14
use mbzSubscriber\Subscriber;
15
use mbzSubscriber\SubscriberFieldData;
16
use Symfony\Component\Serializer\Encoder\DecoderInterface;
17
use Symfony\Component\Serializer\Encoder\EncoderInterface;
18
19
/**
20
 * @author Vincent Chalamon <[email protected]>
21
 */
22
class SubscriberEncoder implements EncoderInterface, DecoderInterface
23
{
24
    const FORMAT = 'mindbaz';
25
    const FIELDS = [
26
        'id'                    => 0,
27
        'email'                 => 1,
28
        'firstSubscriptionDate' => 2,
29
        'lastSubscriptionDate'  => 3,
30
        'unsubscriptionDate'    => 4,
31
        'status'                => 7,
32
        'civility'              => 13,
33
        'lastName'              => 14,
34
        'firstName'             => 15,
35
        'city'                  => 17,
36
        'zicode'                => 18,
37
        'country'               => 19,
38
    ];
39
40
    /**
41
     * @param array  $data
42
     * @param string $format
43
     * @param array  $context
44
     *
45
     * @return Subscriber
46
     */
47 1 View Code Duplication
    public function encode($data, $format, array $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49 1
        $fld = [];
50 1
        foreach ($data as $key => $value) {
51 1
            $fld[] = (new SubscriberFieldData(self::FIELDS[$key]))->setValue($value);
52 1
        }
53
54 1
        $subscriber = new Subscriber(isset($data['id']) ? $data['id'] : -1);
55 1
        $subscriber->setFld($fld);
56
57 1
        return $subscriber;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $subscriber; (mbzSubscriber\Subscriber) is incompatible with the return type declared by the interface Symfony\Component\Serial...ncoderInterface::encode of type integer|double|string|boolean.

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...
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function supportsEncoding($format)
64
    {
65 2
        return self::FORMAT === $format;
66
    }
67
68
    /**
69
     * @param Subscriber $subscriber
70
     * @param string     $format
71
     * @param array      $context
72
     *
73
     * @return array
74
     */
75 1 View Code Duplication
    public function decode($subscriber, $format, array $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
76
    {
77 1
        $fields = array_flip(SubscriberEncoder::FIELDS);
78 1
        $data = [];
79 1
        foreach ($subscriber->getFld() as $fld) {
80 1
            $data[$fields[$fld->getIdField()]] = $fld->getValue();
81 1
        }
82
83 1
        return $data;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 2
    public function supportsDecoding($format)
90
    {
91 2
        return self::FORMAT === $format;
92
    }
93
}
94