Completed
Push — master ( 96ce85...a97445 )
by arto
11:09
created

FileContentGenerator::generate()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 31
rs 8.8571
c 1
b 0
f 0
cc 2
eloc 18
nc 2
nop 2
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-09-19
5
 */
6
namespace Net\Bazzline\Propel\Behavior\EntityInstantiator;
7
8
class FileContentGenerator
9
{
10
    /**
11
     * @param EntityCollection $collection
12
     * @param Configuration $configuration
13
     * @return string
14
     */
15
    public function generate(EntityCollection $collection, Configuration $configuration)
16
    {
17
        $useStatements = $this->generateUseStatements($configuration->getNamespace(), $collection);
18
19
        $content = $this->generateFileHeader(
20
            $configuration->getNamespace(),
21
            $useStatements
22
        );
23
24
        $content .= $this->generateClassHeader(
25
            $configuration->getClassName(),
26
            $configuration->getExtends()
27
        );
28
29
        $content .= $this->generateGetConnectionMethod(
30
            $configuration->getIndention(),
31
            $configuration->getDefaultConnectionMode(),
32
            $configuration->getDefaultConnectionName()
33
        );
34
35
        foreach ($collection as $entity) {
36
            $content .= $this->generateObjectEntityOrQueryEntityGetMethod(
37
                $entity,
38
                $configuration->getIndention()
39
            );
40
        }
41
42
        $content .= '}';
43
44
        return $content;
45
    }
46
47
    /**
48
     * @param string $className
49
     * @param null|string $extends
50
     * @return string
51
     * @todo find a better way to have it indented and readable
52
     */
53
    private function generateClassHeader($className, $extends)
54
    {
55
        $extends = ($this->isValidString($extends)) ? ' extends ' . $extends : '';
56
57
    return '
58
/**
59
 * Class ' . $className . '
60
 *
61
 * @author ' . __CLASS__ . '
62
 * @since ' . date('Y-m-d') . '
63
 * @link http://www.bazzline.net
64
 */
65
class ' . $className . $extends . '
66
{' . PHP_EOL;
67
    }
68
69
    /**
70
     * @param AbstractEntity $entity
71
     * @param string $indention
72
     * @return string
73
     */
74
    private function generateObjectEntityOrQueryEntityGetMethod(AbstractEntity $entity, $indention)
75
    {
76
        $methodName = lcfirst($entity->methodNamePrefix() . ucfirst($entity->className()));
77
78
        $content   = PHP_EOL .
79
            $indention . '/**' . PHP_EOL .
80
            $indention . ' * @return ' . $entity->className() . PHP_EOL .
81
            $indention . ' */' . PHP_EOL .
82
            $indention . 'public function ' . $methodName . '()' . PHP_EOL .
83
            $indention . '{' . PHP_EOL;
84
85
        if ($entity instanceof ObjectEntity) {
86
            $content .= $indention . $indention . 'return new ' . $entity->className() . '();' . PHP_EOL;
87
        } else if ($entity instanceof QueryEntity) {
88
            $content .= $indention . $indention . 'return ' . $entity->className() . '::create();' . PHP_EOL;
89
        }
90
91
        $content .= $indention . '}' . PHP_EOL;
92
93
        return $content;
94
    }
95
96
    /**
97
     * @param null|string $namespace
98
     * @param array $uses
99
     * @return string
100
     */
101
    private function generateFileHeader($namespace, $uses)
102
    {
103
        $content = '<?php';
104
        $content .= ($this->isValidString($namespace))
105
            ? str_repeat(PHP_EOL, 2) . 'namespace ' . $namespace . ';' . PHP_EOL
106
            : PHP_EOL;
107
108
        $thereAreUseStatements = (!empty($uses));
109
110
        if ($thereAreUseStatements) {
111
            $content .= PHP_EOL;
112
            $content .= implode(PHP_EOL, $uses);
113
            $content .= PHP_EOL;
114
        }
115
116
        return $content;
117
    }
118
119
    /**
120
     * @param string $indention
121
     * @param null|string $defaultConnectionMode
122
     * @param null|string $defaultConnectionName
123
     * @return string
124
     * @todo find a better way to have it indented and readable
125
     * @todo move default values out
126
     */
127
    private function generateGetConnectionMethod(
128
        $indention,
129
        $defaultConnectionMode = null,
130
        $defaultConnectionName = null
131
    ) {
132
133
return $indention . '/**
134
' . $indention . ' * @param null|string $name - The data source name that is used to look up the DSN from the runtime configuration file.
135
' . $indention . ' * @param string $mode The connection mode (this applies to replication systems).
136
' . $indention . ' * @return PDO
137
' . $indention . ' */
138
' . $indention . 'public function getConnection($name = ' . $defaultConnectionName . ', $mode = ' . $defaultConnectionMode . ')
139
' . $indention . '{
140
' . (str_repeat($indention, 2)) . 'return Propel::getConnection($name, $mode);
141
' . $indention . '}' . PHP_EOL;
142
    }
143
144
    /**
145
     * @param null|string $namespace
146
     * @param EntityCollection $collection
147
     * @return array
148
     */
149
    private function generateUseStatements($namespace, EntityCollection $collection)
150
    {
151
        $uses = array();
152
153
        if ($this->isValidString($namespace)) {
154
            $uses[] = 'use Propel;';
155
            $uses[] = 'use PDO;';
156
        }
157
158
        foreach ($collection as $entity) {
159
            $uses[] = 'use ' . $entity->fullQualifiedClassName() . ';';
160
        }
161
162
        natsort($uses);
163
164
        return $uses;
165
    }
166
167
    /**
168
     * @param null|string $namespace
169
     * @return bool
170
     */
171
    private function isValidString($namespace)
172
    {
173
        return ((is_string($namespace))
174
            && (strlen($namespace) > 0));
175
    }
176
}