Completed
Push — master ( a55e81...4af1ba )
by Nate
03:49 queued 02:27
created

ErrorHelper::interpretSObjectError()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 0
cts 17
cp 0
rs 8.6737
c 0
b 0
f 0
cc 6
eloc 13
nc 10
nop 3
crap 42
1
<?php
2
3
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/salesforce/blob/master/LICENSE.md
6
 * @link       https://github.com/flipbox/salesforce
7
 */
8
9
namespace Flipbox\Salesforce\Helpers;
10
11
use Flipbox\Skeleton\Helpers\ArrayHelper;
12
13
/**
14
 * @author Flipbox Factory <[email protected]>
15
 * @since 1.0.0
16
 */
17
class ErrorHelper
18
{
19
    /**
20
     * @param $errorMessage
21
     * @return string|null
22
     */
23
    public static function getFieldNameFromMessage(&$errorMessage)
24
    {
25
        // Get the field label between the single quotes
26
        if (preg_match_all('~\'(.*?)\'~', $errorMessage, $m)) {
27
            return ArrayHelper::getFirstValue($m[1]);
28
        }
29
30
        return null;
31
    }
32
33
    /**
34
     * @param $errorMessage
35
     * @return string|null
36
     */
37
    public static function getFieldNamesFromRequiredMessage(&$errorMessage)
38
    {
39
        // Get the field label between the single quotes
40
        if (preg_match_all('~\[(.*?)\]~', $errorMessage, $m)) {
41
            $errorMessage = "Required field missing";
42
            return $m[1];
43
        }
44
45
        return null;
46
    }
47
48
    /**
49
     * @param array $errors
50
     * @return bool
51
     */
52
    public static function hasSessionExpired(array $errors): bool
53
    {
54
        foreach ($errors as $error) {
55
            if (ArrayHelper::getValue($error, 'errorCode') === 'INVALID_SESSION_ID' &&
56
                ArrayHelper::getValue($error, 'message') === 'Session expired or invalid'
57
            ) {
58
                return true;
59
            }
60
        }
61
62
        return false;
63
    }
64
65
    /**
66
     * @param string $errorMessage
67
     * @param string $errorCode
68
     * @param array $fields
69
     * @return array
70
     */
71
    public static function interpretSObjectError(string $errorMessage, string $errorCode, array $fields = []): array
72
    {
73
        $errorKeys = ($fields ?: $errorCode);
74
75
        switch ($errorCode) {
76
            // error message looks similar to: No such column 'Foo' on sobject of type Bar
77
            case 'INVALID_FIELD':
78
                $errorKeys = static::getFieldNameFromMessage($errorMessage);
79
                break;
80
81
            case 'REQUIRED_FIELD_MISSING':
82
                $errorKeys = static::getFieldNamesFromRequiredMessage($errorMessage);
83
                break;
84
85
            case 'FIELD_CUSTOM_VALIDATION_EXCEPTION':
86
                if (empty($fields)) {
87
                    $errorKeys = $errorCode;
88
                }
89
        }
90
91
        return [$errorKeys, $errorMessage, $errorCode];
92
    }
93
}
94