Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.90% covered (danger)
6.90%
8 / 116
9.09% covered (danger)
9.09%
1 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 2
InstancyUserDTO
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
InstancyRepository
6.96% covered (danger)
6.96%
8 / 115
10.00% covered (danger)
10.00%
1 / 10
411.86
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 request
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 updateUser
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 1
20
 updateUserEmail
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 deleteGroup
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 updateGroup
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
6
 createGroup
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 findGroups
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
 getGroups
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getGroup
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Http\Repositories;
4
5use Illuminate\Support\Facades\Http;
6
7class InstancyUserDTO
8{
9    public function __construct(
10        public readonly string $userId,
11        public readonly string $groupId,
12        public readonly string $firstName,
13        public readonly string $lastName,
14        public readonly string $email,
15        public readonly ?string $companyName,
16    ) {}
17}
18
19class InstancyRepository
20{
21    protected $baseUrl;
22
23    protected $apiKey;
24
25    protected $sso_url;
26
27    protected $siteGroupId;
28
29    protected $newUserPassword = 'FlyMSGWelcome#2024';
30
31    public function __construct()
32    {
33        $this->baseUrl = 'https://ondemand-admin.vengreso.com/InstancyService.asmx';
34        $this->apiKey = '22A9838A-2196-47ED-8126-4321B7A07EF1';
35        $this->sso_url = 'https://ondemand-master-admin.vengreso.com/InstancyService.asmx';
36        $this->siteGroupId = '388';
37
38        if (! isProduction() && ! isLocalProduction()) {
39            $this->baseUrl = 'https://ondemand-admin.instancy.net/instancyservice.asmx';
40            $this->apiKey = 'A58ABE98-1B0D-41FC-9805-0F34D6B99E80';
41            $this->sso_url = 'https://ondemand-master-admin.instancy.net/InstancyService.asmx';
42        }
43    }
44
45    protected function request($endpoint, $data)
46    {
47        $url = $this->baseUrl.'/'.$endpoint;
48
49        $response = Http::withHeaders([
50            'Accept-Encoding' => '',
51            'Content-Type' => 'application/x-www-form-urlencoded',
52        ])->asForm()
53            ->post($url, [
54                'astrKey' => $this->apiKey,
55                'astrXML' => $data,
56            ])->body();
57
58        $response = parseInstancyXml($response);
59
60        $response = parseInstancyXml($response['value']);
61
62        return $response;
63    }
64
65    public function updateUser(InstancyUserDTO $user)
66    {
67        $userId = $user->userId;
68        $groupId = $user->groupId;
69        $firstName = str_replace("'", ' ', $user->firstName);
70        $lastName = str_replace("'", ' ', $user->lastName);
71        $email = $user->email;
72        $status = 1;
73        $password = $this->newUserPassword;
74        $mobileNumber = '0000000000';
75        $jobTitle = 'User';
76        $companyName = $user->companyName ?? 'Vengreso';
77
78        if (! isProduction() && ! isLocalProduction()) {
79            $email = 'staging.'.$email;
80        }
81
82        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
83                <InstancyWrapper><Request SiteID=\"$this->siteGroupId\"><UserDetails>
84                    <UserID><![CDATA[$userId]]></UserID>
85                    <GroupID><![CDATA[$groupId]]></GroupID>
86                    <First_Name><![CDATA[$firstName]]></First_Name>
87                    <Last_Name><![CDATA[$lastName]]></Last_Name>
88                    <User_name><![CDATA[$email]]></User_name>
89                    <Email><![CDATA[$email]]></Email>
90                    <Password><![CDATA[$password]]></Password>
91                    <Mobile_Number><![CDATA[$mobileNumber]]></Mobile_Number>
92                    <Job_title><![CDATA[$jobTitle]]></Job_title>
93                    <Company_Name><![CDATA[$companyName]]></Company_Name>
94                    <Status><![CDATA[$status]]></Status>
95                </UserDetails></Request></InstancyWrapper>";
96
97        $group = $this->request('UpdateUser', $xml);
98
99        if (isset($group['UserDetails'][0])) {
100            return $group['UserDetails'][0]['UserID'][0]['value'];
101        }
102
103        return null;
104    }
105
106    public function updateUserEmail($groupData)
107    {
108        $instancyId = $groupData->instancyId;
109        $newEmail = $groupData->email;
110
111        if (! isProduction() && ! isLocalProduction()) {
112            $newEmail = 'staging.'.$newEmail;
113        }
114
115        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
116                <InstancyWrapper>
117                    <Request SiteID=\"$this->siteGroupId\">
118                        <UpdateUserEmail>
119                            <UserID><![CDATA[$instancyId]]></UserID>
120                            <EmailToBeUpdated><![CDATA[$newEmail]]></EmailToBeUpdated>
121                        </UpdateUserEmail>
122                    </Request>
123                </InstancyWrapper>";
124
125        $group = $this->request('/UpdateUserEmail', $xml);
126
127        // if (isset($group['Group'][0])) {
128        //     return $group['Group'][0]['GroupID'][0]['value'];
129        // }
130
131        return $group;
132    }
133
134    public function deleteGroup($groupData)
135    {
136        $groupId = $groupData->groupId;
137        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
138                <InstancyWrapper>
139                    <Request SiteID=\"$this->siteGroupId\">
140                        <DeleteGroup>
141                            <GroupID><![CDATA[$groupId]]></GroupID>
142                        </DeleteGroup>                        
143                    </Request>
144                </InstancyWrapper>";
145
146        $group = $this->request('/DeleteGroup', $xml);
147
148        if (isset($group['Group'][0])) {
149            return $group['Group'][0]['status'][0]['value'] == 'Group deleted successfully';
150        }
151
152        return false;
153    }
154
155    public function updateGroup($groupData)
156    {
157        $parentId = $groupData->parentId ?? $this->siteGroupId;
158        $groupName = str_replace("'", ' ', $groupData->name);
159        $groupId = $groupData->groupId;
160        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
161                <InstancyWrapper>
162                    <Request SiteID=\"$this->siteGroupId\">
163                        <UpdateGroup>
164                            <GroupID><![CDATA[$groupId]]></GroupID>
165                            <GroupName><![CDATA[$groupName]]></GroupName>
166                            <ParentID><![CDATA[$parentId]]></ParentID>
167                        </UpdateGroup>
168                    </Request>
169                </InstancyWrapper>";
170
171        $group = $this->request('/UpdateGroup', $xml);
172
173        if (isset($group['Group'][0])) {
174            return $group['Group'][0]['status'][0]['value'] == 'Updated successfully';
175        }
176
177        return false;
178    }
179
180    public function createGroup($groupData)
181    {
182        $parentId = $groupData->parentId ?? $this->siteGroupId;
183        $groupName = str_replace("'", ' ', $groupData->name);
184        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
185                <InstancyWrapper>
186                    <Request SiteID=\"$this->siteGroupId\">
187                        <CreateGroup>
188                            <GroupID><![CDATA[-1]]></GroupID>
189                            <GroupName><![CDATA[$groupName]]></GroupName>
190                            <ParentID><![CDATA[$parentId]]></ParentID>
191                        </CreateGroup>
192                    </Request>
193                </InstancyWrapper>";
194
195        $group = $this->request('/CreateGroup', $xml);
196
197        if (isset($group['Group'][0])) {
198            return $group['Group'][0]['GroupID'][0]['value'];
199        }
200
201        return null;
202    }
203
204    public function findGroups()
205    {
206        $xml = "<?xml version='1.0' encoding='iso-8859-1'?>
207                <InstancyWrapper>
208                    <Request SiteID=\"$this->siteGroupId\">
209                        <GroupList>
210                            <siteid>
211                                <![CDATA[$this->siteGroupId]]>
212                            </siteid>
213                        </GroupList>
214                    </Request>
215                </InstancyWrapper>";
216
217        $groups = $this->request('/GetGroupListing', $xml);
218
219        return array_map(function ($group) {
220            return json_decode(json_encode([
221                'id' => $group['GroupID'][0]['value'],
222                'name' => $group['Name'][0]['value'],
223                'parentId' => $group['ParentID'][0]['value'],
224            ]));
225        }, $groups['GroupList'][0]['Group']);
226    }
227
228    public function getGroups($option, $searchName)
229    {
230        $groups = $this->findGroups();
231
232        return collect($groups)->where($option, $searchName);
233    }
234
235    public function getGroup($searchName, $company, $parentId)
236    {
237        $groups = $this->findGroups();
238
239        $query = collect($groups)->where('name', $searchName);
240
241        if ($company) {
242            $query = $query->where('parentId', $company);
243        } elseif ($parentId) {
244            $query = $query->where('parentId', $parentId);
245        }
246
247        return $query->first();
248    }
249}