Passed
Branch master (d0211e)
by Willy
02:44
created

AchievementApi   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 87
Duplicated Lines 9.2 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 6
dl 8
loc 87
ccs 0
cts 45
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getAchievement() 0 21 1
A prepareRequestModel() 0 12 1
A prepareResponseObject() 8 8 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Kubinashi\BattlenetApi\WorldOfWarcraft\AchievementApi;
4
5
use Kubinashi\BattlenetApi\Model\AuthenticationModel;
6
use Kubinashi\BattlenetApi\Model\Franchises;
7
use Kubinashi\BattlenetApi\Model\RequestModel;
8
use Kubinashi\BattlenetApi\Service\RequestService;
9
use Kubinashi\BattlenetApi\WorldOfWarcraft\AchievementApi\Achievement\Model\AchievementValueObject;
10
use Kubinashi\BattlenetApi\WorldOfWarcraft\Shared\Criteria\Service\CriteriaService;
11
use Kubinashi\BattlenetApi\WorldOfWarcraft\Shared\RewardItem\Service\RewardItemService;
12
13
/**
14
 * @author  Willy Reiche
15
 * @since   2017-12-06
16
 * @version 1.0
17
 */
18
class AchievementApi
19
{
20
    /**
21
     * @var AuthenticationModel
22
     */
23
    private $authenticationModel;
24
25
    /**
26
     * @var RequestService
27
     */
28
    private $requestService;
29
30
    /**
31
     * AchievementApi constructor.
32
     *
33
     * @param AuthenticationModel $authenticationModel
34
     * @param RequestService      $requestService
35
     */
36
    public function __construct(
37
        AuthenticationModel $authenticationModel,
38
        RequestService $requestService
39
    ) {
40
        $this->authenticationModel = $authenticationModel;
41
        $this->requestService = $requestService;
42
    }
43
44
    /**
45
     * This provides data about an individual achievement
46
     *
47
     * @param int $id
48
     *
49
     * @return AchievementValueObject
50
     */
51
    public function getAchievement($id)
52
    {
53
        $responseObject = $this->prepareResponseObject($id);
54
        $rewardItemService = new RewardItemService();
55
        $criteriaService = new CriteriaService();
56
57
        $achievement = new AchievementValueObject(
58
            $responseObject->id,
59
            $responseObject->title,
60
            $responseObject->points,
61
            $responseObject->description,
62
            $responseObject->reward,
63
            $rewardItemService->prepareRewardItemValueObject($responseObject->rewardItems),
64
            $responseObject->icon,
65
            $criteriaService->prepareMultipleCriteriaValueObject($responseObject->criteria),
66
            $responseObject->accoountWide,
67
            $responseObject->factionId
68
        );
69
70
        return $achievement;
71
    }
72
73
    /**
74
     * @param string $field
75
     *
76
     * @return RequestModel
77
     */
78
    private function prepareRequestModel($field)
79
    {
80
        return new RequestModel(
81
            $this->authenticationModel->getRegion(),
82
            $this->authenticationModel->getApiKey(),
83
            $this->authenticationModel->getLocale(),
84
            [$field],
85
            'achievement',
86
            Franchises::WORLD_OF_WARCRAFT,
87
            ''
88
        );
89
    }
90
91
    /**
92
     * @param string $field
93
     *
94
     * @return \StdClass
95
     */
96 View Code Duplication
    private function prepareResponseObject($field)
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...
97
    {
98
        $requestModel = $this->prepareRequestModel($field);
99
        $response = $this->requestService->doRequest($requestModel);
100
        $responseObject = json_decode($response);
101
102
        return $responseObject;
103
    }
104
}
105