thomas-kl1 /
php-sdk-zoho-desk
| 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\Client\ResponseInterface; |
||
| 12 | use Zoho\Desk\Exception\CouldNotSaveException; |
||
| 13 | use Zoho\Desk\Exception\Exception; |
||
| 14 | use Zoho\Desk\Exception\InvalidArgumentException; |
||
| 15 | use Zoho\Desk\Exception\InvalidRequestException; |
||
| 16 | use Zoho\Desk\Model\DataObjectFactory; |
||
| 17 | use Zoho\Desk\Model\DataObjectInterface; |
||
| 18 | use function array_merge; |
||
| 19 | use function sprintf; |
||
| 20 | |||
| 21 | final class UpdateOperation implements UpdateOperationInterface |
||
| 22 | { |
||
| 23 | private RequestBuilder $requestBuilder; |
||
| 24 | |||
| 25 | private DataObjectFactory $dataObjectFactory; |
||
| 26 | |||
| 27 | private string $entityType; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string[] |
||
| 31 | */ |
||
| 32 | private array $arguments; |
||
| 33 | |||
| 34 | public function __construct( |
||
| 35 | RequestBuilder $requestBuilder, |
||
| 36 | DataObjectFactory $dataObjectFactory, |
||
| 37 | string $entityType, |
||
| 38 | array $arguments = [] |
||
| 39 | ) { |
||
| 40 | $this->requestBuilder = $requestBuilder; |
||
| 41 | $this->dataObjectFactory = $dataObjectFactory; |
||
| 42 | $this->entityType = $entityType; |
||
| 43 | $this->arguments = $arguments; |
||
| 44 | } |
||
| 45 | |||
| 46 | public function update(DataObjectInterface $dataObject): DataObjectInterface |
||
| 47 | { |
||
| 48 | if (!$dataObject->getEntityId()) { |
||
|
0 ignored issues
–
show
|
|||
| 49 | throw new CouldNotSaveException('Could not update an entity without ID.'); |
||
| 50 | } |
||
| 51 | |||
| 52 | try { |
||
| 53 | return $this->dataObjectFactory->create($this->entityType, $this->saveEntity($dataObject)->getResult()); |
||
| 54 | } catch (InvalidArgumentException $e) { |
||
| 55 | throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); |
||
| 56 | } catch (InvalidRequestException $e) { |
||
| 57 | throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e); |
||
| 58 | } catch (Exception $e) { |
||
| 59 | throw new CouldNotSaveException( |
||
| 60 | sprintf('Could not update the entity with ID "%u".', $dataObject->getEntityId()), |
||
| 61 | $e->getCode(), |
||
| 62 | $e |
||
| 63 | ); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param DataObjectInterface $dataObject |
||
| 69 | * @return ResponseInterface |
||
| 70 | * @throws Exception |
||
| 71 | * @throws InvalidArgumentException |
||
| 72 | * @throws InvalidRequestException |
||
| 73 | */ |
||
| 74 | private function saveEntity(DataObjectInterface $dataObject): ResponseInterface |
||
| 75 | { |
||
| 76 | return $this->requestBuilder |
||
| 77 | ->setEntityType($this->entityType) |
||
| 78 | ->setMethod(RequestBuilder::HTTP_PATCH) |
||
| 79 | ->setArguments(array_merge([$dataObject->getEntityId()], $this->arguments)) |
||
| 80 | ->setFields($dataObject->toArray()) |
||
| 81 | ->create() |
||
| 82 | ->execute(); |
||
| 83 | } |
||
| 84 | } |
||
| 85 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: