Passed
Pull Request — master (#8)
by Thomas
11:44 queued 13s
created

UpdateOperation   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 30
c 1
b 0
f 0
dl 0
loc 64
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A update() 0 17 5
A saveEntity() 0 9 2
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\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 reset;
20
use function sprintf;
21
22
final class UpdateOperation implements UpdateOperationInterface
23
{
24
    private RequestBuilder $requestBuilder;
25
26
    private DataObjectFactory $dataObjectFactory;
27
28
    private string $entityType;
29
30
    private ?string $path;
31
32
    /**
33
     * @var string[]
34
     */
35
    private array $arguments;
36
37
    public function __construct(
38
        RequestBuilder $requestBuilder,
39
        DataObjectFactory $dataObjectFactory,
40
        string $entityType,
41
        ?string $path = null,
42
        array $arguments = []
43
    ) {
44
        $this->requestBuilder = $requestBuilder;
45
        $this->dataObjectFactory = $dataObjectFactory;
46
        $this->entityType = $entityType;
47
        $this->path = $path;
48
        $this->arguments = $arguments;
49
    }
50
51
    public function update(DataObjectInterface $dataObject, array $bind = []): DataObjectInterface
52
    {
53
        if (!$dataObject->getEntityId()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $dataObject->getEntityId() of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
54
            throw new CouldNotSaveException('Could not update an entity without ID.');
55
        }
56
57
        try {
58
            return $this->dataObjectFactory->create($this->entityType, $this->saveEntity($dataObject, $bind)->getResult());
59
        } catch (InvalidArgumentException $e) {
60
            throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e);
61
        } catch (InvalidRequestException $e) {
62
            throw new CouldNotSaveException($e->getMessage(), $e->getCode(), $e);
63
        } catch (Exception $e) {
64
            throw new CouldNotSaveException(
65
                sprintf('Could not update the entity with ID "%u".', $dataObject->getEntityId()),
66
                $e->getCode(),
67
                $e
68
            );
69
        }
70
    }
71
72
    /**
73
     * @throws Exception
74
     * @throws InvalidArgumentException
75
     * @throws InvalidRequestException
76
     */
77
    private function saveEntity(DataObjectInterface $dataObject, array $bind = []): ResponseInterface
78
    {
79
        return $this->requestBuilder
80
            ->setPath($this->path ?? $this->entityType, $bind)
81
            ->setMethod(RequestBuilder::HTTP_PATCH)
82
            ->setArguments($this->path ? $this->arguments : array_merge([reset($bind)], $this->arguments))
83
            ->setFields($dataObject->toArray())
84
            ->create()
85
            ->execute();
86
    }
87
}
88