Completed
Push — 2.x ( d6b11c...5f03c1 )
by Akihito
04:42 queued 02:33
created

BindValidator::toProvider()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 4.125

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 2
cts 4
cp 0.5
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
crap 4.125
1
<?php
2
3
/**
4
 * This file is part of the Ray.Di package.
5
 *
6
 * @license http://opensource.org/licenses/MIT MIT
7
 */
8
namespace Ray\Di;
9
10
use Ray\Di\Exception\InvalidProvider;
11
use Ray\Di\Exception\InvalidType;
12
use Ray\Di\Exception\NotFound;
13
14
final class BindValidator
15
{
16
    /**
17
     * @param string $interface
18
     */
19 30
    public function constructor($interface)
20
    {
21
        if ($interface && ! interface_exists($interface) && ! class_exists($interface)) {
22
            throw new NotFound($interface);
23
        }
24 30
    }
25
26
    /**
27
     * To validator
28
     *
29
     * @param string $interface
30
     * @param string $class
31
     */
32 13
    public function to($interface, $class)
33
    {
34
        if (! class_exists($class)) {
35
            throw new NotFound($class);
36
        }
37
        if (interface_exists($interface) && ! (new \ReflectionClass($class))->implementsInterface($interface)) {
38 1
            $msg = "[{$class}] is no implemented [{$interface}] interface";
39
            throw new InvalidType($msg);
40
        }
41 13
    }
42
43
    /**
44
     * toProvider validator
45
     *
46
     * @param string $provider
47
     *
48
     * @return $this
49
     *
50
     * @throws NotFound
51
     */
52 11
    public function toProvider($provider)
53
    {
54
        if (! class_exists($provider)) {
55
            throw new NotFound($provider);
56
        }
57
        if (! (new \ReflectionClass($provider))->implementsInterface('Ray\Di\ProviderInterface')) {
58
            throw new InvalidProvider($provider);
59
        }
60 11
    }
61
}
62