Passed
Push — master ( 9ff894...5ceae2 )
by Soufiene
10:23 queued 08:00
created

Configurations::setUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace MedianetDev\PConnector\Concerns;
4
5
trait Configurations
6
{
7
    /**
8
     * @var bool
9
     *
10
     * Whether to use authentication header or not
11
     */
12
    private $withAuthentication;
13
14
    /**
15
     * @var bool
16
     *
17
     * The gateway profile
18
     */
19
    private $profile;
20
21
    /**
22
     * @var bool
23
     *
24
     * Should we log the request and the response or not
25
     */
26
    private $allowDebugging;
27
28
    /**
29
     * @var string
30
     *
31
     * Parse response as [string, object, array]
32
     */
33
    private $decodeResponse;
34
35
36
    /**
37
     * @var string
38
     *
39
     * The url for the current used profile.
40
     */
41
    private $url;
42
43
    private function updateSettings($profile)
44
    {
45
        $this->withAuthentication = config('p-connector.profiles.'.$profile.'.auth.authenticate_by_default', config('p-connector.auth.authenticate_by_default', false));
46
        $this->allowDebugging = config('p-connector.profiles.'.$profile.'.log', config('p-connector.log', false));
47
        $this->decodeResponse = config('p-connector.profiles.'.$profile.'.decode_response', config('p-connector.decode_response', false));
48
    }
49
50
    /**
51
     * Set the profile to use before sending the request.
52
     *
53
     * It's **RECOMMENDED** to use the profile before using any other setting function to not override any setting
54
     *
55
     * @param string $profile The profile name
56
     *
57
     * @return \MedianetDev\PConnector\PConnector
58
     */
59
    public function profile(string $profile)
60
    {
61
        if (! in_array($profile, array_keys(config('p-connector.profiles')))) {
62
            throw new InvalidArgumentException('The profile"'.$profile.'" does not exist!');
0 ignored issues
show
Bug introduced by
The type MedianetDev\PConnector\C...nvalidArgumentException was not found. Did you mean InvalidArgumentException? If so, make sure to prefix the type with \.
Loading history...
63
        }
64
        $this->profile = $profile;
0 ignored issues
show
Documentation Bug introduced by
The property $profile was declared of type boolean, but $profile is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
65
        $this->updateSettings($profile);
66
67
        return $this;
68
    }
69
70
    /**
71
     * Send language using the app locale through the Accept-Language header.
72
     *
73
     * @param string $locale [optional] The locale will default to the app.locale if not provided
74
     *
75
     * @return \MedianetDev\PConnector\PConnector
76
     */
77
    public function lang(string $locale = null)
78
    {
79
        $this->withHeader('Accept-Language', $locale ?? app()->getLocale());
0 ignored issues
show
Bug introduced by
It seems like withHeader() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $this->/** @scrutinizer ignore-call */ 
80
               withHeader('Accept-Language', $locale ?? app()->getLocale());
Loading history...
introduced by
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
        $this->withHeader('Accept-Language', $locale ?? app()->/** @scrutinizer ignore-call */ getLocale());
Loading history...
80
81
        return $this;
82
    }
83
84
    /**
85
     * Use authentication for this request.
86
     *
87
     * @return \MedianetDev\PConnector\PConnector
88
     */
89
    public function withAuth()
90
    {
91
        $this->withAuthentication = true;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Don't use authentication for this request.
98
     *
99
     * @return \MedianetDev\PConnector\PConnector
100
     */
101
    public function withoutAuth()
102
    {
103
        $this->withAuthentication = false;
104
105
        return $this;
106
    }
107
108
    /**
109
     * Log this request.
110
     *
111
     * @return \MedianetDev\PConnector\PConnector
112
     */
113
    public function withLog()
114
    {
115
        $this->allowDebugging = true;
116
117
        return $this;
118
    }
119
120
    /**
121
     * Don't log this request.
122
     *
123
     * @return \MedianetDev\PConnector\PConnector
124
     */
125
    public function withoutLog()
126
    {
127
        $this->allowDebugging = false;
128
129
        return $this;
130
    }
131
132
    /**
133
     * Parse the response as an object.
134
     *
135
     * @return \MedianetDev\PConnector\PConnector
136
     */
137
    public function objectResponse()
138
    {
139
        $this->decodeResponse = 'object';
140
141
        return $this;
142
    }
143
144
    /**
145
     * Parse the response as a string.
146
     *
147
     * @return \MedianetDev\PConnector\PConnector
148
     */
149
    public function htmlResponse()
150
    {
151
        $this->decodeResponse = 'string';
152
153
        return $this;
154
    }
155
156
    /**
157
     * Parse the response as an array.
158
     *
159
     * @return \MedianetDev\PConnector\PConnector
160
     */
161
    public function arrayResponse()
162
    {
163
        $this->decodeResponse = 'array';
164
165
        return $this;
166
    }
167
168
169
    /**
170
     * Change the url for the current used profile.
171
     *
172
     * @param string $url The new url
173
     *
174
     * @return \MedianetDev\PConnector\PConnector
175
     */
176
    public function url(string $url)
177
    {
178
        $this->url = $url;
179
180
        return $this;
181
    }
182
183
184
    /**
185
     * Change the url for the current used profile.
186
     * This is an alias for url() function
187
     *
188
     * @param string $url The new url
189
     *
190
     * @return \MedianetDev\PConnector\PConnector
191
     */
192
    public function setUrl(string $url)
193
    {
194
        return $this->url($url);
195
    }
196
}
197