Completed
Push — master ( 6ebd1c...df9bae )
by John
10:14
created

ResourceGenerator::generate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
c 2
b 2
f 0
dl 0
loc 19
rs 9.4285
cc 2
eloc 12
nc 2
nop 3
1
<?php
2
/*
3
 * This file is part of the KleijnWeb\SwaggerBundle package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace KleijnWeb\SwaggerBundle\Dev\Generator;
10
11
use KleijnWeb\SwaggerBundle\Document\SwaggerDocument;
12
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
13
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
14
15
/**
16
 * @author John Kleijn <[email protected]>
17
 */
18
class ResourceGenerator extends Generator
19
{
20
    /**
21
     * @param BundleInterface $bundle
22
     * @param SwaggerDocument $document
23
     * @param string          $relativeNamespace
24
     */
25
    public function generate(BundleInterface $bundle, SwaggerDocument $document, $relativeNamespace = 'Model\Resources')
26
    {
27
        $dir = $bundle->getPath();
28
29
        $parameters = [
30
            'namespace'          => $bundle->getNamespace(),
31
            'bundle'             => $bundle->getName(),
32
            'resource_namespace' => $relativeNamespace
33
        ];
34
35
        foreach ($document->getResourceSchemas() as $typeName => $spec) {
36
            $resourceFile = "$dir/" . str_replace('\\', '/', $relativeNamespace) . "/$typeName.php";
37
            $this->renderFile(
38
                'resource.php.twig',
39
                $resourceFile,
40
                array_merge($parameters, $spec, ['resource' => $typeName, 'resource_class' => $typeName])
41
            );
42
        }
43
    }
44
}
45