Passed
Push — master ( 458136...010310 )
by Willy
01:57
created

FeedObjectFactory::getFeedObject()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 41
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 41
ccs 0
cts 37
cp 0
rs 8.5806
cc 4
eloc 31
nc 4
nop 1
crap 20
1
<?php
2
3
namespace Kubinashi\BattlenetApi\WorldOfWarcraft\CharacterProfileApi\Model\Feed;
4
use Kubinashi\BattlenetApi\WorldOfWarcraft\Model\AchievementValueObject;
5
use Kubinashi\BattlenetApi\WorldOfWarcraft\Shared\Criteria\Service\CriteriaService;
6
7
/**
8
 * @author  Willy Reiche
9
 * @since   2017-08-22
10
 * @version 1.0
11
 */
12
class FeedObjectFactory
13
{
14
    /**
15
     * @var CriteriaService
16
     */
17
    private $criteriaService;
18
19
    /**
20
     * FeedObjectFactory constructor.
21
     */
22
    public function __construct()
23
    {
24
        $this->criteriaService = new CriteriaService();
25
    }
26
27
    /**
28
     * @param \StdClass $feed
29
     * @return FeedLootValueObject
30
     */
31
    public function getFeedObject($feed)
32
    {
33
        $feedObj = null;
34
35
        switch ($feed->type) {
36
            case 'LOOT':
37
                $feedObj = new FeedLootValueObject(
38
                    $feed->type,
39
                    $feed->timestamp,
40
                    $feed->itemId,
41
                    $feed->context,
42
                    $feed->bonusLists
43
                );
44
                break;
45
            case 'BOSSKILL':
46
                $achievement = $this->prepareAchievementValueObject($feed->achievement);
47
48
                $feedObj = new FeedBosskillValueObject(
49
                    $feed->type,
50
                    $feed->timestamp,
51
                    $achievement,
52
                    $feed->featOfStrength,
53
                    $this->criteriaService->prepareCriteriaValueObject($feed->criteria),
54
                    $feed->quantity,
55
                    $feed->name
56
                );
57
                break;
58
            case 'ACHIEVEMENT':
59
                $achievement = $this->prepareAchievementValueObject($feed->achievement);
60
61
                $feedObj = new FeedAchievementValueObject(
62
                    $feed->type,
63
                    $feed->timestamp,
64
                    $achievement,
65
                    $feed->featOfStrength
66
                );
67
                break;
68
        }
69
70
        return $feedObj;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $feedObj; (Kubinashi\BattlenetApi\W...evementValueObject|null) is incompatible with the return type documented by Kubinashi\BattlenetApi\W...tFactory::getFeedObject of type Kubinashi\BattlenetApi\W...eedLootValueObject|null.

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...
71
    }
72
73
    /**
74
     * @param \StdClass $achievement
75
     * @return AchievementValueObject
76
     */
77
    private function prepareAchievementValueObject($achievement)
78
    {
79
        $criteria = [];
80
81
        foreach ($achievement->criteria as $singleCriteria) {
82
            $criteria[] = $this->criteriaService->prepareCriteriaValueObject($singleCriteria);
83
        }
84
85
        return new AchievementValueObject(
86
            $achievement->id,
87
            $achievement->title,
88
            $achievement->points,
89
            $achievement->description,
90
            $achievement->rewardItems,
91
            $achievement->icon,
92
            $criteria,
93
            $achievement->accountWide,
94
            $achievement->factionId
95
        );
96
    }
97
}
98