Completed
Push — v2 ( 6101cd...f8a429 )
by Beñat
02:32
created

UserApi::all()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
rs 9.4286
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Stack Exchange Api Client library.
5
 *
6
 * Copyright (c) 2014-2016 Beñat Espiña <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace BenatEspina\StackExchangeApiClient\Api;
13
14
use BenatEspina\StackExchangeApiClient\Authentication\Authentication;
15
use BenatEspina\StackExchangeApiClient\Http\Http;
16
use BenatEspina\StackExchangeApiClient\Model\User;
17
use BenatEspina\StackExchangeApiClient\Serializer\UserSerializer;
18
19
/**
20
 * The user api class.
21
 *
22
 * @author Beñat Espiña <[email protected]>
23
 */
24
final class UserApi
25
{
26
    const URL = 'users/';
27
    const QUERY_PARAMS = [
28
        'order'  => 'desc',
29
        'sort'   => 'activity',
30
        'site'   => 'stackoverflow',
31
        'filter' => Http::FILTER_ALL,
32
    ];
33
34
    /**
35
     * The authentication.
36
     *
37
     * @var Authentication|null
38
     */
39
    private $authentication;
40
41
    /**
42
     * Constructor.
43
     *
44
     * @param Authentication|null $anAuthentication The authentication
45
     */
46
    public function __construct(Authentication $anAuthentication = null)
47
    {
48
        $this->authentication = $anAuthentication;
49
    }
50
51
    /**
52
     * Returns all users on a site.
53
     *
54
     * More info: https://api.stackexchange.com/docs/users
55
     *
56
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
57
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
58
     *
59
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
60
     */
61 View Code Duplication
    public function all($params = self::QUERY_PARAMS, $serialize = true)
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...
62
    {
63
        $response = Http::instance()->get(
64
            self::URL, $params
65
        );
66
67
        return $serialize === true ? UserSerializer::serialize($response) : $response;
68
    }
69
70
    /**
71
     * Gets the users identified in ids in {ids}.
72
     *
73
     * More info: https://api.stackexchange.com/docs/users-by-ids
74
     *
75
     * @param string|array $ids       Array which contains the ids delimited by semicolon, or a simple id
76
     * @param array        $params    QueryString parameter(s)
77
     * @param bool         $serialize Checks if the result will be serialize or not, by default is true
78
     *
79
     * @return array|User
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
80
     */
81 View Code Duplication
    public function getOfIds($ids, array $params = self::QUERY_PARAMS, $serialize = true)
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...
82
    {
83
        $response = Http::instance()->get(
84
            self::URL . (is_array($ids) ? implode(';', $ids) : $ids), $params
85
        );
86
87
        return $serialize === true ? UserSerializer::serialize($response) : $response;
88
    }
89
90
    /**
91
     * Returns the user associated with the passed access_token.
92
     *
93
     * More info: https://api.stackexchange.com/docs/me
94
     *
95
     * @param array $params    QueryString parameter(s)
96
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
97
     *
98
     * @throws \Exception when the auth is null
99
     *
100
     * @return User
101
     */
102 View Code Duplication
    public function me(array $params = self::QUERY_PARAMS, $serialize = true)
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...
103
    {
104
        if (!$this->authentication instanceof Authentication) {
105
            throw new \Exception('Authentication is required');
106
        }
107
        $response = Http::instance()->get(
108
            'me', array_merge($params, ['access_token' => $this->authentication->accessToken()])
109
        );
110
111
        return $serialize === true ? UserSerializer::serialize($response) : $response;
0 ignored issues
show
Bug Compatibility introduced by
The expression $serialize === true ? \B...$response) : $response; of type array|BenatEspina\StackE...geApiClient\Model\Model adds the type array to the return on line 111 which is incompatible with the return type documented by BenatEspina\StackExchangeApiClient\Api\UserApi::me of type BenatEspina\StackExchangeApiClient\Model\User.
Loading history...
112
    }
113
114
    /**
115
     * Gets those users on a site who can exercise moderation powers.
116
     *
117
     * More info: https://api.stackexchange.com/docs/moderators
118
     *
119
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
120
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
121
     *
122
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
123
     */
124 View Code Duplication
    public function moderators(array $params = [], $serialize = true)
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...
125
    {
126
        $response = Http::instance()->get(
127
            self::URL . 'moderators', $params
128
        );
129
130
        return $serialize === true ? UserSerializer::serialize($response) : $response;
131
    }
132
133
    /**
134
     * Returns those users on a site who both have moderator powers, and were actually elected.
135
     *
136
     * More info: https://api.stackexchange.com/docs/elected-moderators
137
     *
138
     * @param array $params    QueryString parameter(s), it admits page and pagesize; by default is null
139
     * @param bool  $serialize Checks if the result will be serialize or not, by default is true
140
     *
141
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|\BenatEspina\Stack...geApiClient\Model\Model? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
142
     */
143 View Code Duplication
    public function electedModerators(array $params = [], $serialize = true)
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...
144
    {
145
        $response = Http::instance()->get(
146
            self::URL . 'moderators/elected', $params
147
        );
148
149
        return $serialize === true ? UserSerializer::serialize($response) : $response;
150
    }
151
}
152