Report::getRevenueReport()   B
last analyzed

Complexity

Conditions 7
Paths 10

Size

Total Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 41
rs 8.3306
c 0
b 0
f 0
cc 7
nc 10
nop 1
1
<?php
2
3
namespace Audiens\DoubleclickClient\service;
4
5
use Audiens\DoubleclickClient\ApiConfigurationInterface;
6
use Audiens\DoubleclickClient\Auth;
7
use Audiens\DoubleclickClient\CachableTrait;
8
use Audiens\DoubleclickClient\CacheableInterface;
9
use Audiens\DoubleclickClient\entity\ReportConfig;
10
use Audiens\DoubleclickClient\entity\SegmentCommunication;
11
use Audiens\DoubleclickClient\entity\SegmentRevenue;
12
use Audiens\DoubleclickClient\exceptions\ReportException;
13
use Audiens\DoubleclickClient\entity\ApiResponse;
14
use Doctrine\Common\Cache\Cache;
15
use GuzzleHttp\Client;
16
use GuzzleHttp\ClientInterface;
17
use GuzzleHttp\Exception\RequestException;
18
19
class Report implements CacheableInterface, ApiConfigurationInterface
20
{
21
22
    use CachableTrait;
23
24
    public const BASE_URL_PROVIDER = 'https://ddp.googleapis.com/api/ddp/provider/'.self::API_VERSION.'/UserListClientService?wsdl';
25
    public const BASE_URL_DDP      = 'https://ddp.googleapis.com/api/ddp/cmu/'.self::API_VERSION.'/CustomerMatchUploaderService?wsdl';
26
    public const USER_LIST_SERVICE = 'https://ddp.googleapis.com/api/ddp/provider/'.self::API_VERSION.'/UserListService?wsdl';
27
28
    public const REVENUE_REPORT_TEMPLATE_NAME = 'revenue.xml.twig';
29
    public const DMP_REPORT_TEMPLATE_NAME     = 'dmp.xml.twig';
30
31
    /** @var Client|Auth */
32
    protected $client;
33
34
    /** @var  int */
35
    protected $memberId;
36
37
    /** @var  Cache */
38
    protected $cache;
39
40
    /** @var string */
41
    protected $baseUrl;
42
43
    /** @var  string */
44
    protected $baseUrlDdp;
45
46
    /** @var  TwigCompiler */
47
    protected $twigCompiler;
48
49
    public function __construct(ClientInterface $client, TwigCompiler $twigCompiler, Cache $cache = null)
50
    {
51
        $this->client       = $client;
0 ignored issues
show
Documentation Bug introduced by
It seems like $client of type object<GuzzleHttp\ClientInterface> is incompatible with the declared type object<GuzzleHttp\Client...DoubleclickClient\Auth> of property $client.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52
        $this->cache        = $cache;
53
        $this->twigCompiler = $twigCompiler;
54
        $this->cacheEnabled = $cache instanceof Cache;
55
56
        $this->baseUrl    = self::BASE_URL_PROVIDER;
57
        $this->baseUrlDdp = self::BASE_URL_DDP;
58
    }
59
60
    public function getBaseUrl(): string
61
    {
62
        return $this->baseUrl;
63
    }
64
65
    public function setBaseUrl(string $baseUrl)
66
    {
67
        $this->baseUrl = $baseUrl;
68
    }
69
70
    public function getRevenueReport(ReportConfig $reportConfig): array
71
    {
72
        $compiledUrl = $this->baseUrl;
73
74
        $requestBody = $this->twigCompiler->getTwig()->render(
75
            self::API_VERSION.'/'.self::REVENUE_REPORT_TEMPLATE_NAME,
76
            $reportConfig->toArray()
77
        );
78
79
        try {
80
            $response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]);
81
        } catch (RequestException $e) {
82
            $response = $e->getResponse();
83
        }
84
85
        $repositoryResponse = ApiResponse::fromResponse($response);
86
87
        if (!$repositoryResponse->isSuccessful()) {
88
            throw ReportException::failed($repositoryResponse);
89
        }
90
91
        if (!isset($repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'])) {
92
            return [];
93
        }
94
95
        $entries = $repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'];
96
97
        if (\is_array($entries) && isset($entries['userlistid'])) {
98
            $segmentsRevenue[] = SegmentRevenue::fromArray($entries);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$segmentsRevenue was never initialized. Although not strictly required by PHP, it is generally a good practice to add $segmentsRevenue = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
99
100
            return $segmentsRevenue;
101
        }
102
103
        $segmentsRevenue = [];
104
105
        foreach ($entries as $entry) {
106
            $segmentsRevenue[] = SegmentRevenue::fromArray($entry);
107
        }
108
109
        return $segmentsRevenue;
110
    }
111
112
    /**
113
     * @param ReportConfig $reportConfig
114
     *
115
     * @return array
116
     * @throws ReportException
117
     */
118
    public function getDmpReport(ReportConfig $reportConfig): array
119
    {
120
        $compiledUrl = self::USER_LIST_SERVICE;
121
122
        $requestBody = $this->twigCompiler->getTwig()->render(
123
            self::API_VERSION.'/'.self::DMP_REPORT_TEMPLATE_NAME,
124
            $reportConfig->toArray()
125
        );
126
127
        try {
128
            $response = $this->client->request('POST', $compiledUrl, ['body' => $requestBody]);
129
        } catch (RequestException $e) {
130
            $response = $e->getResponse();
131
        }
132
133
        $repositoryResponse = ApiResponse::fromResponse($response);
134
135
        if (!$repositoryResponse->isSuccessful()) {
136
            throw ReportException::failed($repositoryResponse);
137
        }
138
139
        if (!isset($repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'])
140
        ) {
141
            throw ReportException::missingIndex('body->envelope->body->getresponse->rval->entries');
142
        }
143
144
        $entries = $repositoryResponse->getResponseArray()['body']['envelope']['body']['getresponse']['rval']['entries'];
145
146
        $segmentCommunication = [];
147
148
        if (is_array($entries) && isset($entries['id'])) {
149
            $segmentCommunication[] = SegmentCommunication::fromArray($entries);
150
151
            return $segmentCommunication;
152
        }
153
        foreach ($entries as $entry) {
154
            $segmentCommunication[] = SegmentCommunication::fromArray($entry);
155
        }
156
157
        return $segmentCommunication;
158
    }
159
}
160