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

BindValidator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 58.33%

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 7
cts 12
cp 0.5833
rs 10

3 Methods

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