diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..78ff967
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+vendor/
+node_modules/
+.idea
\ No newline at end of file
diff --git a/composer.json b/composer.json
index a7ecdeb..3684475 100644
--- a/composer.json
+++ b/composer.json
@@ -14,7 +14,7 @@
},
"autoload": {
"psr-4": {
- "Picahoo\\Communication\\": "src/"
+ "Picahoo\\Communicator\\": "src/"
}
},
"extra": {
diff --git a/src/Communicator.php b/src/Communicator.php
new file mode 100644
index 0000000..eba81da
--- /dev/null
+++ b/src/Communicator.php
@@ -0,0 +1,328 @@
+config_status || !$this->token_generated){
+ return $this->token_response;
+ }
+ }
+
+ public function refreshToken()
+ {
+ return $this->generateToken();
+ }
+
+ public function getContactList()
+ {
+ if($check = $this->checkConfigAndToken()){
+ return $check;
+ }
+
+ $data = $this->request('get', [], ['Authorization' => 'Bearer ' . $this->token], $this->getApiUrl('/user/contact/all'));
+ if ($data['statusCode'] != 200) {
+ return [
+ 'code' => $data['statusCode'],
+ 'status' => 'fail',
+ 'message' => $data['body'],
+ 'contacts' => null
+ ];
+ }
+ $res = json_decode($data['body'], true);
+ return [
+ 'code' => $data['statusCode'],
+ 'status' => 'ok',
+ 'message' => 'all good',
+ 'contacts' => $this->transformContacts(collect($res)->toArray())
+ ];
+ }
+
+ private function addContact($requestData)
+ {
+ if($check = $this->checkConfigAndToken()){
+ return $check;
+ }
+
+ $data = $this->request('POST', [
+ "first_name" => $requestData['first_name'],
+ "last_name" => $requestData['last_name'],
+ "email" => $requestData['email'],
+ "phone" => $requestData['phone']
+ ], ['Authorization' => 'Bearer ' . $this->token], $this->getApiUrl('/user/contact/force-store'));
+
+ if($data['statusCode'] != 200){
+ return [
+ 'code' => $data['statusCode'],
+ 'status' => 'fail',
+ 'message' => $data['body']
+ ];
+ }
+
+ $jsonObj = json_decode($data['body'], true);
+ if(!(isset($jsonObj['contact']) && $jsonObj['contact'])){
+ return [
+ 'code' => 400,
+ 'status' => 'fail',
+ 'message' => 'contact create fail',
+ 'contact' => null
+ ];
+ }
+
+ if(!empty($jsonObj['contact']) && isset($jsonObj['contact']['id'])){
+ return [
+ 'code' => 200,
+ 'status' => 'ok',
+ 'message' => 'contact created',
+ 'contact' => [
+ "id" => $jsonObj['contact']['id'],
+ "user_id" => $jsonObj['contact']['user_id'],
+ "first_name" => $jsonObj['contact']['first_name'],
+ "last_name" => $jsonObj['contact']['last_name'],
+ "email" => $jsonObj['contact']['email'],
+ "phone" => $jsonObj['contact']['phone'],
+ "active" => $jsonObj['contact']['active']
+ ]
+ ];
+ }else{
+ return [
+ 'code' => 400,
+ 'status' => 'fail',
+ 'message' => 'contact create fail',
+ 'contact' => null
+ ];
+ }
+ }
+
+ public function findContactByEmail($email)
+ {
+ $response = $this->getContactList();
+ if($response['code'] != 200){
+ return $response;
+ }
+
+ $conatct = collect($response['contacts'])->where('email', $email)->first();
+ if (!empty($conatct)){
+ return [
+ 'code' => 200,
+ 'status' => 'ok',
+ 'message' => 'contact found',
+ 'contact' => $conatct
+ ];
+ }
+ }
+
+ private function checkContactByEmail($email)
+ {
+ if($check = $this->checkConfigAndToken()){
+ return $check;
+ }
+ $contactResponse = $this->findContactByEmail($email);
+
+ if($contactResponse['code'] == 200){
+ return $contactResponse;
+ }
+
+ $response = $this->addContact([
+ "first_name" => null,
+ "last_name" => null,
+ "email" => $email,
+ "phone" => null
+ ]);
+
+ return $response;
+ }
+
+ public function sendEmail($to, $message, $subject,$attachments=[])
+ {
+ $attachmentTotalSize = 25;
+ $sendableAttachments = [];
+
+ if(count($attachments) > 0){
+ $actualFileSize = 0;
+ foreach ($attachments as $attachmentPath){
+ $sendableAttachments[]=[
+ 'name' => pathinfo($attachmentPath, PATHINFO_BASENAME),
+ 'type' => finfo_file(finfo_open(FILEINFO_MIME_TYPE), $attachmentPath),
+ 'content' => base64_encode(file_get_contents($attachmentPath)),
+ ];
+
+ $actualFileSize+= (floatval(filesize($attachmentPath)) / pow(1024, 2));
+ }
+
+ if(!($actualFileSize <= $attachmentTotalSize)){
+ return ['message' => 'attachment file size limit exceed, you upload under 25mb','status' => 'fail','code' => 404];
+ }
+ }
+
+ if($check = $this->checkConfigAndToken()){
+ return $check;
+ }
+ $res = $this->checkContactByEmail($to);
+ if($res['code'] != 200){
+ return $res;
+ }
+
+ if (empty($res) || !isset($res['contact']) && !isset($res['contact']['id'])) {
+ return ['message' => 'contact not exists in system','status' => 'fail','code' => 404];
+ }
+
+ return $this->sendEmailByContactId($res['contact']['id'], $message, $subject,$sendableAttachments);
+ }
+
+ public function sendEmailWithBase64Attchment($to, $message, $subject, $attachments = [])
+ {
+ $attachmentTotalSize = 25;
+ $sendableAttachments = [];
+// if (count($attachments) > 0) {
+// $actualFileSize = 0;
+// foreach ($attachments as $attachmentPath) {
+// $sendableAttachments[] = [
+// 'name' => pathinfo($attachmentPath, PATHINFO_BASENAME),
+// 'type' => finfo_file(finfo_open(FILEINFO_MIME_TYPE), $attachmentPath),
+// 'content' => base64_encode(file_get_contents($attachmentPath)),
+// ];
+// $actualFileSize += (floatval(filesize($attachmentPath)) / pow(1024, 2));
+// }
+// if (!($actualFileSize <= $attachmentTotalSize)) {
+// return [
+// 'message' => 'attachment file size limit exceed, you upload under 25mb',
+// 'status' => 'fail',
+// 'code' => 404
+// ];
+// }
+// }
+ if ($check = $this->checkConfigAndToken()) {
+ return $check;
+ }
+ $res = $this->checkContactByEmail($to);
+ if ($res['code'] != 200) {
+ return $res;
+ }
+ if (empty($res) || !isset($res['contact']) && !isset($res['contact']['id'])) {
+ return [
+ 'message' => 'contact not exists in system',
+ 'status' => 'fail',
+ 'code' => 404
+ ];
+ }
+ return $this->sendEmailByContactId($res['contact']['id'], $message, $subject, $sendableAttachments);
+ }
+
+ public function sendSms($data = ['email' => '','phone' => ''], $message)
+ {
+ if($check = $this->checkConfigAndToken()){
+ return $check;
+ }
+ $res = $this->addContact([
+ "first_name" => null,
+ "last_name" => null,
+ "email" => $data['email'],
+ "phone" => $data['phone']
+ ]);
+
+ if($res['code'] != 200){
+ return $res;
+ }
+ if (empty($res) || !isset($res['contact']) && !isset($res['contact']['id'])) {
+ return ['message' => 'contact not exists in system','status' => 'fail','code' => 404];
+ }
+ return $this->sendSmsByContactId($res['contact']['id'], $message);
+ }
+ /*
+ * @ contactId integer
+ * @ message string
+ * @sendableAttachments array ['name','type','content']
+ *
+ * */
+ public function sendEmailByContactId($contactId, $message, $subject,$sendableAttachments=[])
+ {
+ $data = $this->request('POST', [
+ "message" => $message,
+ "contact_id" => $contactId,
+ "subject" => $subject,
+ "attachments" => $sendableAttachments
+ ], ['Authorization' => 'Bearer ' . $this->token], $this->getApiUrl('/mandrill/send'));
+
+ if($data['statusCode'] != 200){
+
+ if($data['statusCode'] ==100 && isset($data['error_message'])){
+ $data['body'] = $data['error_message'];
+ }
+
+ return [
+ 'message' => $data['body'],
+ 'status' => 'fail',
+ 'code' => $data['statusCode']
+ ];
+ }
+
+ $res = json_decode($data['body'], true);
+ return [
+ 'message' => $res['message'],
+ 'status' => 'ok',
+ 'code' => 200
+ ];
+ }
+
+
+ /*
+ * @ contactId integer
+ * @ message string
+ * @sendableAttachments array ['name','type','content']
+ *
+ * */
+ public function sendSmsByContactId($contactId, $message)
+ {
+ $data = $this->request('POST', [
+ "message" => $message,
+ "contact_id" => $contactId
+ ], ['Authorization' => 'Bearer ' . $this->token], $this->getApiUrl('/sms/send'));
+ if($data['statusCode'] != 200){
+
+ if($data['statusCode'] ==100 && isset($data['error_message'])){
+ $data['body'] = $data['error_message'];
+ }
+
+ return [
+ 'message' => $data['body'],
+ 'status' => 'fail',
+ 'code' => $data['statusCode']
+ ];
+ }
+
+ $res = json_decode($data['body'], true);
+ return [
+ 'message' => $res['message'],
+ 'status' => 'ok',
+ 'code' => 200
+ ];
+ }
+
+ public function checkConfigAndToken()
+ {
+ if(!$this->config_status){
+ return [
+ 'code' => 400,
+ 'message' => 'Your config file not set properly, please set config file.',
+ 'status' => 'fail',
+ 'body' => null
+ ];
+ }
+
+ if(!$this->token_generated){
+ return [
+ 'code' => 400,
+ 'message' => 'Token not provided',
+ 'status' => 'fail',
+ 'body' => null
+ ];
+ }
+
+ return false;
+ }
+
+}
\ No newline at end of file
diff --git a/src/CommunicatorServiceProvider.php b/src/CommunicatorServiceProvider.php
new file mode 100644
index 0000000..e3bdc42
--- /dev/null
+++ b/src/CommunicatorServiceProvider.php
@@ -0,0 +1,31 @@
+publishes([
+ __DIR__ . '/config/communicator.php' => config_path('communicator.php'),
+ ]);
+ $this->mergeConfigFrom(__DIR__ . '/config/communicator.php', 'communicator');
+ }
+
+ /**
+ * Register the application services.
+ * @return void
+ */
+ public function register()
+ {
+ $this->app->bind('communicator', function () {
+ return new Communicator();
+ });
+ }
+}
diff --git a/src/Configuration.php b/src/Configuration.php
new file mode 100644
index 0000000..b505b83
--- /dev/null
+++ b/src/Configuration.php
@@ -0,0 +1,168 @@
+ 'fail',
+ 'code' => 400,
+ 'message' => 'configuration not set'
+ ];
+ protected $config_status=false;
+
+
+ public function __construct() {
+ $this->setDefaultConfiguration();
+ $this->token_response = $this->generateToken();
+ }
+
+ public function getCredentials()
+ {
+ return [
+ 'email' => $this->email,
+ 'password' => $this->password
+ ];
+ }
+
+ private function setDefaultConfiguration()
+ {
+ if(empty(config('communicator')) || empty(config('communicator.credential')) || empty(config('communicator.mail'))){
+ $this->config_status = false;
+ }else{
+ $this->version = config('communicator.version');
+ $this->api_url = config('communicator.api_url');
+ $this->email = config('communicator.credential.email');
+ $this->password = config('communicator.credential.password');
+ $this->mail_from_name = config('communicator.mail.mail_from_name');
+ $this->config_status = true;
+ }
+ }
+
+ public function generateToken()
+ {
+ if(!$this->config_status){
+ return [
+ 'code' => 400,
+ 'message' => 'Your config file not set properly, please set config file.',
+ 'status' => 'fail',
+ 'body' => null
+ ];
+ }
+
+ if(!empty($this->token)){
+ return [
+ 'code' => 200,
+ 'message' => 'Token Already exists',
+ 'status' => 'ok',
+ 'token' => $this->token
+ ];
+ }
+ $data = $this->request("POST", $this->getCredentials(), [],$this->getApiUrl('/user/authenticate'));
+ if($data['statusCode'] != 200){
+ $this->token_generated = false;
+ return [
+ 'code' => $data['statusCode'],
+ 'message' => $data['body'],
+ 'status' => 'fail',
+ 'token' => null
+ ];
+ }
+ $res = json_decode($data['body'], true);
+ if (isset($res['token'])) {
+ $this->token = $res['token'];
+ $this->token_generated = true;
+ return [
+ 'code' => 200,
+ 'message' => 'Token generated',
+ 'status' => 'ok',
+ 'token' => $res['token']
+ ];
+ }
+
+ }
+
+ public function getApiUrl($url)
+ {
+ return $this->api_url . $url;
+ }
+
+ public function getToken()
+ {
+ return $this->token;
+ }
+ public function transformContacts($contactLists)
+ {
+ $newList = [];
+ if (isset($contactLists['contacts'])) {
+ foreach ($contactLists['contacts'] as $contact) {
+ $newList[] = [
+ "id" => $contact['id'],
+ "user_id" => $contact['user_id'],
+ "first_name" => $contact['first_name'],
+ "last_name" => $contact['last_name'],
+ "email" => $contact['email'],
+ "phone" => $contact['phone'],
+ "active" => $contact['active'],
+ "created_at" => $contact['created_at'],
+ "updated_at" => $contact['updated_at'],
+ "deleted_at" => $contact['deleted_at']
+ ];
+ }
+ }
+ return $newList;
+ }
+
+ public function request($type = "GET", $data = [], $headers = [], $url)
+ {
+ $curl = curl_init();
+ $HEADERS = ['Authorization' => 'Bearer ' . $this->token];
+ $HEADERS = array_merge($HEADERS, $headers);
+ if ($type == 'GET' || $type == 'get') {
+ curl_setopt_array($curl, array (
+ CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_URL => $url . '?token=' . $this->token,
+ ));
+ }
+ else {
+ curl_setopt_array($curl, array (
+ CURLOPT_URL => $url . '?token=' . $this->token,
+ CURLOPT_RETURNTRANSFER => true,
+ CURLOPT_ENCODING => "",
+ CURLOPT_MAXREDIRS => 10,
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+ CURLOPT_CUSTOMREQUEST => $type,
+ CURLOPT_POSTFIELDS => http_build_query($data),
+ CURLOPT_HTTPHEADER => $HEADERS,
+ ));
+ }
+ $response = curl_exec($curl);
+ $httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
+ $error_no =curl_errno($curl);
+ $error_message = curl_error($curl);
+ curl_close($curl);
+
+ if ($curl === null) {
+ return [
+ 'statusCode' => $httpcode,
+ 'body' => $response,
+ 'error_no' => $error_no,
+ 'error_message' => $error_message
+ ];
+ } else {
+ return [
+ 'statusCode' => $httpcode,
+ 'body' => $response,
+ 'error_no' => $error_no,
+ 'error_message' => $error_message
+ ];
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/Facades/Communicator.php b/src/Facades/Communicator.php
new file mode 100644
index 0000000..9b95db3
--- /dev/null
+++ b/src/Facades/Communicator.php
@@ -0,0 +1,19 @@
+$username, "password"=>$password];
-
- $auth = json_decode($this->request("POST", $data, [], "user/authenticate"), true);
-
-
-
- if(isset($auth['token'])){
- return $auth;
- }else{
- return false;
- }
- }
-
-
- /* CONTACT THINGS */
-
- // Fetches all the contacts
- public function get_contacts($username, $password, $filter=""){
- $token = $this->login($username, $password);
- if(!$token)
- return false;
- $token = $token['token'];
- $data = json_decode($this->request("GET", [], ["authorization: Bearer <".$token.">"], "user/contact/all"), true)['contacts'];
-
- global $return;
- $return = [];
-
-
- $test = function($item, $key, $check){
-
- global $return;
-
- $tests = ['id', 'email', 'phone'];
- foreach($tests as $test){
- if(empty($check)){
- array_push($return, $item);
- return;
- }else{
- $has = strpos(" ".$item[$test], $check);
- if($has){
- array_push($return, $item);
- return;
- }
- }
-
- }
- };
- array_walk($data, $test, $filter);
-
- return $return;
- }
-
- public function add_contact($username, $password, $first_name, $last_name, $email, $phone){
- $email_taken = $this->get_contacts($username, $password, $email);
- $number_taken = $this->get_contacts($username, $password, $phone);
-
-
- if($number_taken or $email_taken)
- return false;
-
- $auth = $this->login($username, $password);
- return $this->request("POST", [ "first_name"=>$first_name,
- "last_name"=>$last_name,
- "email"=>$email,
- "phone"=>$phone
- ],
- ["authorization: Bearer <".$auth['token'].">"],
- "user/contact/create");
- }
-
- /* END CONTACT THINGS */
-
- // send an email
- public function send_email($username, $password, $to, $message, $subject){
- $token = $this->login($username, $password);
- if(!$token)
- return false;
- $token = $token['token'];
-
- $contact = $this->get_contacts($username, $password, $to);
- if(empty($contact))
- return false;
- $contact = $contact[0];
- return $this->request("POST", ["message"=>$message, "contact_id"=>$contact['id'],"subject"=>$subject], ["authorization: Bearer <".$token.">"], "mandrill/send");
- }
-
- // sends an sms
- public function send_sms($username, $password, $to, $message){
- $token = $this->login($username, $password);
- if(!$token)
- return false;
- $token = $token['token'];
-
-
- $contact = $this->get_contacts($username, $password, $to);
-
- if(empty($contact))
- return false;
-
- $contact = $contact[0];
-
- return $this->request("POST", ["message"=>$message, "contact_id"=>$contact['id']], ["authorization: Bearer <".$token.">"], "sms/send");
- }
-
- public function get_email(){
- return $this->request("GET", [], [], "mandrill/receive");
- }
-
- public function get_sms($username, $password){
- $token = $this->login($username, $password)['token'];
- return $this->request("GET", [], [], "sms/receive?token=".$token);
- }
-
-
- private function request($type="GET", $data=[], $headers=[], $url){
- $curl = curl_init();
-
- $HEADERS = ["cache-control: no-cache",
- "content-type: application/json; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
- "postman-token: d01b2ab1-dc2a-dc18-1199-47f83f334200"];
-
- $HEADERS = array_merge($HEADERS, $headers);
-
- //dd($query);
- curl_setopt_array($curl, array(
- CURLOPT_URL => "http://picahooapi.test4you.in/api/v1/".$url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_ENCODING => "",
- CURLOPT_MAXREDIRS => 10,
- CURLOPT_TIMEOUT => 25,
- CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
- CURLOPT_CUSTOMREQUEST => $type,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_HTTPHEADER => $HEADERS,
- ));
-
- $response = curl_exec($curl);
- $err = curl_error($curl);
-
- curl_close($curl);
- if ($err) {
- dd("cURL Error #:" . $err);
- } else {
- return $response;
- }
- }
-}
\ No newline at end of file
diff --git a/src/SocialServiceProvider.php b/src/SocialServiceProvider.php
deleted file mode 100644
index 99b129e..0000000
--- a/src/SocialServiceProvider.php
+++ /dev/null
@@ -1,30 +0,0 @@
-app->bind('picahoo-communicator', function(){
- return new Communicator();
- });
- }
-}
diff --git a/src/config/communicator.php b/src/config/communicator.php
new file mode 100644
index 0000000..4fb4c73
--- /dev/null
+++ b/src/config/communicator.php
@@ -0,0 +1,13 @@
+ 'v1',
+ 'api_url' => env('COMMUNICATOR_API_URL', 'http://picahooapi.test4you.in/api/v1'),
+ 'credential' => [
+ 'email' => env('COMMUNICATOR_EMAIL', ''),
+ 'password' => env('COMMUNICATOR_PASSWORD', '')
+ ],
+ 'mail' => [
+ 'mail_from_name' => env('COMMUNICATOR_MAIL_FROM_NAME', '')
+ ]
+];
\ No newline at end of file
diff --git a/src/facades/SocialFacade.php b/src/facades/SocialFacade.php
deleted file mode 100644
index f776136..0000000
--- a/src/facades/SocialFacade.php
+++ /dev/null
@@ -1,11 +0,0 @@
-";
- }
- else
- {
- echo "Test send email FAILED
";
- }
- if($result = HSocial::send_sms("admin@weboccult.com", "123456", "27711304241", "testing the communicator"))
- {
- echo "Test send sms passed ".$result."
";
- }
- else
- {
- echo "Test send sms FAILED
";
- }
-});
-
-Route::any('login', function(){
- return HSocial::login("admin@weboccult.com", "123456");
-});
-
-Route::any('contacts', function(){
- return HSocial::get_contacts("admin@weboccult.com", "123456", "+27");
-});
-
-Route::any('addcontact', function(){
- return HSocial::add_contact("admin@weboccult.com", "123456", "sam", "phillop", "dumbymail@gmail.com", "+27748697417");
-});
-
-Route::any('getemail', function(){
- return HSocial::get_email();
-});
-
-Route::any('sendemail', function(){
- return HSocial::send_email("admin@weboccult.com", "123456", 'dgmon.mail@gmail.com', "this is a message", "test mail");
-});
-
-Route::get('send_sms', function(){
- return HSocial::send_sms("admin@weboccult.com", "123456", 5, "this is a message");
-});
-
-Route::get('get_sms', function(){
- return HSocial::get_sms("admin@weboccult.com", "123456");
-});
-