Completed
Pull Request — master (#548)
by Albin
05:45
created

StorageFailure::unexpectedFailure()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 9
nc 1
nop 3
1
<?php
2
3
namespace Gaufrette\Exception;
4
5
use Gaufrette\Exception;
6
7
/**
8
 * Exception thrown when an unexpected error happened at the storage level (or its underlying sdk).
9
 *
10
 * @author Albin Kerouanton <[email protected]>
11
 */
12
class StorageFailure extends \RuntimeException implements Exception
13
{
14
    /**
15
     * Instantiate a new StorageFailure exception for a particular adapter action.
16
     *
17
     * @param string          $action    The adapter action (e.g read, write, listKeys, ...) that throw the exception.
18
     * @param array           $args      Arguments used by the action (like the read key).
19
     * @param \Exception|null $previous  Previous exception, if any was thrown (like exception from AWS sdk).
20
     *
21
     * @return StorageFailure
22
     */
23
    public static function unexpectedFailure($action, array $args, \Exception $previous = null)
24
    {
25
        $args = array_map(function ($k, $v) {
26
            $v = is_string($v) ? '"'.$v.'"' : $v;
27
28
            return "{$k}: {$v}";
29
        }, array_keys($args), $args);
30
31
        return new self(
32
            sprintf('An unexpected error happened during %s (%s).', $action, implode(', ', $args)),
33
            0,
34
            $previous
35
        );
36
    }
37
}
38