CloudFlareException::collectionErrors()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 7
cts 7
cp 1
cc 3
eloc 6
nc 4
nop 1
crap 3
1
<?php
2
declare(strict_types = 1);
3
/**
4
 *
5
 * Copyright (C) 2018  Ross Mitchell
6
 *
7
 * This file is part of RossMitchell/UpdateCloudFlare.
8
 *
9
 * RossMitchell/UpdateCloudFlare is free software: you can redistribute
10
 * it and/or modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
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, see <http://www.gnu.org/licenses/>.
21
 */
22
23
namespace RossMitchell\UpdateCloudFlare\Exceptions;
24
25
use RossMitchell\UpdateCloudFlare\Abstracts\CloudFlareResponse;
26
27
/**
28
 * Class CloudFlareException
29
 * @package RossMitchell\UpdateCloudFlare\Exceptions
30
 */
31
class CloudFlareException extends \Exception
32
{
33
    /**
34
     * This will parse the result from Cloud Flare and try to produce a meaningful error message
35
     *
36
     * @param CloudFlareResponse $details
37
     */
38 7
    public function setDetails(CloudFlareResponse $details): void
39
    {
40 7
        $errorDetail   = $this->collectionErrors($details);
41 7
        $call          = \get_class($details);
42 7
        $message       = "There was an error making the $call:" . \PHP_EOL . $errorDetail;
43 7
        $this->message = $message;
44 7
    }
45
46
    /**
47
     * This actually loops over the response and fetches the errors
48
     *
49
     * @param CloudFlareResponse $details
50
     *
51
     * @return string
52
     */
53 7
    private function collectionErrors(CloudFlareResponse $details): string
54
    {
55 7
        $errorMessages = [];
56
57 7
        foreach ($details->getErrors() as $error) {
58 4
            $errorMessages[] = $error->getMessage();
59
        }
60
61 7
        if (empty($errorMessages)) {
62 3
            $errorMessages[] = 'No error details returned';
63
        }
64
65 7
        return \implode(PHP_EOL, $errorMessages);
66
    }
67
}
68