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

StorageFailure   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 26
c 0
b 0
f 0
wmc 2
lcom 0
cbo 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A unexpectedFailure() 0 14 2
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