StatBilling::siteBreakdown()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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