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

BindValidator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 11
c 5
b 1
f 0
lcom 0
cbo 3
dl 0
loc 48
ccs 17
cts 17
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A constructor() 0 6 4
A to() 0 10 4
A toProvider() 0 9 3
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