GenderizeClient   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 1
b 0
f 0
dl 0
loc 127
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A country() 0 5 1
A buildQuery() 0 10 1
A names() 0 5 1
A get() 0 7 1
A __construct() 0 4 1
A name() 0 9 2
A lang() 0 5 1
1
<?php
2
3
namespace Pixelpeter\Genderize;
4
5
use Pixelpeter\Genderize\Models\GenderizeResponse;
6
use Unirest\Request;
7
8
class GenderizeClient
9
{
10
    /**
11
     * @var array $names Holds one or more names to check
12
     */
13
    protected $names;
14
15
    /**
16
     * @var string $lang The language used for the check
17
     */
18
    protected $lang;
19
20
    /**
21
     * @var string $country The country used for the check
22
     */
23
    protected $country;
24
25
    /**
26
     * @var string $apikey The api key used for the request
27
     */
28
    protected $apikey;
29
30
    /**
31
     * @var \Unirest\Request
32
     */
33
    protected $request;
34
35
    /**
36
     * @const API_URL The URL of the api endpoint
37
     */
38
    const API_URL = 'https://api.genderize.io';
39
40
41
    /**
42
     * Create new instance of Pixelpeter\Genderize\GenderizeClient
43
     */
44
    public function __construct(Request $request)
45
    {
46
        $this->apikey = config('genderize.apikey');
47
        $this->request = $request;
48
    }
49
50
    /**
51
     * Fluent setter for names given as string
52
     *
53
     * @param string $name
54
     * @return $this|GenderizeClient
55
     */
56
    public function name($name = '')
57
    {
58
        if (is_array($name)) {
0 ignored issues
show
introduced by
The condition is_array($name) is always false.
Loading history...
59
            return $this->names($name);
60
        }
61
62
        $this->names = [$name];
63
64
        return $this;
65
    }
66
67
    /**
68
     * Fluent setter for names given as array
69
     *
70
     * @param array $names
71
     * @return $this
72
     */
73
    public function names(array $names)
74
    {
75
        $this->names = $names;
76
77
        return $this;
78
    }
79
80
    /**
81
     * Fluent setter for language
82
     *
83
     * @param null $lang
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $lang is correct as it would always require null to be passed?
Loading history...
84
     * @return $this
85
     */
86
    public function lang($lang = null)
87
    {
88
        $this->lang = $lang;
89
90
        return $this;
91
    }
92
93
    /**
94
     * Fluent setter for country
95
     *
96
     * @param null $country
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $country is correct as it would always require null to be passed?
Loading history...
97
     * @return $this
98
     */
99
    public function country($country = null)
100
    {
101
        $this->country = $country;
102
103
        return $this;
104
    }
105
106
    /**
107
     * Send the request
108
     *
109
     * @return GenderizeResponse
110
     */
111
    public function get()
112
    {
113
        $headers = array('Accept' => 'application/json');
114
115
        $response = $this->request->get(self::API_URL, $headers, $this->buildQuery());
116
117
        return new GenderizeResponse($response);
118
    }
119
120
    /**
121
     * Build the query
122
     *
123
     * @return array
124
     */
125
    protected function buildQuery()
126
    {
127
        $query = [
128
            'name' => $this->names,
129
            'country_id' => $this->country,
130
            'language_id' => $this->lang,
131
            'apikey' => $this->apikey
132
        ];
133
134
        return array_filter($query);
135
    }
136
}
137