From 8d790c72644dbeda5afcfb9a8e830b53c9c6619d Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Tue, 11 Sep 2018 20:20:53 +0200 Subject: [PATCH] Simplified class constructors --- src/API.php | 10 +++++----- src/Instance.php | 12 ++++-------- src/Playlist.php | 10 +++------- src/RestClient.php | 12 ++++++++++++ src/User.php | 13 ++++--------- 5 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/API.php b/src/API.php index db0a45b..34d3096 100644 --- a/src/API.php +++ b/src/API.php @@ -85,7 +85,7 @@ public function getPlaylists() $playlists = $this->request('/bot/playlists'); $out = []; foreach ($playlists as $playlist) { - array_push($out, new Playlist($this->token, $this->url, $this->timeout, $playlist)); + array_push($out, new Playlist($this, $playlist)); } return $out; } @@ -102,7 +102,7 @@ public function createPlaylist($playlistName) "name" => $playlistName, ]); $resp['name'] = $playlistName; - return new Playlist($this->token, $this->url, $this->timeout, $resp); + return new Playlist($this, $resp); } /** @@ -300,7 +300,7 @@ public function getUsers() $users = $this->request('/bot/users'); $out = []; foreach ($users as $user) { - array_push($out, new User($this->token, $this->url, $this->timeout, $user)); + array_push($out, new User($this, $user)); } return $out; } @@ -369,7 +369,7 @@ public function getInstances() $instances = $this->request('/bot/instances'); $out = []; foreach ($instances as $instance) { - array_push($out, new Instance($this->token, $this->url, $this->timeout, $instance)); + array_push($out, new Instance($this, $instance)); } return $out; } @@ -448,6 +448,6 @@ public function getInstanceByUUID($uuid) { $instance = $this->request("/bot/i/".$uuid."/settings"); $instance['uuid'] = $uuid; - return new Instance($this->token, $this->url, $this->timeout, $instance); + return new Instance($this, $instance); } } diff --git a/src/Instance.php b/src/Instance.php index 4f5c208..336b434 100644 --- a/src/Instance.php +++ b/src/Instance.php @@ -30,17 +30,13 @@ class Instance extends RestClient /** * __construct * - * @param string $token SinusBot auth token - * @param string $url SinusBot Bot URL - * @param int $timeout HTTP Timeout which is used to perform HTTP API requests - * @param array $instance SinusBot Instance array. + * @param API $api SinusBot API + * @param array $instance SinusBot Instance array * @return void */ - public function __construct($token, $url, $timeout, $instance) + public function __construct($api, $instance) { - $this->token = $token; - $this->url = $url; - $this->timeout = $timeout; + parent::__construct($api); $this->uuid = $instance['uuid']; $this->instance = $instance; } diff --git a/src/Playlist.php b/src/Playlist.php index cc0dc9e..a77e3ae 100644 --- a/src/Playlist.php +++ b/src/Playlist.php @@ -30,17 +30,13 @@ class Playlist extends RestClient /** * __construct * - * @param string $token SinusBot auth token - * @param string $url SinusBot Bot URL - * @param int $timeout HTTP Timeout which is used to perform HTTP API requests + * @param API $api SinusBot API * @param array $playlist SinusBot Playlist array. * @return void */ - public function __construct($token, $url, $timeout, $playlist) + public function __construct($api, $playlist) { - $this->token = $token; - $this->url = $url; - $this->timeout = $timeout; + parent::__construct($api); $this->uuid = $playlist['uuid']; $this->playlist = $playlist; } diff --git a/src/RestClient.php b/src/RestClient.php index 31b0db3..048a3f9 100644 --- a/src/RestClient.php +++ b/src/RestClient.php @@ -38,6 +38,18 @@ class RestClient */ protected $headers = []; /** + * __construct + * + * @param API $api SinusBot API + */ + protected function __construct($api) + { + $this->token = $api->token; + $this->url = $api->url; + $this->timeout = $api->timeout; + $this->headers = $api->headers; + } + /** * request executes a request to the SinusBot API * * @param string $path /api/v1/ diff --git a/src/User.php b/src/User.php index bdabfca..3e3295e 100644 --- a/src/User.php +++ b/src/User.php @@ -30,17 +30,12 @@ class User extends RestClient /** * __construct * - * @param string $token SinusBot auth token - * @param string $url SinusBot Bot URL - * @param int $timeout HTTP Timeout which is used to perform HTTP API requests - * @param array $user SinusBot User array. - * @return void + * @param API $api SinusBot API + * @param array $user SiusBot User array */ - public function __construct($token, $url, $timeout, $user) + public function __construct($api, $user) { - $this->token = $token; - $this->url = $url; - $this->timeout = $timeout; + parent::__construct($api); $this->uuid = $user['id']; $this->user = $user; }