ContainerException::fromPrevious()   B
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 2
nop 3
crap 5
1
<?php
2
/*
3
* The MIT License (MIT)
4
* Copyright © 2017 Marcos Lois Bermudez <[email protected]>
5
* *
6
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
* documentation files (the “Software”), to deal in the Software without restriction, including without limitation
8
* the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
* *
11
* The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
* of the Software.
13
* *
14
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
15
* TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
* DEALINGS IN THE SOFTWARE.
19
*/
20
21
namespace TTIC\Interop\Container\Exception;
22
23
use Exception;
24
use Interop\Container\Exception\ContainerException as InteropContainerException;
25
use RuntimeException;
26
27
/**
28
 * An error occurred when trying to retrieve an entry from the container.
29
 *
30
 * @author Marcos Lois Bermúdez <[email protected]>
31
 */
32
class ContainerException extends RuntimeException implements InteropContainerException
33
{
34
    /**
35
     * @var string The message template. Allowed variables are {error} and {id}
36
     */
37
    protected static $template = 'An {error} occurred when attempting to retrieve the "{id}" entry from the container.';
38
39
    /**
40
     * Creates a ContainerException by using the information from the previous exception
41
     *
42
     * @param string|mixed $id
43
     * @param \Exception $prev
44
     * @param int $code
45
     * @return \TTIC\Interop\Container\Exception\ContainerException
46
     */
47 6
    public static function fromPrevious($id, Exception $prev = null, $code = 0)
48
    {
49 6
        $message = strtr(static::$template, array(
50 6
            '{id}'    => (is_string($id) || method_exists($id, '__toString')) ? $id : '?',
51 6
            '{error}' => $prev ? get_class($prev) : 'error',
52 2
        ));
53
54 6
        if ($prev) {
55 3
            $message .= ' Message: ' . $prev->getMessage();
56 1
        }
57
58 6
        return new static($message, $code, $prev);
59
    }
60
}
61