Passed
Push — master ( bcc5fb...a0f044 )
by Alain
03:34
created

FailedToInstantiateObject::fromFactory()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 2
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
ccs 0
cts 10
cp 0
crap 12
1
<?php
2
/**
3
 * Bright Nucleus Shortcode Component.
4
 *
5
 * @package   BrightNucleus\Shortcode
6
 * @author    Alain Schlesser <[email protected]>
7
 * @license   GPL-2.0+
8
 * @link      http://www.brightnucleus.com/
9
 * @copyright 2015-2016 Alain Schlesser, Bright Nucleus
10
 */
11
12
namespace BrightNucleus\Shortcode\Exception;
13
14
use BrightNucleus\Exception\RuntimeException;
15
16
/**
17
 * Class FailedToInstantiateObject.
18
 *
19
 * @since   0.3.0
20
 *
21
 * @package BrightNucleus\Shortcode\Exception
22
 * @author  Alain Schlesser <[email protected]>
23
 */
24
class FailedToInstantiateObject extends RuntimeException implements ShortcodeException {
25
26
	/**
27
	 * Create a new instance from a passed-in class name or factory callable.
28
	 *
29
	 * @since 0.3.0
30
	 *
31
	 * @param mixed  $factory   Class name or factory callable.
32
	 * @param string $interface Interface name that the object should have
33
	 *                          implemented.
34
	 * @return static Instance of an exception.
35
	 */
36
	public static function fromFactory( $factory, $interface ) {
0 ignored issues
show
Coding Style introduced by
The function name fromFactory is in camel caps, but expected from_factory instead as per the coding standard.
Loading history...
37
		if ( is_callable( $factory ) ) {
38
			$message = sprintf(
39
				'Could not instantiate object of type "%1$s" from factory of type: "%2$s".',
40
				$interface,
41
				gettype( $factory )
42
			);
43
		} elseif ( is_string( $factory ) ) {
44
			$message = sprintf(
45
				'Could not instantiate object of type "%1$s" from class name: "%2$s".',
46
				$interface,
47
				$factory
48
			);
49
		} else {
50
			$message = sprintf(
51
				'Could not instantiate object of type "%1$s" from invalid argument of type: "%2$s".',
52
				$interface,
53
				$factory
54
			);
55
		}
56
		return new static( $message );
57
	}
58
59
	/**
60
	 * Create a new instance from a passed-in object that does not implement the
61
	 * correct interface.
62
	 *
63
	 * @since 0.3.0
64
	 *
65
	 * @param mixed  $factory   Class name or factory callable.
66
	 * @param string $interface Interface name that the object should have
67
	 *                          implemented.
68
	 * @return static Instance of an exception.
69
	 */
70
	public static function fromInvalidObject( $factory, $interface ) {
0 ignored issues
show
Unused Code introduced by
The parameter $interface is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Coding Style introduced by
The function name fromInvalidObject is in camel caps, but expected from_invalid_object instead as per the coding standard.
Loading history...
71
		$message = sprintf(
72
			'Could not instantiate object : "%1$s".',
73
			$factory
74
		);
75
		return new static( $message );
76
	}
77
}
78