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

amalfra / supportbee / 3724693789

pending completion
3724693789

push

github

Amal Francis
add more test for HTTP.php

1 of 1 new or added line in 1 file covered. (100.0%)

37 of 127 relevant lines covered (29.13%)

0.46 hits per line

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

84.38
/src/HTTP.php
1
<?php
2

3
namespace Amalfra\SupportBee;
4

5
use \InvalidArgumentException;
6
use WpOrg\Requests\Requests;
7
use Amalfra\SupportBee\Exceptions\HTTPException as HTTPException;
8

9
/**
10
 * Class HTTP
11
 *
12
 * @package Amalfra\SupportBee
13
 */
14
class HTTP {
15
  protected static $http_error_msgs = array(
16
    401 => 'There was an error authenticating with the token.',
17
    403 => 'There was an error authenticating with the token.',
18
    500 => '500: Some error occured at SupportBee servers.'
19
  );
20

21
  protected static function validate($options = array(), $valid = array(), $required = array()) {
22
    if (!is_array($options)) {
3✔
23
      throw new InvalidArgumentException('Parameters need to be passed as array');
1✔
24
    }
25

26
    $options = array_keys($options);
2✔
27

28
    foreach ($options as $k => $v) {
2✔
29
      if (!in_array($v, $valid)) {
2✔
30
        throw new InvalidArgumentException('Not a valid parameter passed');
1✔
31
      }
32
    }
33

34
    if (count($required) && !array_intersect($options, $required)) {
1✔
35
      throw new InvalidArgumentException('Required parameter not passed');
1✔
36
    }
37
  }
38

39
  protected static function tf_to_string(&$value, $key) {
40
    if ($value === true) {
2✔
41
      $value = 'true';
1✔
42
    } else if ($value === false) {
1✔
43
      $value = 'false';
1✔
44
    }
45
  }
46

47
  protected static function inject(&$options) {
48
    $options = array_merge($options, array(
1✔
49
      'auth_token' => Client::$auth_token
1✔
50
    ));
51
  }
52

53
  private static function request($path, $options, $method = 'GET') {
54
    if (strtoupper($method) == 'GET') {
4✔
55
      return Requests::get(Client::$base_url.$path.'?'.http_build_query($options), Client::$headers, $options);
1✔
56
    } else if (strtoupper($method) == 'POST') {
3✔
57
      return Requests::post(Client::$base_url.$path, Client::$headers, json_encode($options));
1✔
58
    } else if (strtoupper($method) == 'DELETE') {
2✔
59
      return Requests::delete(Client::$base_url.$path.'?'.http_build_query($options), Client::$headers, $options);
1✔
60
    } else {
61
      throw new HTTPException('Unknown HTTP request method');
1✔
62
    }
63
  }
64

65
  protected static function handle_response($resp) {
66
    if ($resp->status_code == 204) {
2✔
67
      return true;
1✔
68
    }
69

70
    $decoded = @json_decode($resp->body, true);
1✔
71
    if (!$decoded) {
1✔
72
      throw new HTTPException((isset(self::$http_error_msgs[$resp->status_code])) ?
1✔
73
        self::$http_error_msgs[$resp->status_code] : 'An HTTP error with status code '.$resp->status_code.' occured');
1✔
74
    } else {
75
      return $decoded;
×
76
    }
77
  }
78

79
  protected static function process_request($path, $options = array(), $method = 'GET') {
80
    self::inject($options);
×
81
    array_walk($options, 'self::tf_to_string');
×
82

83
    $resp = self::request($path, $options, $method);
×
84
    return self::handle_response($resp);
×
85
  }
86
}
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

© 2025 Coveralls, Inc