Completed
Push — master ( 93b855...faa581 )
by Cesar
21s queued 10s
created

UserService   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 102
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A list() 0 6 1
A create() 0 9 2
A update() 0 17 2
A get() 0 8 2
A listTransferMethods() 0 8 2
A listBalances() 0 8 2
A getAuthenticationToken() 0 8 2
1
<?php
2
3
namespace App\Service\Hyperwallet;
4
5
use Exception;
6
use DateTime;
7
use Hyperwallet\Exception\HyperwalletApiException;
8
use Hyperwallet\Model\AuthenticationToken;
9
use Hyperwallet\Model\User;
10
use Hyperwallet\Response\ListResponse;
11
12
/**
13
 * Class UserService
14
 * @package App\Service\Hyperwallet
15
 */
16
class UserService extends AbstractHyperwalletService
17
{
18
    /**
19
     * @return ListResponse
20
     *
21
     * @throws HyperwalletApiException
22
     */
23
    public function list(): ListResponse
24
    {
25
        return $this->client->listUsers([
26
            'status' => ['ACTIVATED', 'PRE_ACTIVATED'],
27
        ]);
28
    }
29
30
    /**
31
     * @param array $userParameters
32
     * @return User | Exception
33
     */
34
    public function create(array $userParameters)
35
    {
36
        try {
37
            $user = new User($userParameters);
38
            return $this->client->createUser($user);
39
        } catch (Exception $exception) {
40
            return $exception;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $exception; (Exception) is incompatible with the return type documented by App\Service\Hyperwallet\UserService::create of type Hyperwallet\Model\User.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
41
        }
42
    }
43
44
    /**
45
     * @param array $userParameters
46
     * @return User | Exception
47
     */
48
    public function update(array $userParameters)
49
    {
50
        try {
51
            $user = new User($userParameters);
52
            $user
53
                ->setFirstName($userParameters['firstName'])
54
                ->setLastName($userParameters['lastName'])
55
                ->setEmail($userParameters['email'])
56
                ->setDateOfBirth(new Datetime($userParameters['dateOfBirth']))
57
                ->setClientUserId($userParameters['clientUserId'])
58
                ->setLanguage($userParameters['language'])
59
            ;
60
            return $this->client->updateUser($user);
61
        } catch (Exception $exception) {
62
            return $exception;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $exception; (Exception) is incompatible with the return type documented by App\Service\Hyperwallet\UserService::update of type Hyperwallet\Model\User.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
63
        }
64
    }
65
66
    /**
67
     * @param string $userToken
68
     * @return Exception|User
69
     */
70
    public function get(string $userToken)
71
    {
72
        try {
73
            return $this->client->getUser($userToken);
74
        } catch (Exception $exception) {
75
            return $exception;
76
        }
77
    }
78
79
    /**
80
     * @param string $userToken
81
     * @return Exception|ListResponse
82
     */
83
    public function listTransferMethods(string $userToken)
84
    {
85
        try {
86
            return $this->client->listTransferMethods($userToken);
87
        } catch (Exception $exception) {
88
            return $exception;
89
        }
90
    }
91
92
    /**
93
     * @param string $userToken
94
     * @return Exception|ListResponse
95
     */
96
    public function listBalances(string $userToken)
97
    {
98
        try {
99
            return $this->client->listBalancesForUser($userToken);
100
        } catch (Exception $exception) {
101
            return $exception;
102
        }
103
    }
104
105
    /**
106
     * @param string $userToken
107
     * @return Exception|AuthenticationToken
108
     */
109
    public function getAuthenticationToken(string $userToken)
110
    {
111
        try {
112
            return $this->client->getAuthenticationToken($userToken);
113
        } catch (Exception $exception) {
114
            return $exception;
115
        }
116
    }
117
}
118