Completed
Push — master ( 2daeca...cab24a )
by Tyler
02:26
created

PeopleMatter   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 194
Duplicated Lines 16.49 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 88.89%

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 7
dl 32
loc 194
c 0
b 0
f 0
ccs 88
cts 99
cp 0.8889
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B hire() 0 42 3
A buildUrl() 0 4 1
A getBusinessUnits() 16 16 2
A getJobs() 16 16 2
A getPerson() 0 20 3
A login() 0 15 2
A getClient() 0 9 2
B request() 0 23 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Zenapply\PeopleMatter;
4
5
use DateTime;
6
use Exception;
7
use GuzzleHttp\Client;
8
use Zenapply\PeopleMatter\Exceptions\PeopleMatterException;
9
use Zenapply\PeopleMatter\Models\BusinessUnit;
10
use Zenapply\PeopleMatter\Models\Job;
11
use Zenapply\PeopleMatter\Models\Person;
12
13
class PeopleMatter
14
{
15
    protected $alias;
16
    protected $authenticated = false;
17
    protected $client;
18
    protected $host;
19
    protected $password;
20
    protected $username;
21
22
    /**
23
     * Creates a PeopleMatter instance that can register and unregister webhooks with the API
24
     * @param string      $username The Username
25
     * @param string      $password The Password
26
     * @param string      $alias    The business alias
27
     * @param string      $host     The host to connect to
28
     * @param Client|null $client   The Guzzle client (used for testing)
29
     */
30 15
    public function __construct($username, $password, $alias, $host = "api.peoplematter.com", Client $client = null)
31
    {
32 15
        $this->alias = $alias;
33 15
        $this->client = $client;
34 15
        $this->host = $host;
35 15
        $this->password = $password;
36 15
        $this->username = $username;
37 15
    }
38
39 3
    public function hire(Person $person, Job $job, BusinessUnit $businessUnit, $timeStatus, DateTime $hired_at = null)
40
    {
41 3
        $this->login();
42
43 3
        if ($hired_at === null) {
44
            $hired_at = new DateTime("now");
45
        }
46
47 3
        if (!in_array($timeStatus, ["FullTime", "PartTime"])) {
48
            throw new Exception("{$timeStatus} is invalid! Please use FullTime or PartTime");
49
        }
50
51 3
        $url = "https://{$this->host}/api/services/platform/hireemployee";
52
53 3
        return $this->request("POST", $url, [
54
            "json" => [
55 3
                "HireDate" => $hired_at->format("m/d/Y"),
56
                "Business" => [
57 3
                    "Alias" => $this->alias,
58 3
                ],
59
                "BusinessUnit" => [
60 3
                    "UnitNumber" => $businessUnit->UnitNumber
0 ignored issues
show
Bug introduced by
The property UnitNumber does not seem to exist in Zenapply\PeopleMatter\Models\BusinessUnit.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
61 3
                ],
62 3
                "Person" => $person->toArray(),
63
                "JobPositions" => [
64
                    [
65
                        "Business" => [
66 3
                            "Alias" => $this->alias,
67 3
                        ],
68
                        "BusinessUnit" => [
69 3
                            "UnitNumber" => $businessUnit->UnitNumber
70 3
                        ],
71
                        "Job" => [
72 3
                            "Code" => $job->Code,
0 ignored issues
show
Bug introduced by
The property Code does not seem to exist in Zenapply\PeopleMatter\Models\Job.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
73 3
                        ],
74 3
                        "TimeStatus" => $timeStatus,
75 3
                        "Person" => $person->toArray(),
76
                    ]
77 3
                ]
78 3
            ]
79 3
        ]);
80
    }
81
82
    /**
83
     * @return string
84
     */
85 9
    protected function buildUrl($resource)
86
    {
87 9
        return "https://{$this->host}/api/{$resource}";
88
    }
89
90 3 View Code Duplication
    public function getBusinessUnits()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92 3
        $this->login();
93 3
        $response = $this->request("GET", $this->buildUrl("businessunit"), [
94
            "query" => [
95 3
                "businessalias" => $this->alias,
96
            ]
97 3
        ]);
98
99 3
        $units = [];
100 3
        foreach ($response["Records"] as $unit) {
101 3
            $units[] = new BusinessUnit($unit);
102 3
        }
103
104 3
        return $units;
105
    }
106
107 3 View Code Duplication
    public function getJobs()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
108
    {
109 3
        $this->login();
110 3
        $response = $this->request("GET", $this->buildUrl("job"), [
111
            "query" => [
112 3
                "businessalias" => $this->alias,
113
            ]
114 3
        ]);
115
116 3
        $jobs = [];
117 3
        foreach ($response["Jobs"] as $unit) {
118 3
            $jobs[] = new Job($unit);
119 3
        }
120
121 3
        return $jobs;
122
    }
123
124 3
    public function getPerson($email)
125
    {
126 3
        if (empty($email)) {
127
            throw new Exception("Email is invalid!");
128 1
        }
129 3
        $this->login();
130 3
        $units = [];
131 3
        $response = $this->request("GET", $this->buildUrl("businessunitemployee"), [
132
            "query" => [
133 3
                "businessalias" => $this->alias,
134 3
                "PersonEmailAddress" => $email,
135
            ]
136 3
        ]);
137
138 3
        foreach ($response["Records"] as $unit) {
139 3
            $units[] = new Person($unit);
140 3
        }
141
142 3
        return $units;
143
    }
144
145
146 12
    protected function login()
147
    {
148 12
        if ($this->authenticated !== true) {
149 12
            $url = "https://{$this->host}/api/account/login";
150 12
            $this->request("POST", $url, [
151
                "form_params" => [
152 12
                    "email" => $this->username,
153 12
                    "password" => $this->password,
154
                ]
155 12
            ]);
156 12
            $this->authenticated = true;
157 12
        }
158
159 12
        return $this->authenticated;
160
    }
161
162
    /**
163
     * Returns the Client instance
164
     * @return Client
165
     */
166 12
    public function getClient()
167
    {
168 12
        if (!$this->client instanceof Client) {
169
            $this->client = new Client([
170
                "cookies" => true
171
            ]);
172
        }
173 12
        return $this->client;
174
    }
175
    
176
    /**
177
     * Executes a request to the PeopleMatter API
178
     * @param  string $method  The request type
179
     * @param  string $url     The url to request
180
     * @param  array  $options An array of options for the request
181
     * @return array           The response as an array
182
     */
183 12
    protected function request($method, $url, $options = [])
184
    {
185 12
        $client = $this->getClient();
186
        try {
187 12
            $response = $client->request($method, $url, $options);
188 12
        } catch (\GuzzleHttp\Exception\ClientException $e) {
189
            $response = $e->getResponse();
190
            throw new PeopleMatterException($response->getStatusCode().": ".$response->getReasonPhrase(), 1);
191
        }
192
193 12
        $body = $response->getBody();
194 12
        if (!is_array($body)) {
195 12
            $json = json_decode($body, true);
196 12
        } else {
197
            $json = $body;
198
        }
199
200 12
        if (!empty($json["ErrorMessage"])) {
201
            throw new PeopleMatterException($json["ErrorMessage"], $json["ErrorCode"]);
202
        }
203
204 12
        return $json;
205
    }
206
}
207