Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
36 / 36 |
Ovh | |
100.00% |
1 / 1 |
|
100.00% |
4 / 4 |
12 | |
100.00% |
36 / 36 |
manageOvhRecord | |
100.00% |
1 / 1 |
6 | |
100.00% |
21 / 21 |
|||
getOvhDomains | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
getOvhZoneRecords | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
createOvhZoneRecord | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\Entities\Services\Ovh; |
4 | |
5 | use Illuminate\Support\Facades\App; |
6 | use Illuminate\Support\Facades\Log; |
7 | use Illuminate\Support\Str; |
8 | use Qmp\Laravel\MicroService\Client\Client; |
9 | use Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
10 | |
11 | class Ovh |
12 | { |
13 | const SERVICE_NAME = 'service_ovh'; |
14 | |
15 | /** |
16 | * Manage url record on ovh |
17 | * |
18 | * @param string $url |
19 | * @throws \Exception |
20 | */ |
21 | public static function manageOvhRecord(string $url) |
22 | { |
23 | if (Str::startsWith($url, 'http')) { |
24 | $url = parse_url($url, PHP_URL_HOST); |
25 | } |
26 | |
27 | $parse = explode('.', $url); |
28 | $countParse = count($parse); |
29 | |
30 | if($countParse > 2) { |
31 | $extension = array_pop($parse); |
32 | $domain = array_pop($parse); |
33 | $zone = $domain .'.'. $extension; |
34 | $subDomain = implode('.', $parse); |
35 | |
36 | $domains = Ovh::getOvhDomains(); |
37 | |
38 | if(in_array($zone, $domains)) { |
39 | $record = collect(Ovh::getOvhZoneRecords($zone))->where('subDomain', $subDomain)->first(); |
40 | |
41 | if (is_null($record)) { |
42 | Log::debug('No record found on service_ovh'); |
43 | |
44 | if(App::environment() === 'production') { |
45 | self::createOvhZoneRecord($zone,[ |
46 | 'fieldType' => 'A', |
47 | 'subDomain' => $subDomain, |
48 | 'target' => $zone, |
49 | 'ttl' => null, |
50 | ]); |
51 | } |
52 | } |
53 | } |
54 | |
55 | } else { |
56 | throw new \Exception('Invalid external front url'); |
57 | } |
58 | } |
59 | |
60 | /** |
61 | * get all domains on ovh |
62 | * |
63 | * @return mixed|null |
64 | * @throws \Exception |
65 | */ |
66 | public static function getOvhDomains() |
67 | { |
68 | $ClientRequest = ClientRequest::createObject(self::SERVICE_NAME, 'domain/'); |
69 | $response = Client::systemSend('get', $ClientRequest); |
70 | |
71 | if(!Str::startsWith($response->code, '2')) { |
72 | throw new \Exception('Unable to get all zones'); |
73 | } |
74 | return $response->content; |
75 | } |
76 | |
77 | /** |
78 | * Get All records of zone on ovh |
79 | * |
80 | * @param string $zone |
81 | * @return mixed|null |
82 | * @throws \Exception |
83 | */ |
84 | public static function getOvhZoneRecords(string $zone) |
85 | { |
86 | $ClientRequest = ClientRequest::createObject(self::SERVICE_NAME, 'domain/' . $zone . '/records'); |
87 | $response = Client::systemSend('get', $ClientRequest); |
88 | |
89 | if (!Str::startsWith($response->code, '2')) { |
90 | throw new \Exception('Unable to get domain zones'); |
91 | } |
92 | |
93 | return $response->content; |
94 | } |
95 | |
96 | /** |
97 | * Create zone's record on ovh |
98 | * |
99 | * @param string $zone |
100 | * @param array $data |
101 | * @return mixed|null |
102 | * @throws \Exception |
103 | */ |
104 | public static function createOvhZoneRecord(string $zone, array $data) |
105 | { |
106 | $ClientRequest = ClientRequest::createObject(self::SERVICE_NAME, 'domain/' . $zone . '/records', ['body' => $data]); |
107 | $response = Client::systemSend('post', $ClientRequest); |
108 | |
109 | if(!Str::startsWith($response->code, '2')) { |
110 | throw new \Exception('Unable to create domain record'); |
111 | } |
112 | |
113 | return $response->content; |
114 | } |
115 | } |