1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Thomas Klein, All rights reserved. |
4
|
|
|
* See LICENSE bundled with this library for license details. |
5
|
|
|
*/ |
6
|
|
|
declare(strict_types=1); |
7
|
|
|
|
8
|
|
|
namespace Zoho\Desk\Service; |
9
|
|
|
|
10
|
|
|
use Zoho\Desk\Client\RequestBuilder; |
11
|
|
|
use Zoho\Desk\Exception\CouldNotDeleteException; |
12
|
|
|
use Zoho\Desk\Exception\Exception; |
13
|
|
|
use Zoho\Desk\Exception\InvalidArgumentException; |
14
|
|
|
use Zoho\Desk\Exception\InvalidRequestException; |
15
|
|
|
use function array_merge; |
16
|
|
|
use function reset; |
17
|
|
|
use function rtrim; |
18
|
|
|
use function sprintf; |
19
|
|
|
|
20
|
|
|
final class DeleteOperation implements DeleteOperationInterface |
21
|
|
|
{ |
22
|
|
|
private RequestBuilder $requestBuilder; |
23
|
|
|
|
24
|
|
|
private string $entityType; |
25
|
|
|
|
26
|
|
|
private ?string $path; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
private array $arguments; |
32
|
|
|
|
33
|
|
|
public function __construct( |
34
|
|
|
RequestBuilder $requestBuilder, |
35
|
|
|
string $entityType, |
36
|
|
|
?string $path = null, |
37
|
|
|
array $arguments = [] |
38
|
|
|
) { |
39
|
|
|
$this->requestBuilder = $requestBuilder; |
40
|
|
|
$this->entityType = $entityType; |
41
|
|
|
$this->path = $path; |
42
|
|
|
$this->arguments = $arguments; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function delete(array $bind): void |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
$this->requestBuilder |
49
|
|
|
->setPath($this->path ?? $this->entityType, $bind) |
50
|
|
|
->setMethod(RequestBuilder::HTTP_DELETE) |
51
|
|
|
->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments)) |
52
|
|
|
->create() |
53
|
|
|
->execute(); |
54
|
|
|
} catch (InvalidArgumentException $e) { |
55
|
|
|
throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e); |
56
|
|
|
} catch (InvalidRequestException $e) { |
57
|
|
|
throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e); |
58
|
|
|
} catch (Exception $e) { |
59
|
|
|
$flatten = ''; |
60
|
|
|
foreach ($bind as $key => $value) { |
61
|
|
|
$flatten .= sprintf('%s: %s ', $key, $value); |
62
|
|
|
} |
63
|
|
|
throw new CouldNotDeleteException( |
64
|
|
|
sprintf('Could not delete the entity with %s.', rtrim($flatten)), |
65
|
|
|
$e->getCode(), |
66
|
|
|
$e |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|