EmailFinder::make()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.5866
c 0
b 0
f 0
cc 7
nc 4
nop 0
1
<?php
2
3
namespace Messerli90\Hunterio;
4
5
use Illuminate\Support\Facades\Http;
6
use Messerli90\Hunterio\Exceptions\InvalidRequestException;
7
use Messerli90\Hunterio\Interfaces\EndpointInterface;
8
9
class EmailFinder extends HunterClient
10
{
11
    /**
12
     * Domain name from which you want to find the email addresses
13
     *
14
     * @var string
15
     */
16
    public $domain;
17
18
    /**
19
     * The company name from which you want to find the email addresses
20
     *
21
     * @var string
22
     */
23
    public $company;
24
25
    /**
26
     * The person's full name
27
     *
28
     * @var string
29
     */
30
    public $full_name;
31
32
    /**
33
     * The person's first name
34
     *
35
     * @var string
36
     */
37
    public $first_name;
38
39
    /**
40
     * The person's last name
41
     *
42
     * @var string
43
     */
44
    public $last_name;
45
46
    public function __construct(string $api_key = null)
47
    {
48
        parent::__construct($api_key);
49
50
        $this->endpoint = 'email-finder';
51
    }
52
53
    /**
54
     * Sets domain to search
55
     *
56
     * @param string $domain
57
     * @return EmailFinder
58
     */
59
    public function domain(string $domain): self
60
    {
61
        $this->domain = $domain;
62
63
        return $this;
64
    }
65
66
    /**
67
     * Set company name to search
68
     *
69
     * @param string $company
70
     * @return EmailFinder
71
     */
72
    public function company(string $company): self
73
    {
74
        $this->company = $company;
75
76
        return $this;
77
    }
78
79
    /**
80
     * Set the person's name to search
81
     * Sets `full_name` by passing in a single argument, or
82
     * sets `first_name` and `last_name` when passing two arguments
83
     *
84
     * Note that you'll get better results by supplying the person's first and last name if you can.
85
     *
86
     * @param string $first_name
87
     * @param string|null $last_name
88
     * @return EmailFinder
89
     */
90
    public function name(string $first_name, string $last_name = null): self
91
    {
92
        if ($last_name === null) {
93
            $this->full_name = implode("+", explode(" ", $first_name));
94
            $this->first_name = null;
95
            $this->last_name = null;
96
        } else {
97
            $this->full_name = null;
98
            $this->first_name = $first_name;
99
            $this->last_name = $last_name;
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * Build query with set attributes
107
     *
108
     * @return string
109
     * @throws InvalidRequestException
110
     */
111
    public function make()
112
    {
113
        if (empty($this->company) && empty($this->domain)) {
114
            throw new InvalidRequestException('Either Domain or Company fields are required.');
115
        }
116
        if (empty($this->full_name) && (empty($this->first_name) || empty($this->last_name))) {
117
            throw new InvalidRequestException('Either full name or first and last name fields are required.');
118
        }
119
120
        $this->query_params = [
121
            'company' => $this->company ?? null,
122
            'domain' => $this->domain ?? null,
123
            'api_key' => $this->api_key ?? null
124
        ];
125
126
        if ($this->full_name) {
127
            $this->query_params = array_merge($this->query_params, ['full_name' => $this->full_name]);
128
        } else {
129
            $this->query_params = array_merge($this->query_params, [
130
                'first_name' => $this->first_name, 'last_name' => $this->last_name
131
            ]);
132
        }
133
134
        return $this->query_params;
135
    }
136
}
137