Completed
Push — master ( bd8043...d919b4 )
by Francesco
05:20
created

UserUpload::enableCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Audiens\AppnexusClient\service;
4
5
use Audiens\AppnexusClient\Auth;
6
use Audiens\AppnexusClient\CacheableInterface;
7
use Audiens\AppnexusClient\entity\UploadTicket;
8
use Audiens\AppnexusClient\entity\UploadJobStatus;
9
use Audiens\AppnexusClient\repository\RepositoryResponse;
10
use Doctrine\Common\Cache\Cache;
11
use GuzzleHttp\Client;
12
use GuzzleHttp\ClientInterface;
13
14
/**
15
 * Class UserSegmentRepository
16
 */
17
class UserUpload implements CacheableInterface
18
{
19
20
    const BASE_URL = 'http://api.adnxs.com/batch-segment';
21
22
    /** @var  \SplQueue */
23
    protected $userSegments;
24
25
    /** @var Client|Auth */
26
    protected $client;
27
28
    /** @var  int */
29
    protected $memberId;
30
31
    /** @var  Cache */
32
    protected $cache;
33
34
    /** @var bool */
35
    protected $cacheEnabled;
36
37
    const CACHE_NAMESPACE = 'appnexus_segment_user_upload';
38
39
    const CACHE_EXPIRATION = 3600;
40
41
    /**
42
     * SegmentRepository constructor.
43
     *
44
     * @param ClientInterface $client
45
     * @param Cache|null      $cache
46
     */
47
    public function __construct(ClientInterface $client, Cache $cache = null)
48
    {
49
        $this->client = $client;
0 ignored issues
show
Documentation Bug introduced by
$client is of type object<GuzzleHttp\ClientInterface>, but the property $client was declared to be of type object<GuzzleHttp\Client>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
50
        $this->cache = $cache;
51
        $this->cacheEnabled = $cache instanceof Cache;
52
53
    }
54
55
    /**
56
     * @param $fileAsString
57
     * @param $memberId
58
     *
59
     * @return UploadJobStatus
60
     * @throws \Exception
61
     */
62
    public function upload($fileAsString, $memberId)
63
    {
64
65
        $tempFile = tmpfile();
66
        fwrite($tempFile, $fileAsString);
67
        fseek($tempFile, 0);
68
69
        $job = $this->getUploadTicket($memberId);
70
71
        $response = $this->client->request('POST', $job->getUploadUrl(), ['body' => $tempFile]);
72
73
        $repositoryResponse = RepositoryResponse::fromResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
74
75
        if (!$repositoryResponse->isSuccessful()) {
76
            throw new \Exception('name me - not success');
77
        }
78
79
        return $this->getJobStatus($job);
80
81
    }
82
83
    /**
84
     * @param $memberId
85
     *
86
     * @return UploadTicket
87
     * @throws \Exception
88
     */
89
    public function getUploadTicket($memberId)
90
    {
91
92
        $compiledUrl = self::BASE_URL.'?member_id='.$memberId;
93
94
        $response = $this->client->request('POST', $compiledUrl);
95
96
        $repositoryResponse = RepositoryResponse::fromResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
97
98
        if (!$repositoryResponse->isSuccessful()) {
99
            throw new \Exception('name me - not success');
100
        }
101
102
        if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'])) {
103
            throw new \Exception('name me - not index');
104
        }
105
106
        $uploadJob = UploadTicket::fromArray(
107
            $repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job']
108
        );
109
110
        return $uploadJob;
111
112
    }
113
114
    /**
115
     * @param UploadTicket $uploadJob
116
     *
117
     * @return UploadJobStatus $uploadJobStatus
118
     * @throws \Exception
119
     */
120
    public function getJobStatus(UploadTicket $uploadJob)
121
    {
122
123
        $compiledUrl = self::BASE_URL."?member_id={$uploadJob->getMemberId()}&job_id={$uploadJob->getJobId()}";
124
125
        $response = $this->client->request('GET', $compiledUrl);
126
127
        $repositoryResponse = RepositoryResponse::fromResponse($response);
0 ignored issues
show
Compatibility introduced by
$response of type object<Psr\Http\Message\ResponseInterface> is not a sub-type of object<GuzzleHttp\Psr7\Response>. It seems like you assume a concrete implementation of the interface Psr\Http\Message\ResponseInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
128
129
        if (!$repositoryResponse->isSuccessful()) {
130
            throw new \Exception('name me - not success');
131
        }
132
133
        if (!isset($repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0])) {
134
            throw new \Exception('name me - not index');
135
        }
136
137
        $uploadJobStatus = UploadJobStatus::fromArray(
138
            $repositoryResponse->getResponseAsArray()['response']['batch_segment_upload_job'][0]
139
        );
140
141
        return $uploadJobStatus;
142
143
    }
144
145
    /**
146
     * @return boolean
147
     */
148
    public function isCacheEnabled()
149
    {
150
        return $this->cacheEnabled;
151
    }
152
153
    public function disableCache()
154
    {
155
        $this->cacheEnabled = false;
156
    }
157
158
    public function enableCache()
159
    {
160
        $this->cacheEnabled = true;
161
    }
162
}
163