Completed
Push — master ( 11a393...2daeca )
by Tyler
04:34
created

PeopleMatter   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 177
Duplicated Lines 13.56 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 90.8%

Importance

Changes 0
Metric Value
wmc 19
lcom 1
cbo 7
dl 24
loc 177
ccs 79
cts 87
cp 0.908
rs 10
c 0
b 0
f 0

9 Methods

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

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
10
class PeopleMatter
11
{
12
    protected $alias;
13
    protected $authenticated = false;
14
    protected $client;
15
    protected $host;
16
    protected $password;
17
    protected $username;
18
19
    /**
20
     * Creates a PeopleMatter instance that can register and unregister webhooks with the API
21
     * @param string      $username The Username
22
     * @param string      $password The Password
23
     * @param string      $alias    The business alias
24
     * @param string      $host     The host to connect to
25
     * @param Client|null $client   The Guzzle client (used for testing)
26
     */
27 15
    public function __construct($username, $password, $alias, $host = "api.peoplematter.com", Client $client = null)
28
    {
29 15
        $this->alias = $alias;
30 15
        $this->client = $client;
31 15
        $this->host = $host;
32 15
        $this->password = $password;
33 15
        $this->username = $username;
34 15
    }
35
36 3
    public function hire(Person $person, Job $job, BusinessUnit $businessUnit, $timeStatus, DateTime $hired_at = null)
37
    {
38 3
        $this->login();
39
40 3
        if ($hired_at === null) {
41
            $hired_at = new DateTime('now');
42
        }
43
44 3
        if (!in_array($timeStatus, ["FullTime", "PartTime"])) {
45
            throw new Exception("{$timeStatus} is invalid! Please use FullTime or PartTime");
46
        }
47
48 3
        $url = "https://{$this->host}/api/services/platform/hireemployee";
49
50 3
        return $this->request('POST', $url, [
51 3
            'debug' => true,
52
            'json' => [
53 3
                "HireDate" => $hired_at->format("m/d/Y"),
54
                "Business" => [
55
                    "Alias" => "cafezupassandbox"
56 3
                ],
57
                "BusinessUnit" => [
58 3
                    "UnitNumber" => $businessUnit->UnitNumber
0 ignored issues
show
Bug introduced by
The property UnitNumber does not seem to exist in Zenapply\PeopleMatter\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...
59 3
                ],
60 3
                "Person" => $person->toArray(),
61
                "JobPositions" => [
62
                    [
63
                        "Business" => [
64
                            "Alias" => "cafezupassandbox"
65 3
                        ],
66
                        "BusinessUnit" => [
67 3
                            "UnitNumber" => $businessUnit->UnitNumber
68 3
                        ],
69
                        "Job" => [
70 3
                            "Code" => $job->Code,
0 ignored issues
show
Bug introduced by
The property Code does not seem to exist in Zenapply\PeopleMatter\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...
71 3
                        ],
72 3
                        "TimeStatus" => $timeStatus,
73 3
                        "Person" => $person->toArray(),
74
                    ]
75 3
                ]
76 3
            ]
77 3
        ]);
78
    }
79
80
    /**
81
     * @return string
82
     */
83 6
    protected function buildUrl($resource)
84
    {
85 6
        return "https://{$this->host}/api/{$resource}";
86
    }
87
88 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...
89
    {
90 3
        $this->login();
91 3
        $response = $this->request('GET', $this->buildUrl("businessunit?businessalias={$this->alias}"));
92
93 3
        $units = [];
94 3
        foreach ($response["Records"] as $unit) {
95 3
            $units[] = new BusinessUnit($unit);
96 3
        }
97
98 3
        return $units;
99
    }
100
101 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...
102
    {
103 3
        $this->login();
104 3
        $response = $this->request('GET', $this->buildUrl("job?businessalias={$this->alias}"));
105
106 3
        $jobs = [];
107 3
        foreach ($response["Jobs"] as $unit) {
108 3
            $jobs[] = new Job($unit);
109 3
        }
110
111 3
        return $jobs;
112
    }
113
114 3
    public function getPerson($email)
115
    {
116 3
        if (empty($email)) {
117
            throw new Exception("Email is invalid!");
118
        }
119 3
        $this->login();
120 3
        $units = [];
121 3
        $url = "https://{$this->host}/api/businessunitemployee?businessalias={$this->alias}&PersonEmailAddress={$email}";
122 3
        $response = $this->request('GET', $url);
123
124 3
        foreach ($response["Records"] as $unit) {
125 3
            $units[] = new Person($unit);
126 3
        }
127
128 3
        return $units;
129
    }
130
131 12
    protected function login()
132
    {
133 12
        if ($this->authenticated !== true) {
134 12
            $url = "https://{$this->host}/api/account/login";
135 12
            $this->request('POST', $url, [
136
                'form_params' => [
137 12
                    'email' => $this->username,
138 12
                    'password' => $this->password,
139
                ]
140 12
            ]);
141 12
            $this->authenticated = true;
142 12
        }
143
144 12
        return $this->authenticated;
145
    }
146
147
    /**
148
     * Returns the Client instance
149
     * @return Client
150
     */
151 12
    public function getClient()
152
    {
153 12
        if (!$this->client instanceof Client) {
154
            $this->client = new Client([
155
                'cookies' => true
156
            ]);
157
        }
158 12
        return $this->client;
159
    }
160
    
161
    /**
162
     * Executes a request to the PeopleMatter API
163
     * @param  string $method  The request type
164
     * @param  string $url     The url to request
165
     * @param  array  $options An array of options for the request
166
     * @return array           The response as an array
167
     */
168 12
    protected function request($method, $url, $options = [])
169
    {
170 12
        $client = $this->getClient();
171
        try {
172 12
            $response = $client->request($method, $url, $options);
173 12
        } catch (\GuzzleHttp\Exception\ClientException $e) {
174 3
            $response = $e->getResponse();
175 3
            throw new PeopleMatterException($response->getStatusCode().": ".$response->getReasonPhrase(), 1);
176
        }
177
178 12
        $json = json_decode($response->getBody(), true);
179
180 12
        if (!empty($json["ErrorMessage"])) {
181
            throw new PeopleMatterException($json["ErrorMessage"], $json["ErrorCode"]);
182
        }
183
184 12
        return $json;
185
    }
186
}
187