PHPCRGenerator::generateDocumentFiles()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 8.425
c 0
b 0
f 0
cc 6
nc 6
nop 2

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\EasyExtendsBundle\Generator;
15
16
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class PHPCRGenerator implements GeneratorInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $DocumentTemplate;
25
26
    /**
27
     * @var string
28
     */
29
    protected $DocumentRepositoryTemplate;
30
31
    public function __construct()
32
    {
33
        $this->DocumentTemplate = (string) file_get_contents(__DIR__.'/../Resources/skeleton/phpcr/document.mustache');
34
        $this->DocumentRepositoryTemplate = (string) file_get_contents(
35
            __DIR__.'/../Resources/skeleton/phpcr/repository.mustache'
36
        );
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function generate(OutputInterface $output, BundleMetadata $bundleMetadata): void
43
    {
44
        $this->generateMappingDocumentFiles($output, $bundleMetadata);
45
        $this->generateDocumentFiles($output, $bundleMetadata);
46
        $this->generateDocumentRepositoryFiles($output, $bundleMetadata);
47
    }
48
49
    public function generateMappingDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void
50
    {
51
        $output->writeln(' - Copy Document files');
52
53
        $files = $bundleMetadata->getPhpcrMetadata()->getDocumentMappingFiles();
54
        foreach ($files as $file) {
55
            // copy mapping definition
56
            $fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.'));
57
58
            $dest_file = sprintf(
59
                '%s/%s',
60
                $bundleMetadata->getPhpcrMetadata()->getExtendedMappingDocumentDirectory(),
61
                $fileName
62
            );
63
            $src_file = sprintf(
64
                '%s/%s',
65
                $bundleMetadata->getPhpcrMetadata()->getMappingDocumentDirectory(),
66
                $file->getFileName()
67
            );
68
69
            if (is_file($dest_file)) {
70
                $output->writeln(sprintf('   ~ <info>%s</info>', $fileName));
71
            } else {
72
                $output->writeln(sprintf('   + <info>%s</info>', $fileName));
73
74
                $mappingEntityTemplate = file_get_contents($src_file);
75
76
                $string = Mustache::replace($mappingEntityTemplate, [
77
                    'namespace' => $bundleMetadata->getExtendedNamespace(),
78
                ]);
79
80
                file_put_contents($dest_file, $string);
81
            }
82
        }
83
    }
84
85
    public function generateDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void
86
    {
87
        $output->writeln(' - Generating Document files');
88
89
        $names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames();
90
91
        foreach ($names as $name) {
92
            $extendedName = $name;
93
94
            $dest_file = sprintf(
95
                '%s/%s.php',
96
                $bundleMetadata->getPhpcrMetadata()->getExtendedDocumentDirectory(),
97
                $name
98
            );
99
            $src_file = sprintf(
100
                '%s/%s.php',
101
                $bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(),
102
                $extendedName
103
            );
104
105
            if (!is_file($src_file)) {
106
                $extendedName = 'Base'.$name;
107
                $src_file = sprintf(
108
                    '%s/%s.php',
109
                    $bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(),
110
                    $extendedName
111
                );
112
113
                if (!is_file($src_file)) {
114
                    $output->writeln(sprintf('   ! <info>%s</info>', $extendedName));
115
116
                    continue;
117
                }
118
            }
119
120
            if (is_file($dest_file)) {
121
                $output->writeln(sprintf('   ~ <info>%s</info>', $name));
122
            } else {
123
                $output->writeln(sprintf('   + <info>%s</info>', $name));
124
125
                $string = Mustache::replace($this->getDocumentTemplate(), [
126
                    'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
127
                    'name' => $extendedName,
128
                    'class' => $name,
129
                    'extended_name' => $name === $extendedName ? 'Base'.$name : $extendedName,
130
                    'namespace' => $bundleMetadata->getNamespace(),
131
                ]);
132
133
                file_put_contents($dest_file, $string);
134
            }
135
        }
136
    }
137
138
    public function generateDocumentRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void
139
    {
140
        $output->writeln(' - Generating Document repository files');
141
142
        $names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames();
143
144
        foreach ($names as $name) {
145
            $dest_file = sprintf(
146
                '%s/%sRepository.php',
147
                $bundleMetadata->getPhpcrMetadata()->getExtendedDocumentDirectory(),
148
                $name
149
            );
150
            $src_file = sprintf(
151
                '%s/Base%sRepository.php',
152
                $bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(),
153
                $name
154
            );
155
156
            if (!is_file($src_file)) {
157
                $output->writeln(sprintf('   ! <info>%sRepository</info>', $name));
158
159
                continue;
160
            }
161
162
            if (is_file($dest_file)) {
163
                $output->writeln(sprintf('   ~ <info>%sRepository</info>', $name));
164
            } else {
165
                $output->writeln(sprintf('   + <info>%sRepository</info>', $name));
166
167
                $string = Mustache::replace($this->getDocumentRepositoryTemplate(), [
168
                    'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
169
                    'name' => $name,
170
                    'namespace' => $bundleMetadata->getNamespace(),
171
                ]);
172
173
                file_put_contents($dest_file, $string);
174
            }
175
        }
176
    }
177
178
    public function getDocumentTemplate(): string
179
    {
180
        return $this->DocumentTemplate;
181
    }
182
183
    public function getDocumentRepositoryTemplate(): string
184
    {
185
        return $this->DocumentRepositoryTemplate;
186
    }
187
}
188