|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Cyberfusion\ClusterApi\Endpoints; |
| 4 | + |
| 5 | +use Cyberfusion\ClusterApi\Exceptions\RequestException; |
| 6 | +use Cyberfusion\ClusterApi\Models\MailHostname; |
| 7 | +use Cyberfusion\ClusterApi\Request; |
| 8 | +use Cyberfusion\ClusterApi\Response; |
| 9 | +use Cyberfusion\ClusterApi\Support\ListFilter; |
| 10 | + |
| 11 | +class MailHostnames extends Endpoint |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @param ListFilter|null $filter |
| 15 | + * @return Response |
| 16 | + * @throws RequestException |
| 17 | + */ |
| 18 | + public function list(ListFilter $filter = null): Response |
| 19 | + { |
| 20 | + if (is_null($filter)) { |
| 21 | + $filter = new ListFilter(); |
| 22 | + } |
| 23 | + |
| 24 | + $request = (new Request()) |
| 25 | + ->setMethod(Request::METHOD_GET) |
| 26 | + ->setUrl(sprintf('mail-hostnames?%s', $filter->toQuery())); |
| 27 | + |
| 28 | + $response = $this |
| 29 | + ->client |
| 30 | + ->request($request); |
| 31 | + if (!$response->isSuccess()) { |
| 32 | + return $response; |
| 33 | + } |
| 34 | + |
| 35 | + return $response->setData([ |
| 36 | + 'mailHostnames' => array_map( |
| 37 | + function (array $data) { |
| 38 | + return (new MailHostname())->fromArray($data); |
| 39 | + }, |
| 40 | + $response->getData() |
| 41 | + ), |
| 42 | + ]); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @param int $id |
| 47 | + * @return Response |
| 48 | + * @throws RequestException |
| 49 | + */ |
| 50 | + public function get(int $id): Response |
| 51 | + { |
| 52 | + $request = (new Request()) |
| 53 | + ->setMethod(Request::METHOD_GET) |
| 54 | + ->setUrl(sprintf('mail-hostnames/%d', $id)); |
| 55 | + |
| 56 | + $response = $this |
| 57 | + ->client |
| 58 | + ->request($request); |
| 59 | + if (!$response->isSuccess()) { |
| 60 | + return $response; |
| 61 | + } |
| 62 | + |
| 63 | + return $response->setData([ |
| 64 | + 'mailHostname' => (new MailHostname())->fromArray($response->getData()), |
| 65 | + ]); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @param MailHostname $mailHostname |
| 70 | + * @return Response |
| 71 | + * @throws RequestException |
| 72 | + */ |
| 73 | + public function create(MailHostname $mailHostname): Response |
| 74 | + { |
| 75 | + $this->validateRequired($mailHostname, 'create', [ |
| 76 | + 'domain', |
| 77 | + 'cluster_id', |
| 78 | + ]); |
| 79 | + |
| 80 | + $request = (new Request()) |
| 81 | + ->setMethod(Request::METHOD_POST) |
| 82 | + ->setUrl('mail-hostnames') |
| 83 | + ->setBody($this->filterFields($mailHostname->toArray(), [ |
| 84 | + 'domain', |
| 85 | + 'certificate_id', |
| 86 | + 'cluster_id', |
| 87 | + ])); |
| 88 | + |
| 89 | + $response = $this |
| 90 | + ->client |
| 91 | + ->request($request); |
| 92 | + if (!$response->isSuccess()) { |
| 93 | + return $response; |
| 94 | + } |
| 95 | + |
| 96 | + $mailHostname = (new MailHostname())->fromArray($response->getData()); |
| 97 | + |
| 98 | + // Log which cluster is affected by this change |
| 99 | + $this |
| 100 | + ->client |
| 101 | + ->addAffectedCluster($mailHostname->getClusterId()); |
| 102 | + |
| 103 | + return $response->setData([ |
| 104 | + 'mailHostname' => $mailHostname, |
| 105 | + ]); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @param MailHostname $mailHostname |
| 110 | + * @return Response |
| 111 | + * @throws RequestException |
| 112 | + */ |
| 113 | + public function update(MailHostname $mailHostname): Response |
| 114 | + { |
| 115 | + $this->validateRequired($mailHostname, 'update', [ |
| 116 | + 'domain', |
| 117 | + 'cluster_id', |
| 118 | + 'id', |
| 119 | + ]); |
| 120 | + |
| 121 | + $request = (new Request()) |
| 122 | + ->setMethod(Request::METHOD_PUT) |
| 123 | + ->setUrl(sprintf('mail-hostnames/%d', $mailHostname->getId())) |
| 124 | + ->setBody($this->filterFields($mailHostname->toArray(), [ |
| 125 | + 'domain', |
| 126 | + 'certificate_id', |
| 127 | + 'cluster_id', |
| 128 | + 'id', |
| 129 | + ])); |
| 130 | + |
| 131 | + $response = $this |
| 132 | + ->client |
| 133 | + ->request($request); |
| 134 | + if (!$response->isSuccess()) { |
| 135 | + return $response; |
| 136 | + } |
| 137 | + |
| 138 | + $mailHostname = (new MailHostname())->fromArray($response->getData()); |
| 139 | + |
| 140 | + // Log which cluster is affected by this change |
| 141 | + $this |
| 142 | + ->client |
| 143 | + ->addAffectedCluster($mailHostname->getClusterId()); |
| 144 | + |
| 145 | + return $response->setData([ |
| 146 | + 'mailHostname' => $mailHostname, |
| 147 | + ]); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * @param int $id |
| 152 | + * @return Response |
| 153 | + * @throws RequestException |
| 154 | + */ |
| 155 | + public function delete(int $id): Response |
| 156 | + { |
| 157 | + // Log the affected cluster by retrieving the model first |
| 158 | + $result = $this->get($id); |
| 159 | + if ($result->isSuccess()) { |
| 160 | + $clusterId = $result |
| 161 | + ->getData('mailHostname') |
| 162 | + ->getClusterId(); |
| 163 | + |
| 164 | + $this |
| 165 | + ->client |
| 166 | + ->addAffectedCluster($clusterId); |
| 167 | + } |
| 168 | + |
| 169 | + $request = (new Request()) |
| 170 | + ->setMethod(Request::METHOD_DELETE) |
| 171 | + ->setUrl(sprintf('mail-hostnames/%d', $id)); |
| 172 | + |
| 173 | + return $this |
| 174 | + ->client |
| 175 | + ->request($request); |
| 176 | + } |
| 177 | +} |
0 commit comments