Writer::getPhpClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the ClassGeneration package.
5
 *
6
 * (c) Antonio Spinelli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ClassGeneration;
13
14
use ClassGeneration\Element\ElementAbstract;
15
16
/**
17
 * @author Antonio Spinelli <[email protected]>
18
 */
19
class Writer extends ElementAbstract implements WriterInterface
20
{
21
22
    protected $fileName;
23
24
    /**
25
     * @var PhpClassInterface
26
     */
27
    protected $phpClass;
28
29
    /**
30
     * @var string
31
     */
32
    protected $path;
33
34
    /**
35
     * @inheritdoc
36
     */
37 6
    public function __construct($class = null)
38
    {
39 6
        if (is_array($class)) {
40 1
            $this->setOptions($class);
41 1
        }
42 6
        if ($class instanceof PhpClassInterface) {
43 1
            $this->setPhpClass($class);
44 1
        }
45 6
        $this->init();
46 6
    }
47
48
    /**
49
     * @{inheritdoc}
50
     */
51 6
    public function init()
52
    {
53 6
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58 2
    public function setPhpClass(PhpClassInterface $class)
59
    {
60 2
        $this->phpClass = $class;
61
62 2
        return $this;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68 1
    public function getPhpClass()
69
    {
70 1
        return $this->phpClass;
71
    }
72
73
    /**
74
     * @inheritdoc
75
     */
76 2
    public function setPath($path)
77
    {
78 2
        if (!is_dir($path) || !is_writable($path)) {
79 1
            throw new \RuntimeException('This directory ' . $path . ' not exists or not writable');
80
        }
81
82 2
        $this->path = $path;
83
84 2
        return $this;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90 2
    public function getPath()
91
    {
92 2
        return $this->path;
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98 1
    public function setFileName($fileName)
99
    {
100 1
        $this->fileName = $fileName;
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108 2
    public function getFileName()
109
    {
110 2
        return (!is_null($this->fileName) ? $this->fileName : $this->getPhpClass()->getName());
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 1
    public function write()
117
    {
118 1
        $fileName = $this->getFileName();
119
120 1
        $directoryPath = realpath($this->getPath());
121 1
        $namespace = $this->getPhpClass()->getNamespace()->getPath();
122
123 1
        $directoryPath .= DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
124 1
        $fullFileName = $directoryPath . DIRECTORY_SEPARATOR . $fileName . '.php';
125 1
        if (!is_dir($directoryPath)) {
126 1
            mkdir($directoryPath, 0777, true);
127 1
        }
128 1
        $file = new \SplFileObject($fullFileName, 'w');
129 1
        $file->fwrite($this->getPhpClass()->toString());
130 1
    }
131
132
    /**
133
     * @inheritdoc
134
     */
135 1
    public function toString()
136
    {
137 1
        throw new \RuntimeException('toString is not applicable in this context');
138
    }
139
}
140