Passed
Push — feature/0.7.0 ( 0c7d59...ae5b22 )
by Ryuichi
78:01 queued 33:04
created

Database   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getAnnotationInfo() 0 3 1
A onClassInject() 0 13 2
A onInject() 0 4 1
1
<?php
2
namespace WebStream\Annotation\Attributes;
3
4
use WebStream\Annotation\Base\Annotation;
5
use WebStream\Annotation\Base\IAnnotatable;
6
use WebStream\Annotation\Base\IRead;
7
use WebStream\Annotation\Base\IClass;
8
use WebStream\Container\Container;
9
use WebStream\Exception\Extend\DatabaseException;
10
use WebStream\IO\File;
11
12
/**
13
 * Database
14
 * @author Ryuichi TANAKA.
15
 * @since 2013/12/07
16
 * @version 0.7
17
 *
18
 * @Annotation
19
 * @Target("CLASS")
20
 */
21
class Database extends Annotation implements IClass, IRead
22
{
23
    /**
24
     * @var array<string> 注入アノテーション情報
25
     */
26
    private $injectAnnotation;
27
28
    /**
29
     * @var array<string> 読み込みアノテーション情報
30
     */
31
    private $readAnnotation;
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function onInject(array $injectAnnotation)
37
    {
38
        $this->injectAnnotation = $injectAnnotation;
39
        $this->readAnnotation = [];
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function getAnnotationInfo(): array
46
    {
47
        return $this->readAnnotation;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function onClassInject(IAnnotatable $instance, \ReflectionClass $class, Container $container)
54
    {
55
        $driver = $this->injectAnnotation['driver'];
56
        $config = $this->injectAnnotation['config'];
57
58
        if (!class_exists($driver)) {
59
            throw new DatabaseException("Database driver is undefined:" . $driver);
60
        }
61
62
        $file = new File($container->rootPath . '/' . $config);
0 ignored issues
show
Bug Best Practice introduced by
The property rootPath does not exist on WebStream\Container\Container. Since you implemented __get, consider adding a @property annotation.
Loading history...
63
        $this->readAnnotation['filepath'] = $class->getFileName();
64
        $this->readAnnotation['configPath'] = $file->getAbsoluteFilePath();
65
        $this->readAnnotation['driverClassPath'] = $driver;
66
    }
67
}
68