Passed
Push — master ( 5a87d9...5af04f )
by Mihail
03:04
created

CacheException::withConnectionErrorFor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/*
4
 * This file is part of the Koded package.
5
 *
6
 * (c) Mihail Binev <[email protected]>
7
 *
8
 * Please view the LICENSE distributed with this source code
9
 * for the full copyright and license information.
10
 *
11
 */
12
13
namespace Koded\Caching;
14
15
use Exception;
16
use Koded\Exceptions\KodedException;
17
use Psr\SimpleCache\InvalidArgumentException;
18
19
20
class CacheException extends KodedException implements InvalidArgumentException
21
{
22
    protected $messages = [
23
        Cache::E_INVALID_KEY => 'The cache key is invalid, given (:type) :key',
24
        Cache::E_UNSUPPORTED_LOGGER => 'The cache logger should be NULL or an instance of :supported, given :given',
25
        Cache::E_DIRECTORY_NOT_CREATED => 'Failed to create a cache directory ":dir"',
26
        Cache::E_PHP_EXCEPTION => '[Cache Exception] :message',
27
        Cache::E_CONNECTION_ERROR => '[Cache Exception] Failed to connect the :client client',
28
        Cache::E_UNSUPPORTED_CLIENT => '[Cache Exception] Unsupported cache client :name',
29
    ];
30
31 977
    public static function forInvalidKey($key)
32
    {
33 977
        return new self(Cache::E_INVALID_KEY, [':key' => var_export($key, true), ':type' => gettype($key)]);
34
    }
35
36
37 1
    public static function forUnsupportedLogger(string $supported, string $given)
38
    {
39 1
        return new self(Cache::E_UNSUPPORTED_LOGGER, [':supported' => $supported, ':given' => $given]);
40
    }
41
42
43 1
    public static function forCreatingDirectory(string $directory)
44
    {
45 1
        return new self(Cache::E_DIRECTORY_NOT_CREATED, [':dir' => $directory]);
46
    }
47
48
49 161
    public static function generic(string $message, Exception $previous = null)
50
    {
51 161
        return new self(Cache::E_PHP_EXCEPTION, [':message' => $message], $previous);
52
    }
53
54
55 2
    public static function withConnectionErrorFor(string $clientName)
56
    {
57 2
        return new self(Cache::E_CONNECTION_ERROR, [':client' => $clientName]);
58
    }
59
60 1
    public static function forUnsupportedClient(string $client)
61
    {
62 1
        return new self(Cache::E_UNSUPPORTED_CLIENT, [':name' => $client]);
63
    }
64
}
65