ImageEntityGenerator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 3
dl 0
loc 74
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
     * @param bool       $overwrite
31
     */
32
    public function __construct(Filesystem $filesystem, $overwrite = false)
33
    {
34
        $this->filesystem = $filesystem;
35
        $this->overwrite  = $overwrite;
36
    }
37
38
    /**
39
     * @param BundleInterface $bundle
40
     * @param                 $name
41
     */
42
    public function generate(BundleInterface $bundle, $name)
43
    {
44
        $bundleDir = $bundle->getPath();
45
        $imageDir  = $bundleDir . '/Entity';
46
        self::mkdir($imageDir);
47
48
        $imageClassName = $name;
49
        $imageFile      = $imageDir . '/' . $imageClassName . '.php';
50
51
        $parameters = [
52
            'namespace'  => $bundle->getNamespace(),
53
            'class_name' => $imageClassName,
54
            'name'       => $name,
55
            'table'      => strtolower($name), // @TODO: Use the tablize function
56
        ];
57
58
        // Build an array of files to be created
59
        $filesArray   = [];
60
        $filesArray[] = [
61
            'entity/Image.php.twig',
62
            $imageFile,
63
            $parameters,
64
        ];
65
66
        if (!empty($filesArray)) {
67
            $this->generateFiles($filesArray);
68
        }
69
    }
70
71
    /**
72
     * @param array $files
73
     */
74
    protected function generateFiles(array $files)
75
    {
76
        // Set generator to look in correct directory for notifications template.
77
        $path = __DIR__ . '/../Resources/skeleton';
78
        $this->setSkeletonDirs([$path]);
79
80
        // Check that each file does not already exist
81
        foreach ($files as $file) {
82
            if ($this->filesystem->exists($file[1]) && empty($this->overwrite)) {
83
                throw new \RuntimeException(sprintf('"%s" already exists', $file[1]));
84
            }
85
        }
86
87
        // Generate each file
88
        foreach ($files as $file) {
89
            // Template, destination, params
90
            $this->renderFile($file[0], $file[1], $file[2]);
91
        }
92
    }
93
}