Completed
Push — master ( 73492b...2db3cf )
by Jeremy
11:56
created

writeDocumentRepositoryClass()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 4.0047

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 14
cts 15
cp 0.9333
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 8
nop 3
crap 4.0047
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ODM\MongoDB\Tools;
21
22
/**
23
 * Class to generate document repository classes
24
 *
25
 * @since   1.0
26
 */
27
class DocumentRepositoryGenerator
28
{
29
    protected static $template =
30
'<?php
31
32
namespace <namespace>;
33
34
use Doctrine\ODM\MongoDB\DocumentRepository;
35
36
/**
37
 * <className>
38
 *
39
 * This class was generated by the Doctrine ODM. Add your own custom
40
 * repository methods below.
41
 */
42
class <className> extends DocumentRepository
43
{
44
}
45
';
46
47 2
    public function generateDocumentRepositoryClass($fullClassName)
48
    {
49 2
        $namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\'));
50 2
        $className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName));
51
52
        $variables = array(
53 2
            '<namespace>' => $namespace,
54 2
            '<className>' => $className
55
        );
56 2
        return str_replace(array_keys($variables), array_values($variables), self::$template);
57
    }
58
59 2
    public function writeDocumentRepositoryClass($fullClassName, $outputDirectory, $outputDirectoryNamespace = null)
60
    {
61 2
        $code = $this->generateDocumentRepositoryClass($fullClassName);
62
        
63 2
        if (null === $outputDirectoryNamespace) {
64 1
            $relativeClassName = $fullClassName;
65
        } else {
66 1
            $relativeClassName = preg_replace(
67 1
                '/^'.str_replace('\\', '\\\\', $outputDirectoryNamespace).'\\\\/', '', $fullClassName
68
            );
69
        }
70
        
71 2
        $path = $outputDirectory . DIRECTORY_SEPARATOR
72 2
              . str_replace('\\', \DIRECTORY_SEPARATOR, $relativeClassName) . '.php';
73 2
        $dir = dirname($path);
74
75 2
        if ( ! is_dir($dir)) {
76
            mkdir($dir, 0775, true);
77
        }
78
79 2
        if ( ! file_exists($path)) {
80 2
            file_put_contents($path, $code);
81 2
            chmod($path, 0664);
82
        }
83 2
    }
84
}
85