cryptommer /
sms-ir
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Cryptommer\Smsir\Classes; |
||||
| 4 | |||||
| 5 | use Cryptommer\Smsir\Exceptions\HttpException; |
||||
| 6 | use GuzzleHttp\Client; |
||||
| 7 | use GuzzleHttp\Exception\GuzzleException; |
||||
| 8 | use function __; |
||||
| 9 | use function config; |
||||
| 10 | |||||
| 11 | class Smsir { |
||||
| 12 | |||||
| 13 | private const BASEURL = 'https://api.sms.ir/'; |
||||
| 14 | |||||
| 15 | private Client $client; |
||||
| 16 | public $LineNumber; |
||||
| 17 | |||||
| 18 | public function __construct() { |
||||
| 19 | $this->client = new Client([ |
||||
| 20 | 'base_uri' => self::BASEURL, |
||||
| 21 | 'headers' =>[ |
||||
| 22 | 'X-API-KEY' => config('smsir.api-key'), |
||||
| 23 | 'ACCEPT' => 'application/json', |
||||
| 24 | 'Content-Type' => 'application/json' |
||||
| 25 | ] |
||||
| 26 | ]); |
||||
| 27 | $this->LineNumber = config('smsir.line-number'); |
||||
| 28 | } |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * Sending Messages |
||||
| 32 | * @return Send |
||||
| 33 | */ |
||||
| 34 | public function Send(): Send { |
||||
| 35 | return new Send($this); |
||||
| 36 | } |
||||
| 37 | |||||
| 38 | /** |
||||
| 39 | * Get Reports |
||||
| 40 | * @return Report |
||||
| 41 | */ |
||||
| 42 | public function Report(): Report { |
||||
| 43 | return new Report($this); |
||||
| 44 | } |
||||
| 45 | |||||
| 46 | /** |
||||
| 47 | * @return Setting |
||||
| 48 | */ |
||||
| 49 | public function Setting(): Setting { |
||||
| 50 | return new Setting($this); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * @param string $path |
||||
| 55 | * @param array|null $params |
||||
| 56 | * @return mixed |
||||
| 57 | * @throws GuzzleException |
||||
| 58 | * @throws HttpException |
||||
| 59 | * @throws \JsonException |
||||
| 60 | */ |
||||
| 61 | public function post(string $path, array $params = []) { |
||||
| 62 | $response = $this->client->post($path, ['body' => json_encode($params, JSON_THROW_ON_ERROR)]); |
||||
| 63 | if ($response->getStatusCode() !== 200) { |
||||
| 64 | throw new HttpException(__('smsir.error.'.$response->getStatusCode())); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 65 | } |
||||
| 66 | return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); |
||||
| 67 | } |
||||
| 68 | |||||
| 69 | /** |
||||
| 70 | * @param string $path |
||||
| 71 | * @param array|null $params |
||||
| 72 | * @return mixed |
||||
| 73 | * @throws GuzzleException |
||||
| 74 | * @throws HttpException |
||||
| 75 | * @throws \JsonException |
||||
| 76 | */ |
||||
| 77 | public function get(string $path, array $params = []) { |
||||
| 78 | $response = $this->client->get($path, ['body' => json_encode($params, JSON_THROW_ON_ERROR)]); |
||||
| 79 | if ($response->getStatusCode() !== 200) { |
||||
| 80 | throw new HttpException(__('smsir.error.'.$response->getStatusCode())); |
||||
|
0 ignored issues
–
show
It seems like
__('smsir.error.' . $response->getStatusCode()) can also be of type array and array; however, parameter $message of Cryptommer\Smsir\Excepti...xception::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 81 | } |
||||
| 82 | return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | /** |
||||
| 86 | * @param string $path |
||||
| 87 | * @param array $params |
||||
| 88 | * @return mixed |
||||
| 89 | * @throws GuzzleException |
||||
| 90 | * @throws HttpException |
||||
| 91 | * @throws \JsonException |
||||
| 92 | */ |
||||
| 93 | public function delete(string $path, array $params = []) { |
||||
| 94 | $response = $this->client->delete($path, ['body' => json_encode($params, JSON_THROW_ON_ERROR)]); |
||||
| 95 | if ($response->getStatusCode() !== 200) { |
||||
| 96 | throw new HttpException(__('smsir.error.'.$response->getStatusCode())); |
||||
|
0 ignored issues
–
show
It seems like
__('smsir.error.' . $response->getStatusCode()) can also be of type array and array; however, parameter $message of Cryptommer\Smsir\Excepti...xception::__construct() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 97 | } |
||||
| 98 | return json_decode($response->getBody()->getContents(), true, 512, JSON_THROW_ON_ERROR); |
||||
| 99 | } |
||||
| 100 | |||||
| 101 | |||||
| 102 | } |
||||
| 103 |