DeleteOperation::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 1
nc 1
nop 3
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\Model\Operation;
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 sprintf;
17
18
final class DeleteOperation implements DeleteOperationInterface
19
{
20
    private RequestBuilder $requestBuilder;
21
22
    private string $entityType;
23
24
    /**
25
     * @var string[]
26
     */
27
    private array $arguments;
28
29
    public function __construct(
30
        RequestBuilder $requestBuilder,
31
        string $entityType,
32
        array $arguments = []
33
    ) {
34
        $this->requestBuilder = $requestBuilder;
35
        $this->entityType = $entityType;
36
        $this->arguments = $arguments;
37
    }
38
39
    public function delete(int $entityId): void
40
    {
41
        try {
42
            $this->requestBuilder
43
                ->setEntityType($this->entityType)
44
                ->setMethod(RequestBuilder::HTTP_DELETE)
45
                ->setArguments(array_merge([$entityId], $this->arguments))
46
                ->create()
47
                ->execute();
48
        } catch (InvalidArgumentException $e) {
49
            throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e);
50
        } catch (InvalidRequestException $e) {
51
            throw new CouldNotDeleteException($e->getMessage(), $e->getCode(), $e);
52
        } catch (Exception $e) {
53
            throw new CouldNotDeleteException(
54
                sprintf('Could not delete the entity with ID "%u".', $entityId),
55
                $e->getCode(),
56
                $e
57
            );
58
        }
59
    }
60
}
61