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

AchievementApi::prepareRequestModel()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 12
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 1
crap 2
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