Passed
Push — master ( e7b389...95f69d )
by 世昌
02:10
created

StatementSet::build()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
4
namespace suda\application\database;
5
6
use suda\application\Application;
7
use suda\application\exception\ConfigurationException;
8
use suda\application\Resource as ApplicationResource;
9
use suda\framework\Config;
10
use suda\orm\statement\QueryAccess;
11
use suda\orm\struct\QueryStatement;
12
13
class StatementSet
14
{
15
    /**
16
     * 查询操作
17
     * @var QueryAccess
18
     */
19
    protected $access;
20
21
    /**
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * @var array
28
     */
29
    protected $config;
30
31
    /**
32
     * Statement constructor.
33
     * @param QueryAccess $access
34
     * @param string $name
35
     */
36
    public function __construct(QueryAccess $access, string $name)
37
    {
38
        $this->access = $access;
39
        $this->name = $name;
40
    }
41
42
    /**
43
     * @param Application $application
44
     * @return $this
45
     */
46
    public function load(Application $application)
47
    {
48
        list($resource, $name) = $this->parseNameResource($application, $this->name);
49
        $this->config = $this->getConfigFrom($resource, $name);
50
        return $this;
51
    }
52
53
    /**
54
     * @param string $name
55
     * @return QueryStatement
56
     */
57
    public function build(string $name):QueryStatement
58
    {
59
        if (array_key_exists($name, $this->config) === false) {
60
            throw new ConfigurationException('missing statement @resource:'.$this->name.'#'.$name);
61
        }
62
        $config =  $this->config[$name];
63
        return (new QueryStatementBuilder($this->access, $config))->build();
64
    }
65
66
    /**
67
     * @param ApplicationResource $resource
68
     * @param string $name
69
     * @return array
70
     */
71
    protected function getConfigFrom(ApplicationResource $resource, string $name):array
72
    {
73
        $configPath = $resource->getConfigResourcePath($name);
74
        if ($configPath !== null) {
75
            $config = Config::loadConfig($configPath);
76
            if ($config !== null) {
77
                return $config;
78
            }
79
        }
80
        throw new ConfigurationException('missing statement @resource:'.$this->name);
81
    }
82
83
    /**
84
     * @param Application $application
85
     * @param string $name
86
     * @return array
87
     */
88
    protected function parseNameResource(Application $application, string $name)
89
    {
90
        if (strpos($name, ':') > 0) {
91
            list($module, $group, $name) = $application->parseRouteName($name);
92
        }
93
        if ($module !== null && ($moduleObj = $application->find($module))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $module does not seem to be defined for all execution paths leading up to this point.
Loading history...
94
            $res =  $moduleObj->getResource();
95
        } else {
96
            $res = $application->getResource();
97
        }
98
        return [$res, $name];
99
    }
100
}
101