PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.4.5
Fluent Support – Helpdesk & Customer Support Ticket System v2.4.5
2.4.5 2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 All 69 releases
fluent-support / app / Api / Classes / Agents.php

Agents.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.4.5, at app/Api/Classes/Agents.php

128 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Agents PHP API.
10 *
11 * Example Usage: $agentsApi = FluentSupportApi('agents');
12 */
13 class Agents
14 {
15 private $instance = null;
16
17 private $allowedInstanceMethods = [
18 'all',
19 'get',
20 'find',
21 'first',
22 'paginate'
23 ];
24
25 public function __construct(Agent $instance)
26 {
27 $this->instance = $instance;
28 }
29
30 /**
31 * Paginated list of agents.
32 *
33 * @return object
34 */
35
36 public function getAgents()
37 {
38 return Agent::paginate();
39 }
40
41 /**
42 * @param int $agentId
43 * @return mixed
44 */
45
46 public function getAgent($agentId)
47 {
48 if (is_numeric($agentId)) {
49 return Agent::findOrFail($agentId);
50 }
51 return false;
52 }
53
54 /**
55 * @param array $data
56 * @param int $agentId
57 * @return mixed
58 */
59
60 public function updateAgent(array $data, $agentId)
61 {
62 if (!$agentId) {
63 return false;
64 }
65 if ($agent = Agent::where('id', $agentId)->first()) {
66 return $agent->update($data);
67 }
68 return false;
69 }
70
71 /**
72 * Create an agent, optionally creating the WP user first.
73 *
74 * @param array $data
75 * @param bool $createWpUser
76 * @return mixed
77 */
78
79 public function createAgentWithOrWithoutWpUser(array $data, $createWpUser = false)
80 {
81 if (!$createWpUser) {
82 $isExist = Agent::where('email', $data['email'])->first();
83 if (!$data['email'] || !is_email($data['email']) || $isExist) {
84 return false;
85 }
86 return Agent::create($data);
87 }
88
89 if (!username_exists($data['username'])) {
90 $authController = new AuthController();
91 $createdUser = $authController->createUser($data);
92 $authController->maybeUpdateUser($createdUser, $data);
93 if ($createdUser) {
94 $data['user_id'] = $createdUser;
95 return Agent::create($data);
96
97 }
98 }
99
100 return false;
101 }
102
103 /**
104 * @param int $id
105 */
106 public function deleteAgent($id)
107 {
108 if (!$id) {
109 return;
110 }
111 Agent::findOrFail($id)->delete();
112 }
113
114 public function getInstance()
115 {
116 return $this->instance;
117 }
118
119 public function __call($method, $params)
120 {
121 if (in_array($method, $this->allowedInstanceMethods)) {
122 return call_user_func_array([$this->instance, $method], $params);
123 }
124
125 throw new \Exception(sprintf('Method %s does not exist.', esc_html($method)));
126 }
127 }
128