1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Cubiche\Core\Bus\Exception; |
12
|
|
|
|
13
|
|
|
use RuntimeException; |
14
|
|
|
use Exception; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* NotFoundException class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class NotFoundException extends RuntimeException |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Creates an exception for a not found message name, method name or handler for a given message. |
25
|
|
|
* |
26
|
|
|
* @param string $type |
27
|
|
|
* @param mixed $message |
28
|
|
|
* @param int $code |
29
|
|
|
* @param Exception|null $cause |
30
|
|
|
* |
31
|
|
|
* @return NotFoundException |
32
|
|
|
*/ |
33
|
|
View Code Duplication |
protected static function notFound($type, $message, $code = 0, Exception $cause = null) |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
return new static(sprintf( |
36
|
|
|
'Not found a %s for a given object of type %s', |
37
|
|
|
$type, |
38
|
|
|
is_object($message) ? get_class($message) : gettype($message) |
39
|
|
|
), $code, $cause); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param mixed $object |
44
|
|
|
* @param Exception|null $cause |
45
|
|
|
* |
46
|
|
|
* @return NotFoundException |
47
|
|
|
*/ |
48
|
|
|
public static function nameOfMessage($object, Exception $cause = null) |
49
|
|
|
{ |
50
|
|
|
return self::notFound('name of message', $object, 1, $cause); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param mixed $object |
55
|
|
|
* @param Exception|null $cause |
56
|
|
|
* |
57
|
|
|
* @return NotFoundException |
58
|
|
|
*/ |
59
|
|
|
public static function nameOfCommand($object, Exception $cause = null) |
60
|
|
|
{ |
61
|
|
|
return self::notFound('name of command', $object, 2, $cause); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param mixed $object |
66
|
|
|
* @param Exception|null $cause |
67
|
|
|
* |
68
|
|
|
* @return NotFoundException |
69
|
|
|
*/ |
70
|
|
|
public static function handlerMethodNameForObject($object, Exception $cause = null) |
71
|
|
|
{ |
72
|
|
|
return self::notFound('handler method name', $object, 3, $cause); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $object |
77
|
|
|
* @param Exception|null $cause |
78
|
|
|
* |
79
|
|
|
* @return NotFoundException |
80
|
|
|
*/ |
81
|
|
|
public static function nameOfQuery($object, Exception $cause = null) |
82
|
|
|
{ |
83
|
|
|
return self::notFound('name of query', $object, 4, $cause); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param string $messageName |
88
|
|
|
* @param Exception|null $cause |
89
|
|
|
* |
90
|
|
|
* @return NotFoundException |
91
|
|
|
*/ |
92
|
|
|
public static function handlerFor($messageName, Exception $cause = null) |
93
|
|
|
{ |
94
|
|
|
return new static(sprintf( |
95
|
|
|
'Not found a handler for a given message named %s', |
96
|
|
|
$messageName |
97
|
|
|
), 5, $cause); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Creates an exception for a not found middleware. |
102
|
|
|
* |
103
|
|
|
* @param string $type |
104
|
|
|
* @param Exception|null $cause |
105
|
|
|
* |
106
|
|
|
* @return NotFoundException |
107
|
|
|
*/ |
108
|
|
|
public static function middlewareOfType($type, Exception $cause = null) |
109
|
|
|
{ |
110
|
|
|
return new static(sprintf( |
111
|
|
|
'Not found a middleware of type %s', |
112
|
|
|
$type |
113
|
|
|
), 6, $cause); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param mixed $object |
118
|
|
|
* @param string $method |
119
|
|
|
* @param Exception|null $cause |
120
|
|
|
* |
121
|
|
|
* @return NotFoundException |
122
|
|
|
*/ |
123
|
|
|
public static function methodForObject($object, $method, Exception $cause = null) |
124
|
|
|
{ |
125
|
|
|
return self::notFound('method with name `'.$method.'`', $object, 7, $cause); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.