File size: 2,470 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php

namespace MauticPlugin\MauticCrmBundle\Integration\Salesforce;

use Mautic\PluginBundle\Exception\ApiErrorException;
use Psr\Log\LoggerInterface;

class ResultsPaginator
{
    /**
     * @var array
     */
    private $results;

    /**
     * @var int
     */
    private $totalRecords = 0;

    private int $recordCount = 0;

    private int $retryCount = 0;

    /**
     * @var string|null
     */
    private $nextRecordsUrl;

    /**
     * @param string $salesforceBaseUrl
     */
    public function __construct(
        private LoggerInterface $logger,
        private $salesforceBaseUrl
    ) {
    }

    /**
     * @return $this
     *
     * @throws ApiErrorException
     */
    public function setResults(array $results)
    {
        if (!isset($results['records'])) {
            throw new ApiErrorException(var_export($results, true));
        }

        $this->results      = $results;
        $this->totalRecords = $results['totalSize'];
        $this->recordCount += count($results['records']);

        return $this;
    }

    /**
     * @return string
     *
     * @throws ApiErrorException
     */
    public function getNextResultsUrl()
    {
        if (isset($this->results['nextRecordsUrl'])) {
            $this->retryCount     = 0;
            $this->nextRecordsUrl = $this->results['nextRecordsUrl'];

            if (!str_contains($this->nextRecordsUrl, $this->salesforceBaseUrl)) {
                $this->nextRecordsUrl = $this->salesforceBaseUrl.$this->nextRecordsUrl;
            }

            return $this->nextRecordsUrl;
        }

        if ($this->recordCount < $this->totalRecords) {
            // Something has gone wrong so try a few more times before giving up
            if ($this->retryCount <= 5) {
                $this->logger->debug("SALESFORCE: Processed less than total but didn't get a nextRecordsUrl in the response: ".var_export($this->results, true));

                usleep(500);
                ++$this->retryCount;

                // Try again
                return $this->nextRecordsUrl;
            }

            // Throw an exception cause something isn't right
            throw new ApiErrorException("Expected to process {$this->totalRecords} but only processed {$this->recordCount}: ".var_export($this->results, true));
        }

        $this->nextRecordsUrl = null;

        return '';
    }

    public function getTotal(): int
    {
        return (int) $this->totalRecords;
    }
}