Completed
Push — v3 ( d12fea )
by Beñat
05:39
created

AnemicNetworkPost::fromJson()   B

Complexity

Conditions 5
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
rs 8.8571
cc 5
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 Beñat Espiña <[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 BenatEspina\StackExchangeApiClient\Infrastructure\Domain\Model;
13
14
use BenatEspina\StackExchangeApiClient\Domain\Model\NetworkPost;
15
16
/**
17
 * The anemic implementation of network post domain class.
18
 *
19
 * @author Beñat Espiña <[email protected]>
20
 */
21
class AnemicNetworkPost implements NetworkPost
22
{
23
    const POST_TYPE_QUESTION = 'question';
24
    const POST_TYPE_ANSWER = 'answer';
25
26
    private $id;
27
    private $postType;
28
    private $score;
29
    private $title;
30
31
    public static function fromProperties($id, $postType, $score, $title)
32
    {
33
        return new self($id, $postType, $score, $title);
34
    }
35
36
    public static function fromJson($data)
37
    {
38
        return new self(
39
            array_key_exists('post_id', $data) ? $data['post_id'] : null,
40
            array_key_exists('post_type', $data) ? $data['post_type'] : null,
41
            array_key_exists('score', $data) ? $data['score'] : null,
42
            array_key_exists('title', $data) ? $data['title'] : null
43
        );
44
    }
45
46
    private function __construct($id = null, $postType = null, $score = null, $title = null)
47
    {
48
        $this->id = $id;
49
        $this->setPostType($postType);
50
        $this->score = $score;
51
        $this->title = $title;
52
    }
53
54
    public function getId()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
55
    {
56
        return $this->id;
57
    }
58
59
    public function setId($id)
60
    {
61
        $this->id = $id;
62
63
        return $this;
64
    }
65
66
    public function getPostType()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
    {
68
        return $this->postType;
69
    }
70
71 View Code Duplication
    public function setPostType($postType)
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...
72
    {
73
        if (in_array($postType, [self::POST_TYPE_QUESTION, self::POST_TYPE_ANSWER], true)) {
74
            $this->postType = $postType;
75
        }
76
77
        return $this;
78
    }
79
80
    public function getScore()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
81
    {
82
        return $this->score;
83
    }
84
85
    public function setScore($score)
86
    {
87
        $this->score = $score;
88
89
        return $this;
90
    }
91
92
    public function getTitle()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
93
    {
94
        return $this->title;
95
    }
96
97
    public function setTitle($title)
98
    {
99
        $this->title = $title;
100
101
        return $this;
102
    }
103
}
104