Completed
Push — master ( fa92b2...371532 )
by Thijs
01:32
created

ManagesPeople::personByEmail()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
dl 13
loc 13
rs 9.8333
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace TestMonitor\Custify\Actions;
4
5
use TestMonitor\Custify\Validator;
6
use TestMonitor\Custify\Resources\Person;
7
use TestMonitor\Custify\Transforms\TransformsPeople;
8
use TestMonitor\Custify\Exceptions\NotFoundException;
9
10
trait ManagesPeople
11
{
12
    use TransformsPeople;
13
14
    /**
15
     * Get a list of of people.
16
     *
17
     * @param int $page
18
     * @param int $limit
19
     *
20
     * @throws \TestMonitor\Custify\Exceptions\InvalidDataException
21
     * @return \TestMonitor\Custify\Resources\Person[]
22
     */
23 View Code Duplication
    public function people($page = 1, $limit = 10): array
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...
24
    {
25
        $response = $this->get('people/all', [
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
26
            'query' => ['itemsPerPage' => $limit, 'page' => $page],
27
        ]);
28
29
        return $this->fromCustifyPeople($response[0]['people']);
30
    }
31
32
    /**
33
     * Get a single person.
34
     *
35
     * @param string $id
36
     *
37
     * @throws \TestMonitor\Custify\Exceptions\InvalidDataException
38
     * @return \TestMonitor\Custify\Resources\Person
39
     */
40
    public function person(string $id): Person
41
    {
42
        $response = $this->get("people/{$id}");
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
43
44
        return $this->fromCustifyPerson($response);
45
    }
46
47
    /**
48
     * Get a single person by user id.
49
     *
50
     * @param string $userId
51
     *
52
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
53
     * @throws \TestMonitor\Custify\Exceptions\InvalidDataException
54
     * @return \TestMonitor\Custify\Resources\Person
55
     */
56 View Code Duplication
    public function personByUserId(string $userId): Person
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...
57
    {
58
        $response = $this->get("people?user_id={$userId}");
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
59
60
        Validator::keysExists($response[0], ['people']);
61
62
        // Simulate a not found response when the user_id does not exists.
63
        if (empty($response[0]['people'])) {
64
            throw new NotFoundException();
65
        }
66
67
        return $this->fromCustifyPerson($response[0]['people'][0]);
68
    }
69
70
    /**
71
     * Get a single person by email.
72
     *
73
     * @param string $email
74
     *
75
     * @throws \TestMonitor\Custify\Exceptions\NotFoundException
76
     * @throws \TestMonitor\Custify\Exceptions\InvalidDataException
77
     * @return \TestMonitor\Custify\Resources\Person
78
     */
79 View Code Duplication
    public function personByEmail(string $email): Person
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...
80
    {
81
        $response = $this->get("people?email={$email}");
0 ignored issues
show
Bug introduced by
It seems like get() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
82
83
        Validator::keysExists($response[0], ['people']);
84
85
        // Simulate a not found response when the user_id does not exists.
86
        if (empty($response[0]['people'])) {
87
            throw new NotFoundException();
88
        }
89
90
        return $this->fromCustifyPerson($response[0]['people'][0]);
91
    }
92
93
    /**
94
     * Create or update a person.
95
     *
96
     * @param \TestMonitor\Custify\Resources\Person $person
97
     *
98
     * @throws \TestMonitor\Custify\Exceptions\InvalidDataException
99
     * @return \TestMonitor\Custify\Resources\Person
100
     */
101
    public function createOrUpdatePerson(Person $person): Person
102
    {
103
        $response = $this->post('people', ['json' => $this->toCustifyPerson($person)]);
0 ignored issues
show
Bug introduced by
It seems like post() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
104
105
        return $this->fromCustifyPerson($response);
106
    }
107
}
108