WebinarJam::addToWebinar()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 10
c 3
b 0
f 0
dl 0
loc 24
rs 9.9332
cc 1
nc 1
nop 8

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
namespace joseayram;
3
4
/**
5
 *
6
 * Implements the WebinarJam API
7
 *
8
 */
9
class WebinarJam
10
{
11
    protected const API_URL = 'https://webinarjam.genndi.com/api/';
12
13
    protected const CURL_OPTIONS = [
14
        CURLOPT_CONNECTTIMEOUT => 10,
15
        CURLOPT_RETURNTRANSFER => true,
16
        CURLOPT_TIMEOUT        => 60,
17
    ];
18
19
    /**
20
     * WebinarJam API Key
21
     * @var string
22
     */
23
    protected $apiKey;
24
25
    /**
26
     * WebinarJam Class Constructor
27
     *
28
     * @param  string $apiKey WebinarJam API Key
29
     */
30
    public function __construct(string $apiKey)
31
    {
32
        $this->apiKey = $apiKey;
33
    }
34
35
    /**
36
     * Call to API URL through curl extension
37
     *
38
     * @param  string $endpoint WebinarJam API EndPoint
39
     * @return array $jsonResults
40
     */
41
    protected function authenticatedCall(string $endpoint, array $params = []): array
42
    {
43
        $curl = curl_init(self::API_URL . $endpoint);
44
45
        if (empty($this->apiKey)) {
46
            throw new \Exception('You must specify a valid WebinarJam API Key');
47
        }
48
49
        if (is_resource($curl)) {
50
            $params['api_key'] = $this->apiKey;
51
            curl_setopt_array($curl, self::CURL_OPTIONS);
52
            curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
53
            $result = curl_exec($curl);
54
55
            if (false === $result) {
56
                $error = curl_error($curl);
57
                curl_close($curl);
58
                throw new \Exception($error);
59
            }
60
61
            curl_close($curl);
62
63
            $jsonResults = is_string($result) ? json_decode($result, true) : null;
64
65
            if (!is_array($jsonResults)) {
66
                throw new \Exception($result);
0 ignored issues
show
Bug introduced by
It seems like $result can also be of type true; however, parameter $message of Exception::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
                throw new \Exception(/** @scrutinizer ignore-type */ $result);
Loading history...
67
            }
68
69
            return $jsonResults;
70
        }
71
72
        return [];
73
    }
74
75
    /**
76
     * Retrieve a full list of all webinars published in your account
77
     * @return array Webinar List
78
     */
79
    public function getWebinars(): ?array
80
    {
81
        return $this->authenticatedCall('webinars');
82
    }
83
84
    /**
85
     * Get details about one particular webinar from your account
86
     *
87
     * @param  string $webinarId Webinar ID
88
     * @return array $webinar Webinar Details
89
     */
90
    public function getWebinar(string $webinarId): ?array
91
    {
92
        return $this->authenticatedCall('webinar', ['webinar_id' => $webinarId]);
93
    }
94
95
    /**
96
     * Register a person to a specific webinar
97
     *
98
     * @param string      $webinarId   Webinar ID (required)
99
     * @param string      $first_name  First Name (required)
100
     * @param string      $email       E-mail (required)
101
     * @param int         $schedule    Schedule (required)
102
     * @param string|null $last_name   Last Name (optional)
103
     * @param string|null $ipAddress   IP Address (optional)
104
     * @param int|null $countryCode Country Code (optional)
105
     * @param int|null $phone       Phone (optional)
106
     * @return array $webinar Webinar Details
107
     */
108
    public function addToWebinar(
109
        string $webinarId,
110
        string $first_name,
111
        string $email,
112
        int $schedule = 0,
113
        string $last_name = null,
114
        string $ipAddress = null,
115
        int $countryCode = null,
116
        int $phone = null
117
    ): array
118
    {
119
        $params = [
120
            'webinar_id' => $webinarId,
121
            'first_name' => $first_name,
122
            'email' => $email,
123
            'schedule' => $schedule
124
        ];
125
126
        $params['last_name'] = $last_name ?? null;
127
        $params['ip_address'] = $ipAddress ?? null;
128
        $params['phone_country_code'] = $countryCode ?? null;
129
        $params['phone'] = $phone ?? null;
130
131
        return $this->authenticatedCall('register', $params);
132
    }
133
}
134