Completed
Push — master ( 6344d3...86aff3 )
by Tyler
02:06
created

PeopleMatter::getBusinessUnits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 13
loc 13
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.4285
cc 2
eloc 8
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Zenapply\PeopleMatter;
4
5
use GuzzleHttp\Client;
6
use Zenapply\PeopleMatter\Exceptions\PeopleMatterException;
7
use DateTime;
8
9
class PeopleMatter
10
{
11
    const V3 = 'v3';
12
13
    protected $host;
14
    protected $version;
15
    protected $client;
16
    protected $token;
17
    protected $authenticated = false;
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 12
    public function __construct($username, $password, $alias, $host = "api.peoplematter.com", Client $client = null)
28
    {
29 12
        $this->alias = $alias;
0 ignored issues
show
Bug introduced by
The property alias does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
30 12
        $this->username = $username;
0 ignored issues
show
Bug introduced by
The property username does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
31 12
        $this->password = $password;
0 ignored issues
show
Bug introduced by
The property password does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32 12
        $this->host = $host;
33 12
        $this->client = $client;
34 12
    }
35
36 3
    public function hire(Person $person, Job $job, BusinessUnit $businessUnit, DateTime $hired_at = null, $timeStatus = "FullTime")
37
    {
38 3
        $this->login();
39
40 3
        if ($hired_at === null) {
41
            $hired_at = new DateTime('now');
42
        }
43
44 3
        $url = "https://{$this->host}/api/services/platform/hireemployee";
45
46 3
        return $this->request('POST', $url, [
47 3
            'debug' => true,
48
            'json' => [
49 3
                "HireDate" => $hired_at->format("m/d/Y"),
50
                "Business" => [
51
                    "Alias" => "cafezupassandbox"
52 3
                ],
53
                "BusinessUnit" => [
54 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...
55 3
                ],
56 3
                "Person" => $person->toArray(),
57
                "JobPositions" => [
58
                    [
59
                        "Business" => [
60
                            "Alias" => "cafezupassandbox"
61 3
                        ],
62
                        "BusinessUnit" => [
63 3
                            "UnitNumber" => $businessUnit->UnitNumber
64 3
                        ],
65
                        "Job" => [
66 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...
67 3
                        ],
68 3
                        "TimeStatus" => $timeStatus,
69 3
                        "Person" => $person->toArray(),
70
                    ]
71 3
                ]
72 3
            ]
73 3
        ]);
74
    }
75
76 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...
77
    {
78 3
        $this->login();
79 3
        $units = [];
80 3
        $url = "https://{$this->host}/api/businessunit?businessalias={$this->alias}";
81 3
        $response = $this->request('GET', $url);
82
83 3
        foreach ($response["Records"] as $unit) {
84 3
            $units[] = new BusinessUnit($unit);
85 3
        }
86
87 3
        return $units;
88
    }
89
90
    // public function getPerson($email)
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
91
    // {
92
    //     if (empty($email)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
93
    //         throw new \Exception("Email is invalid!");
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
94
    //     }
95
    //     $this->login();
0 ignored issues
show
Unused Code Comprehensibility introduced by
72% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
96
    //     $units = [];
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
97
    //     $url = "https://{$this->host}/api/employees/list";
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
98
    //     $response = $this->request('GET', $url);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
99
    //     var_dump($response);
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
100
    //     foreach ($response["Records"] as $unit) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
101
    //         $units[] = new BusinessUnit($unit);
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
102
    //     }
103
104
    //     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
        $jobs = [];
111 3
        $url = "https://{$this->host}/api/job?businessalias={$this->alias}";
112 3
        $response = $this->request('GET', $url);
113
114 3
        foreach ($response["Jobs"] as $unit) {
115 3
            $jobs[] = new Job($unit);
116 3
        }
117
118 3
        return $jobs;
119
    }
120
121 9
    protected function login()
122
    {
123 9
        if ($this->authenticated !== true) {
124 9
            $url = "https://{$this->host}/api/account/login";
125 9
            $this->request('POST', $url, [
126 1
                'form_params' => [
127 9
                    'email' => $this->username,
128 9
                    'password' => $this->password,
129
                ]
130 9
            ]);
131 9
            $this->authenticated = true;
132 9
        }
133
134 9
        return $this->authenticated;
135
    }
136
137
    /**
138
     * Returns the Client instance
139
     * @return Client
140
     */
141 9
    public function getClient()
142
    {
143 9
        if (!$this->client instanceof Client) {
144
            $this->client = new Client([
145
                'cookies' => true
146
            ]);
147
        }
148 9
        return $this->client;
149
    }
150
    
151
    /**
152
     * Executes a request to the PeopleMatter API
153
     * @param  string $method  The request type
154
     * @param  string $url     The url to request
155
     * @param  array  $options An array of options for the request
156
     * @return array           The response as an array
157
     */
158 9
    protected function request($method, $url, $options = [])
159
    {
160 9
        $client = $this->getClient();
161
        try {
162 9
            $response = $client->request($method, $url, $options);
163 9
        } catch (\GuzzleHttp\Exception\ClientException $e) {
164 3
            $response = $e->getResponse();
165 3
            throw new PeopleMatterException($response->getStatusCode().": ".$response->getReasonPhrase(), 1);
166
        }
167
168 9
        $json = json_decode($response->getBody(), true);
169
170 9
        if (!empty($json["ErrorMessage"])) {
171
            throw new PeopleMatterException($json["ErrorMessage"], $json["ErrorCode"]);
172
        }
173
174 9
        return $json;
175
    }
176
}
177