Completed
Push — master ( a06a95...a1d542 )
by dan
15s
created

ImageEntityGenerator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 73
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B generate() 0 28 2
B generateFiles() 0 19 5
1
<?php
2
/**
3
 * This file is part of the IrishDan\ResponsiveImageBundle package.
4
 *
5
 * (c) Daniel Byrne <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE file that was distributed with this source
8
 * code.
9
 */
10
11
namespace IrishDan\ResponsiveImageBundle\Generator;
12
13
use Sensio\Bundle\GeneratorBundle\Generator\Generator;
14
use Symfony\Component\Filesystem\Filesystem;
15
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
16
17
/**
18
 * Generates a Notification inside a bundle.
19
 */
20
class ImageEntityGenerator extends Generator
21
{
22
    /** @var Filesystem */
23
    private $filesystem;
24
    private $overwrite;
25
26
    /**
27
     * NotificationGenerator constructor.
28
     *
29
     * @param Filesystem $filesystem
30
     */
31
    public function __construct(Filesystem $filesystem, $overwrite = false)
32
    {
33
        $this->filesystem = $filesystem;
34
        $this->overwrite  = $overwrite;
35
    }
36
37
    /**
38
     * @param BundleInterface $bundle
39
     * @param                 $name
40
     */
41
    public function generate(BundleInterface $bundle, $name)
42
    {
43
        $bundleDir = $bundle->getPath();
44
        $imageDir  = $bundleDir . '/Entity';
45
        self::mkdir($imageDir);
46
47
        $imageClassName = $name;
48
        $imageFile      = $imageDir . '/' . $imageClassName . '.php';
49
50
        $parameters = [
51
            'namespace'  => $bundle->getNamespace(),
52
            'class_name' => $imageClassName,
53
            'name'       => $name,
54
            'table'      => strtolower($name), // @TODO: Use the tablize function
55
        ];
56
57
        // Build an array of files to be created
58
        $filesArray   = [];
59
        $filesArray[] = [
60
            'entity/Image.php.twig',
61
            $imageFile,
62
            $parameters,
63
        ];
64
65
        if (!empty($filesArray)) {
66
            $this->generateFiles($filesArray);
67
        }
68
    }
69
70
    /**
71
     * @param array $files
72
     */
73
    protected function generateFiles(array $files)
74
    {
75
        // Set generator to look in correct directory for notifications template.
76
        $path = __DIR__ . '/../Resources/skeleton';
77
        $this->setSkeletonDirs([$path]);
78
79
        // Check that each file does not already exist
80
        foreach ($files as $file) {
81
            if ($this->filesystem->exists($file[1]) && empty($this->overwrite)) {
82
                throw new \RuntimeException(sprintf('"%s" already exists', $file[1]));
83
            }
84
        }
85
86
        // Generate each file
87
        foreach ($files as $file) {
88
            // Template, destination, params
89
            $this->renderFile($file[0], $file[1], $file[2]);
90
        }
91
    }
92
}