Passed
Push — master ( 6bc3ab...6ca7b9 )
by Soufiene
03:25
created

Configurations::lang()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 5
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
    private function updateSettings($profile)
36
    {
37
        $this->withAuthentication = config('p-connector.profiles.'.$profile.'.auth.authenticate_by_default', config('p-connector.auth.authenticate_by_default', false));
38
        $this->allowDebugging = config('p-connector.profiles.'.$profile.'.log', config('p-connector.log', false));
39
        $this->decodeResponse = config('p-connector.profiles.'.$profile.'.decode_response', config('p-connector.decode_response', false));
40
    }
41
42
    /**
43
     * Set the profile to use before sending the request.
44
     *
45
     * It's **RECOMMENDED** to use the profile before using any other setting function to not override any setting
46
     *
47
     * @param string $profile The profile name
48
     *
49
     * @return \MedianetDev\PConnector\PConnector
50
     */
51
    public function profile(string $profile)
52
    {
53
        if (! in_array($profile, array_keys(config('p-connector.profiles')))) {
54
            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...
55
        }
56
        $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...
57
        $this->updateSettings($profile);
58
59
        return $this;
60
    }
61
62
    /**
63
     * Send language using the app locale through the Accept-Language header.
64
     *
65
     * @param string $locale [optional] The locale will default to the app.locale if not provided
66
     *
67
     * @return \MedianetDev\PConnector\PConnector
68
     */
69
    public function lang(string $locale = null)
70
    {
71
        $this->withHeader('Accept-Language', $locale ?? app()->getLocale());
0 ignored issues
show
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

71
        $this->withHeader('Accept-Language', $locale ?? app()->/** @scrutinizer ignore-call */ getLocale());
Loading history...
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

71
        $this->/** @scrutinizer ignore-call */ 
72
               withHeader('Accept-Language', $locale ?? app()->getLocale());
Loading history...
72
73
        return $this;
74
    }
75
76
    /**
77
     * Use authentication for this request.
78
     *
79
     * @return \MedianetDev\PConnector\PConnector
80
     */
81
    public function withAuth()
82
    {
83
        $this->withAuthentication = true;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Don't use authentication for this request.
90
     *
91
     * @return \MedianetDev\PConnector\PConnector
92
     */
93
    public function withoutAuth()
94
    {
95
        $this->withAuthentication = false;
96
97
        return $this;
98
    }
99
100
    /**
101
     * Log this request.
102
     *
103
     * @return \MedianetDev\PConnector\PConnector
104
     */
105
    public function withLog()
106
    {
107
        $this->allowDebugging = true;
108
109
        return $this;
110
    }
111
112
    /**
113
     * Don't log this request.
114
     *
115
     * @return \MedianetDev\PConnector\PConnector
116
     */
117
    public function withoutLog()
118
    {
119
        $this->allowDebugging = false;
120
121
        return $this;
122
    }
123
124
    /**
125
     * Parse the response as an object.
126
     *
127
     * @return \MedianetDev\PConnector\PConnector
128
     */
129
    public function objectResponse()
130
    {
131
        $this->decodeResponse = 'object';
132
133
        return $this;
134
    }
135
136
    /**
137
     * Parse the response as a string.
138
     *
139
     * @return \MedianetDev\PConnector\PConnector
140
     */
141
    public function htmlResponse()
142
    {
143
        $this->decodeResponse = 'string';
144
145
        return $this;
146
    }
147
148
    /**
149
     * Parse the response as an array.
150
     *
151
     * @return \MedianetDev\PConnector\PConnector
152
     */
153
    public function arrayResponse()
154
    {
155
        $this->decodeResponse = 'array';
156
157
        return $this;
158
    }
159
}
160