Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiException
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 7
156
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 setApiError
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
30
 getApiError
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isApiError
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getApiErrorCode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getApiErrorDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getApiDetailedErrorDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * EBSCO API Exception class
5 *
6 * PHP version 8
7 *
8 * Copyright (C) EBSCO Industries 2013
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category EBSCOIndustries
24 * @package  EBSCO
25 * @author   Michelle Milton <mmilton@epnet.com>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org
28 */
29
30namespace VuFindSearch\Backend\EDS;
31
32use function count;
33use function is_array;
34
35/**
36 * EBSCO API Exception class
37 *
38 * @category EBSCOIndustries
39 * @package  EBSCO
40 * @author   Michelle Milton <mmilton@epnet.com>
41 * @author   Cornelius Amzar <cornelius.amzar@bsz-bw.de>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org
44 */
45class ApiException extends \VuFindSearch\Backend\Exception\BackendException
46{
47    /**
48     * Error message details returned from the API
49     *
50     * @var array
51     */
52    protected $apiErrorDetails = [];
53
54    /**
55     * Constructor
56     *
57     * @param array $apiErrorMessage Error message
58     */
59    public function __construct($apiErrorMessage)
60    {
61        if (is_array($apiErrorMessage)) {
62            $this->setApiError($apiErrorMessage);
63            parent::__construct($this->apiErrorDetails['Description'] ?? '');
64        } else {
65            parent::__construct($apiErrorMessage);
66        }
67    }
68
69    /**
70     * Set the api error details into an array
71     *
72     * @param array $message Error message
73     *
74     * @return void
75     */
76    protected function setApiError($message)
77    {
78        if (isset($message['ErrorCode'])) {
79            // AuthErrorMessages
80            $this->apiErrorDetails['ErrorCode'] = $message['ErrorCode'];
81            $this->apiErrorDetails['Description'] = $message['Reason'];
82            $this->apiErrorDetails['DetailedDescription']
83                = $message['AdditionalDetail'];
84        } elseif (isset($message['ErrorNumber'])) {
85            // EDSAPI error messages
86            $this->apiErrorDetails['ErrorCode'] = $message['ErrorNumber'];
87            $this->apiErrorDetails['Description'] = $message['ErrorDescription'];
88            $this->apiErrorDetails['DetailedDescription']
89                = $message['DetailedErrorDescription'];
90        } elseif (
91            is_array($message['errors'] ?? null)
92            && count($message['errors']) > 0
93        ) {
94            // Array of errors
95            $this->apiErrorDetails['ErrorCode'] = $message['errors'][0]['code'];
96            $this->apiErrorDetails['Description'] = $message['errors'][0]['message'];
97        } else {
98            $this->apiErrorDetails['ErrorCode'] = null;
99            $this->apiErrorDetails['Description'] = 'unrecognized error';
100        }
101    }
102
103    /**
104     * Get the Api Error message details.
105     *
106     * @return array
107     */
108    public function getApiError()
109    {
110        return $this->apiErrorDetails;
111    }
112
113    /**
114     * Is this a know api error
115     *
116     * @return bool
117     */
118    public function isApiError()
119    {
120        return isset($this->apiErrorDetails);
121    }
122
123    /**
124     * Known api error code
125     *
126     * @return array
127     */
128    public function getApiErrorCode()
129    {
130        return $this->apiErrorDetails['ErrorCode'] ?? '';
131    }
132
133    /**
134     * Known api error description
135     *
136     * @return string
137     */
138    public function getApiErrorDescription()
139    {
140        return $this->apiErrorDetails['Description'] ?? '';
141    }
142
143    /**
144     * Known api detailed error description
145     *
146     * @return string
147     */
148    public function getApiDetailedErrorDescription()
149    {
150        return $this->apiErrorDetails['DetailedDescription'] ?? '';
151    }
152}