Code

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