| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSupport\App\Api\Classes; |
| 4 |
|
| 5 |
use FluentSupport\App\Http\Controllers\AuthController; |
| 6 |
use FluentSupport\App\Models\Agent; |
| 7 |
|
| 8 |
/** |
| 9 |
* Agent class for PHP API |
| 10 |
* |
| 11 |
* Example Usage: $agentsApi = FluentSupportApi('agents'); |
| 12 |
* |
| 13 |
* @package FluentSupport\App\Api\Classes |
| 14 |
* |
| 15 |
* @version 1.0.0 |
| 16 |
*/ |
| 17 |
class Agents |
| 18 |
{ |
| 19 |
private $instance = null; |
| 20 |
|
| 21 |
private $allowedInstanceMethods = [ |
| 22 |
'all', |
| 23 |
'get', |
| 24 |
'find', |
| 25 |
'first', |
| 26 |
'paginate' |
| 27 |
]; |
| 28 |
|
| 29 |
public function __construct(Agent $instance) |
| 30 |
{ |
| 31 |
$this->instance = $instance; |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* getAgents method will return a all available customers |
| 36 |
* |
| 37 |
* @return object |
| 38 |
*/ |
| 39 |
|
| 40 |
public function getAgents() |
| 41 |
{ |
| 42 |
return Agent::paginate(); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* getAgent method will return a specific agent by id |
| 47 |
* |
| 48 |
* @param int $agentId |
| 49 |
* @return mixed |
| 50 |
*/ |
| 51 |
|
| 52 |
public function getAgent($agentId) |
| 53 |
{ |
| 54 |
if (is_numeric($agentId)) { |
| 55 |
return Agent::findOrFail($agentId); |
| 56 |
} |
| 57 |
return false; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* updateAgent method will update the specific agent by id |
| 62 |
* |
| 63 |
* @param array $data |
| 64 |
* @param int $agentId |
| 65 |
* @return mixed |
| 66 |
*/ |
| 67 |
|
| 68 |
public function updateAgent(array $data, $agentId) |
| 69 |
{ |
| 70 |
if (!$agentId) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
if ($agent = Agent::where('id', $agentId)->first()) { |
| 74 |
return $agent->update($data); |
| 75 |
} |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* createAgentWithOrWithoutWpUser method will create a new agent |
| 81 |
* also it will create a wp user if you want to create one |
| 82 |
* if you want to create a wp user too then the process will be 1st it will create a wp user |
| 83 |
* after creating the wp user successfully it will create a fluent support agent |
| 84 |
* |
| 85 |
* @param array $data |
| 86 |
* @param bool $createWpUser |
| 87 |
* @return mixed |
| 88 |
*/ |
| 89 |
|
| 90 |
public function createAgentWithOrWithoutWpUser(array $data, $createWpUser = false) |
| 91 |
{ |
| 92 |
if (!$createWpUser) { |
| 93 |
$isExist = Agent::where('email', $data['email'])->first(); |
| 94 |
if (!$data['email'] || !is_email($data['email']) || $isExist) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
return Agent::create($data); |
| 98 |
} |
| 99 |
|
| 100 |
if (!username_exists($data['username'])) { |
| 101 |
$authController = new AuthController(); |
| 102 |
$createdUser = $authController->createUser($data); |
| 103 |
$authController->maybeUpdateUser($createdUser, $data); |
| 104 |
if ($createdUser) { |
| 105 |
$data['user_id'] = $createdUser; |
| 106 |
return Agent::create($data); |
| 107 |
|
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return false; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* deleteAgent method will delete agent by id |
| 116 |
* @param int $id |
| 117 |
*/ |
| 118 |
public function deleteAgent($id) |
| 119 |
{ |
| 120 |
if (!$id) { |
| 121 |
return; |
| 122 |
} |
| 123 |
Agent::findOrFail($id)->delete(); |
| 124 |
} |
| 125 |
|
| 126 |
public function getInstance() |
| 127 |
{ |
| 128 |
return $this->instance; |
| 129 |
} |
| 130 |
|
| 131 |
public function __call($method, $params) |
| 132 |
{ |
| 133 |
if (in_array($method, $this->allowedInstanceMethods)) { |
| 134 |
return call_user_func_array([$this->instance, $method], $params); |
| 135 |
} |
| 136 |
|
| 137 |
throw new \Exception(sprintf('Method %s does not exist.', esc_html($method))); |
| 138 |
} |
| 139 |
} |
| 140 |
|