Completed
Branch 2.x (dc1b30)
by Akihito
02:25
created

BindValidator::to()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 6
nc 3
nop 2
crap 4
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 80
    public function constructor($interface)
20
    {
21 80
        if ($interface && ! interface_exists($interface) && ! class_exists($interface)) {
22 2
            throw new NotFound($interface);
23
        }
24 79
    }
25
26
    /**
27
     * To validator
28
     *
29
     * @param string $interface
30
     * @param string $class
31
     */
32 31
    public function to($interface, $class)
33
    {
34 31
        if (! class_exists($class)) {
35 1
            throw new NotFound($class);
36
        }
37 30
        if (interface_exists($interface) && ! (new \ReflectionClass($class))->implementsInterface($interface)) {
38 1
            $msg = "[{$class}] is no implemented [{$interface}] interface";
39 1
            throw new InvalidType($msg);
40
        }
41 29
    }
42
43
    /**
44
     * toProvider validator
45
     *
46
     * @param string $provider
47
     *
48
     * @return $this
49
     *
50
     * @throws NotFound
51
     */
52 14
    public function toProvider($provider)
53
    {
54 14
        if (! class_exists($provider)) {
55 1
            throw new NotFound($provider);
56
        }
57 13
        if (! (new \ReflectionClass($provider))->implementsInterface('Ray\Di\ProviderInterface')) {
58 1
            throw new InvalidProvider($provider);
59
        }
60 12
    }
61
}
62