Completed
Push — master ( ae4bd7...74685b )
by Felix
02:37
created

StatBilling::userBreakdown()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 2
eloc 14
nc 2
nop 2
1
<?php
2
3
namespace SchulzeFelix\Stat\Api;
4
5
use Carbon\Carbon;
6
use Illuminate\Support\Collection;
7
use SchulzeFelix\Stat\Objects\StatBill;
8
use SchulzeFelix\Stat\Objects\StatBillKeywordType;
9
use SchulzeFelix\Stat\Objects\StatBillKeywordTypes;
10
use SchulzeFelix\Stat\Objects\StatBillOptionalServiceType;
11
use SchulzeFelix\Stat\Objects\StatBillServices;
12
use SchulzeFelix\Stat\Objects\StatBillSummary;
13
use SchulzeFelix\Stat\Objects\StatSite;
14
use SchulzeFelix\Stat\Objects\StatSubAccount;
15
use SchulzeFelix\Stat\Objects\StatTag;
16
17
class StatBilling extends BaseStat
18
{
19
20
    public function bill($year, $month)
21
    {
22
        $response = $this->performQuery('billing/bill', ['year' => $year, 'month' => $month]);
23
24
        $statBill = new StatBill();
25
        $statBill->summary = $this->extractSummary($response);
26
        $statBill->services = new StatBillServices();
27
28
        $statBill->services->keywords = new StatBillKeywordTypes();
29
30
        $statBill->services->keywords->under_commit = new StatBillKeywordType([
31
            'count' => $response['Services']['Keywords']['UnderCommit']['Count'],
32
            'price' => $response['Services']['Keywords']['UnderCommit']['Price'],
33
            'total' => $response['Services']['Keywords']['UnderCommit']['Total'],
34
        ]);
35
36
        $statBill->services->keywords->over_commit = new StatBillKeywordType([
37
            'count' => $response['Services']['Keywords']['OverCommit']['Count'],
38
            'price' => $response['Services']['Keywords']['OverCommit']['Price'],
39
            'total' => $response['Services']['Keywords']['OverCommit']['Total'],
40
        ]);
41
42
        $statBill->services->keywords->non_unique = new StatBillKeywordType([
43
            'count' => $response['Services']['Keywords']['NonUnique']['Count'],
44
            'price' => $response['Services']['Keywords']['NonUnique']['Price'],
45
            'total' => $response['Services']['Keywords']['NonUnique']['Total'],
46
        ]);
47
48
        $statBill->services->optional_services = $this->extractOptionalServices(array_get($response, 'Services.OptionalServices'));
49
50
        return $statBill;
51
    }
52
53
    public function userBreakdown($year, $month)
54
    {
55
        $response = $this->performQuery('billing/user_breakdown', ['year' => $year, 'month' => $month]);
56
57
        $statBill = new StatBill();
58
        $statBill->summary = $this->extractSummary($response);
59
        $statBill->users = new Collection();
60
61
        foreach ($response['Users']['User'] as $user) {
62
            $statBill->users->push(new StatSubAccount([
63
                'id' => array_get($user, 'Id'),
64
                'name' => array_get($user, 'Name'),
65
                'count' => array_get($user, 'Count'),
66
                'percentage_of_bill' => array_get($user, 'PercentageOfBill'),
67
                'deleted' => filter_var(array_get($user, 'Deleted'), FILTER_VALIDATE_BOOLEAN),
68
                'total' => array_get($user, 'Total'),
69
            ]));
70
        }
71
72
        return $statBill;
73
    }
74
75
    public function siteBreakdown($year, $month, $charged_only = false)
76
    {
77
        $response = $this->performQuery('billing/site_breakdown', ['year' => $year, 'month' => $month, 'charged_only' => $charged_only]);
78
79
        $statBill = new StatBill();
80
        $statBill->summary = $this->extractSummary($response);
81
        $statBill->sites = new Collection();
82
83
        foreach ($response['Sites']['Site'] as $site) {
84
            $statSite = new StatSite([
85
                'id' => array_get($site, 'Id'),
86
                'title' => array_get($site, 'Title'),
87
                'url' => array_get($site, 'URL'),
88
                'project_id' => array_get($site, 'ProjectId'),
89
                'project_name' => array_get($site, 'ProjectName'),
90
                'folder_id' => array_get($site, 'FolderId'),
91
                'folder_name' => array_get($site, 'FolderName'),
92
                'deleted' => filter_var(array_get($site, 'Deleted'), FILTER_VALIDATE_BOOLEAN),
93
                'services' => new StatBillServices(),
94
            ]);
95
96
            $statSite->services->keywords = new StatBillKeywordType([
97
                'count' => array_get($site, 'Services.Keywords.Count'),
98
                'percentage_of_bill' => array_get($site, 'Services.Keywords.PercentageOfBill'),
99
                'total' => array_get($site, 'Services.Keywords.Total'),
100
            ]);
101
            $statSite->services->optional_services = $this->extractOptionalServices(array_get($site, 'Services.OptionalServices'));
102
            $statSite->services->total = array_get($site, 'Services.Total');
103
104
            $statBill->sites->push($statSite);
105
        }
106
107
        return $statBill;
108
109
    }
110
111
    /**
112
     * @param $response
113
     * @return StatBillSummary
114
     */
115
    private function extractSummary($response)
116
    {
117
        $statBillSummary = new StatBillSummary([
118
            'start_date'             => $response['Summary']['StartDate'],
119
            'end_date'               => $response['Summary']['EndDate'],
120
            'min_committed_charge'   => (float)array_get($response, 'Summary.MinCommittedCharge', 0.0),
121
            'tracked_keywords'       => (int)$response['Summary']['TrackedKeywords'],
122
            'tracked_keywords_total' => (float)$response['Summary']['TrackedKeywordsTotal'],
123
            'optional_service_total' => (float)$response['Summary']['OptionalServiceTotal'],
124
            'total'                  => (float)$response['Summary']['Total'],
125
        ]);
126
127
        return $statBillSummary;
128
    }
129
130
    private function extractOptionalServices($optionalServices)
131
    {
132
        $services = new Collection();
133
134
        if(is_null($optionalServices['OptionalService'])){
135
            return $services;
136
        }
137
138
        foreach ($optionalServices['OptionalService'] as $service) {
139
            $services->push(new StatBillOptionalServiceType([
140
                'type' => array_get($service, 'type'),
141
                'count' => array_get($service, 'Count'),
142
                'price' => array_get($service, 'Price'),
143
                'total' => array_get($service, 'Total'),
144
            ]));
145
        }
146
147
        return $services;
148
    }
149
150
}
151