Passed
Pull Request — master (#8)
by Thomas
01:47
created

ListOperation::buildEntities()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 4
nc 2
nop 1
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\CouldNotReadException;
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 Zoho\Desk\Model\ListCriteriaInterface;
19
use function array_merge;
20
use function is_array;
21
22
final class ListOperation implements ListOperationInterface
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 getList(ListCriteriaInterface $listCriteria, array $bind = []): array
52
    {
53
        $arguments = $listCriteria->getFilters() ? array_merge(['search'], $this->arguments) : $this->arguments;
54
55
        try {
56
            $response = $this->fetchResult($arguments, $listCriteria->getQueryParams(), $bind);
57
        } catch (InvalidArgumentException $e) {
58
            throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e);
59
        } catch (InvalidRequestException $e) {
60
            throw new CouldNotReadException($e->getMessage(), $e->getCode(), $e);
61
        } catch (Exception $e) {
62
            throw new CouldNotReadException('Could not fetch the entities.', $e->getCode(), $e);
63
        }
64
65
        return $this->buildEntities($response);
66
    }
67
68
    /**
69
     * @return DataObjectInterface[]
70
     */
71
    private function buildEntities(ResponseInterface $response): array
72
    {
73
        $entities = [];
74
        $result = $response->getResult();
75
        if (isset($result['data']) && is_array($result['data'])) {
76
            foreach ($result['data'] as $entity) {
77
                $entities[] = $this->dataObjectFactory->create($this->entityType, $entity);
78
            }
79
        }
80
81
        return $entities;
82
    }
83
84
    /**
85
     * @throws Exception
86
     * @throws InvalidArgumentException
87
     * @throws InvalidRequestException
88
     */
89
    private function fetchResult(array $arguments, array $params = [], array $bind = []): ResponseInterface
90
    {
91
        return $this->requestBuilder
92
            ->setPath($this->path ?? $this->entityType, $bind)
93
            ->setMethod(RequestBuilder::HTTP_GET)
94
            ->setArguments($arguments)
95
            ->setQueryParameters($params)
96
            ->create()
97
            ->execute();
98
    }
99
}
100