• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

NIT-Administrative-Systems / SysDev-laravel-soa / 7805057844

06 Feb 2024 07:28PM UTC coverage: 45.675% (-0.2%) from 45.913%
7805057844

push

github

web-flow
Housekeeping (#162)

11 of 35 new or added lines in 14 files covered. (31.43%)

9 existing lines in 7 files now uncovered.

264 of 578 relevant lines covered (45.67%)

14.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.82
/src/DirectorySearch.php
1
<?php
2

3
namespace Northwestern\SysDev\SOA;
4

5
use GuzzleHttp;
6

7
/**
8
 * Bindings for NU's DirectorySearch SOA service.
9
 *
10
 * For documentation, see <https://northwestern-apiportal.apigee.io/apis/directory-search-expanded/index>.
11
 */
12
class DirectorySearch
13
{
14
    protected string $baseUrl;
15

16
    protected string $apiKey;
17

18
    protected ?string $lastError = null;
19

20
    protected array $lookupMethods = [
21
        'netid' => '/res/netid',
22
        'emplid' => '/res/emplid',
23
        'hremplid' => '/res/hremplid',
24
        'sesemplid' => '/res/sesemplid',
25
        'barcode' => '/res/barcode',
26
        'mail' => '/res/mail',
27
        'studentemail' => '/res/studentemail',
28
    ];
29

30
    protected array $detailLevel = [
31
        'public' => '/pub/',
32
        'basic' => '/bas/',
33
        'expanded' => '/exp/',
34
    ];
35

36
    private GuzzleHttp\Client $http_client;
37

38
    public function __construct(GuzzleHttp\Client $client)
39
    {
40
        $this->http_client = $client;
96✔
41
        $this->baseUrl = (string) config('nusoa.directorySearch.baseUrl');
96✔
42
        $this->apiKey = (string) config('nusoa.directorySearch.apiKey');
96✔
43
    } // end __constructg
44

45
    /**
46
     * Convenience method to get the expanded detail by netID.
47
     */
48
    public function lookupByNetId($netid, $level = 'expanded')
49
    {
50
        return $this->lookup($netid, 'netid', $level);
15✔
51
    } // end lookupByNetId
52

53
    /**
54
     * [lookup description]
55
     *
56
     * @param  string  $value  Value to search by.
57
     * @param  string  $searchBy  See the $lookupMethods property.
58
     * @param  string  $level  public, basic, or expanded
59
     * @return array|false NetID details. Fields depend on the level.
60
     */
61
    public function lookup($value, $searchBy, $level): array|false
62
    {
63
        if (array_key_exists($searchBy, $this->lookupMethods) == false) {
15✔
64
            throw new \Exception("Invalid searchBy specified: '$searchBy'.");
×
65
        }
66

67
        if (array_key_exists($level, $this->detailLevel) == false) {
15✔
68
            throw new \Exception("Invalid $level specified: '$level'.");
×
69
        }
70

71
        $url = implode('', [$this->baseUrl, $this->lookupMethods[$searchBy], $this->detailLevel[$level], $value]);
15✔
72

73
        $result = $this->doGet($url);
15✔
74
        if ($result == false) {
15✔
75
            return $result;
12✔
76
        }
77

78
        $result = json_decode($result, true);
3✔
79

80
        // HTTP 200 can still have an error.
81
        if (array_key_exists('ErrorMessage', $result) == true) {
3✔
82
            $this->lastError = $result['ErrorMessage'];
×
83

UNCOV
84
            return false;
×
85
        }
86

87
        return $result['results'][0];
3✔
88
    } // end lookup
89

90
    public function getLastError()
91
    {
92
        return $this->lastError;
12✔
93
    } // end getLastError
94

95
    public function setBaseUrl($url)
96
    {
97
        $this->baseUrl = $url;
×
98
    } // end setBaseUrl
99

100
    public function setApiKey($key)
101
    {
102
        $this->apiKey = $key;
×
103
    } // end setApiKey
104

105
    public function setHttpClient(GuzzleHttp\Client $client)
106
    {
107
        $this->http_client = $client;
15✔
108
    } // end setHttpClient
109

110
    protected function doGet($url)
111
    {
112
        try {
113
            $request = $this->http_client->request('GET', $url, [
15✔
114
                'headers' => [
15✔
115
                    'apikey' => $this->apiKey,
15✔
116
                ],
15✔
117
                'http_errors' => false, // don't throw exceptions, I want to check the status code
15✔
118
            ]);
15✔
119
        } catch (\GuzzleHttp\Exception\RequestException $e) {
3✔
120
            $this->lastError = vsprintf('Verify connectivity to %s from the server: %s', [$this->baseUrl, $e->getMessage()]);
3✔
121

122
            return false;
3✔
123
        }
124

125
        /** @phpstan-ignore-next-line Bad netID or service unavailable with older Guzzles */
126
        if ($request === null) {
12✔
127
            $this->lastError = vsprintf('Request failed. Verify connectivity to %s from the server.', [$this->baseUrl]);
×
128

UNCOV
129
            return false;
×
130
        }
131

132
        if ($request->getStatusCode() != 200) {
12✔
133
            $message = 'HTTP connection succeeded but no body. HTTP code was '.$request->getStatusCode();
9✔
134
            if ($request->getBody() != null) {
9✔
135
                $error = json_decode($request->getBody(), true);
9✔
136

137
                // handle error based on returned format
138
                if (! empty($error['errorMessage'])) {
9✔
139
                    $message = $error['errorMessage'];
3✔
140
                } elseif (! empty($error['fault']['faultstring'])) {
6✔
141
                    $message = $error['fault']['faultstring'];
6✔
142
                }
143
            }
144

145
            $this->lastError = $message;
9✔
146

147
            return false;
9✔
148
        }
149

150
        return $request->getBody();
3✔
151
    } // end doGet
152

153
} // end IdMapper
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc